refactored FoafBase
added check for vk messenger accounts rewritten all legacy code
This commit is contained in:
@@ -6,43 +6,43 @@ import android.util.Log;
|
||||
import com.vk.core.dialogs.alert.VkAlertDialog;
|
||||
import com.vtosters.lite.R;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import ru.vtosters.lite.di.singleton.VtOkHttpClient;
|
||||
import ru.vtosters.lite.net.Request;
|
||||
import ru.vtosters.lite.utils.AndroidUtils;
|
||||
import ru.vtosters.lite.utils.LifecycleUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static android.widget.Toast.makeText;
|
||||
import static ru.vtosters.lite.proxy.ProxyUtils.getApi;
|
||||
import static ru.vtosters.lite.proxy.ProxyUtils.isAnyProxyEnabled;
|
||||
import static ru.vtosters.lite.utils.AndroidUtils.getGlobalContext;
|
||||
|
||||
public class FoafBase {
|
||||
private static final Pattern FOAF_REGEX = Pattern.compile("<ya:created dc:date=\"(.+?)\"");
|
||||
private static final String DELETED = "<ya:profileState>deleted</ya:profileState>";
|
||||
private static final String API_VKNEXT = "https://api.vtosters.app/v1/getBypassedOnlineInfo?json=1&ids=";
|
||||
private static final OkHttpClient client = VtOkHttpClient.getInstance();
|
||||
|
||||
public static JSONObject getBypassedOnlineInfo(int id) throws JSONException {
|
||||
var request = new okhttp3.Request.a()
|
||||
Request request = new Request.a()
|
||||
.b(API_VKNEXT + id)
|
||||
.a();
|
||||
|
||||
String response;
|
||||
|
||||
var dummy = new JSONObject();
|
||||
JSONObject dummy = new JSONObject();
|
||||
dummy.put("last_seen", 0);
|
||||
|
||||
try {
|
||||
response = client.a(request).execute().a().g();
|
||||
var online_info = new JSONObject(response).optJSONObject("response").optJSONObject(Integer.toString(id));
|
||||
try (Response response = client.a(request).execute()) {
|
||||
JSONObject online_info = new JSONObject(response.a().g()).getJSONObject("response").optJSONObject(Integer.toString(id));
|
||||
return online_info == null ? dummy : online_info;
|
||||
} catch (Exception e) {
|
||||
Log.e("GetBypassedOnlineInfo", e.getMessage());
|
||||
@@ -51,17 +51,14 @@ public class FoafBase {
|
||||
}
|
||||
|
||||
public static JSONObject getBypassedOnlineInfo(String ids) {
|
||||
var request = new okhttp3.Request.a()
|
||||
Request request = new Request.a()
|
||||
.b(API_VKNEXT + ids)
|
||||
.a();
|
||||
|
||||
String response;
|
||||
JSONObject dummy = new JSONObject();
|
||||
|
||||
var dummy = new JSONObject();
|
||||
|
||||
try {
|
||||
response = client.a(request).execute().a().g();
|
||||
var online_info = new JSONObject(response).optJSONObject("response");
|
||||
try (Response response = client.a(request).execute()) {
|
||||
JSONObject online_info = new JSONObject(response.a().g()).getJSONObject("response").optJSONObject("response");
|
||||
return online_info == null ? dummy : online_info;
|
||||
} catch (Exception e) {
|
||||
Log.e("GetBypassedOnlineInfo1", e.getMessage());
|
||||
@@ -70,18 +67,8 @@ public class FoafBase {
|
||||
}
|
||||
|
||||
private static String getLink(int i) {
|
||||
String str;
|
||||
StringBuilder sb;
|
||||
if (isAnyProxyEnabled()) {
|
||||
sb = new StringBuilder();
|
||||
str = "https://" + getApi() + "/_/vk.com/foaf.php?id=";
|
||||
} else {
|
||||
sb = new StringBuilder();
|
||||
str = "https://vk.com/foaf.php?id=";
|
||||
}
|
||||
sb.append(str);
|
||||
sb.append(i);
|
||||
return sb.toString();
|
||||
String prefix = isAnyProxyEnabled() ? "https://" + getApi() + "/_/vk.com/foaf.php?id=" : "https://vk.com/foaf.php?id=";
|
||||
return prefix + i;
|
||||
}
|
||||
|
||||
public static void loadAndShow(Context context, int i) {
|
||||
@@ -90,49 +77,69 @@ public class FoafBase {
|
||||
progressDialog.setCancelable(false);
|
||||
progressDialog.show();
|
||||
|
||||
Request.RequestCallback callback = str -> FoafBase.lambdaLoadAndShow(progressDialog, context, i, str);
|
||||
Request.makeRequest(getLink(i), callback);
|
||||
Request request = new Request.a()
|
||||
.b(getLink(i))
|
||||
.a();
|
||||
|
||||
try (Response response = client.a(request).execute()) {
|
||||
LifecycleUtils.getCurrentActivity().runOnUiThread(() -> {
|
||||
try {
|
||||
showFoafInfo(progressDialog, context, i, response.a().g());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Log.e("FoafBase", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
static void lambdaLoadAndShow(ProgressDialog progressDialog, Context context, int i, String str) {
|
||||
try {
|
||||
progressDialog.cancel();
|
||||
Matcher matcher = FOAF_REGEX.matcher(str);
|
||||
matcher.find();
|
||||
private static void showFoafInfo(ProgressDialog progressDialog, Context context, int i, String str) {
|
||||
progressDialog.cancel();
|
||||
Optional<String> foafDate = findPattern(str, FOAF_REGEX);
|
||||
|
||||
String m = matcher.group(1);
|
||||
Log.e("FoafBase", str);
|
||||
|
||||
if (m == null) {
|
||||
LifecycleUtils.getCurrentActivity().runOnUiThread(() -> makeText(getGlobalContext(), context.getString(R.string.foaferr), LENGTH_SHORT).show());
|
||||
return;
|
||||
}
|
||||
|
||||
String normalHumanDate = getNormalHumanDate(m);
|
||||
|
||||
LifecycleUtils.getCurrentActivity().runOnUiThread(() -> new VkAlertDialog.Builder(context)
|
||||
.setTitle(R.string.addinf)
|
||||
.setMessage(context.getString(R.string.foafid) +
|
||||
" " + i + context.getString(R.string.foafregdate)
|
||||
+ " " + normalHumanDate + context.getString(R.string.foafdate)
|
||||
+ " " + daysPassedFromFoafDate(normalHumanDate))
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.show());
|
||||
} catch (Exception e) {
|
||||
LifecycleUtils.getCurrentActivity().runOnUiThread(() -> makeText(getGlobalContext(), context.getString(R.string.foaferr), LENGTH_SHORT).show());
|
||||
if (foafDate.isPresent()) {
|
||||
String normalHumanDate = getNormalHumanDate(foafDate.get());
|
||||
long daysPassed = daysPassedFromFoafDate(normalHumanDate);
|
||||
showFoafDialog(context, i, normalHumanDate, daysPassed);
|
||||
} else if (str.contains(DELETED)) {
|
||||
AndroidUtils.sendToast("Профиль удален или не существует (аккаунт VK Мессенджера)");
|
||||
} else {
|
||||
AndroidUtils.sendToast(context.getString(R.string.foaferr));
|
||||
}
|
||||
}
|
||||
|
||||
private static Optional<String> findPattern(String str, Pattern pattern) {
|
||||
Matcher matcher = pattern.matcher(str);
|
||||
if (matcher.find() && matcher.groupCount() != 0) {
|
||||
return Optional.ofNullable(matcher.group(1));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private static void showFoafDialog(Context context, int i, String normalHumanDate, long daysPassed) {
|
||||
String message = String.format("%s %d%s %s%s %d", context.getString(R.string.foafid), i, context.getString(R.string.foafregdate), normalHumanDate, context.getString(R.string.foafdate), daysPassed);
|
||||
new VkAlertDialog.Builder(context)
|
||||
.setTitle(R.string.addinf)
|
||||
.setMessage(message)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.show();
|
||||
}
|
||||
|
||||
private static long daysPassedFromFoafDate(String str) {
|
||||
try {
|
||||
Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(str.split(" ")[1]);
|
||||
return TimeUnit.DAYS.convert(new Date().getTime() - parse.getTime(), TimeUnit.MILLISECONDS);
|
||||
} catch (ParseException e) {
|
||||
LocalDate foafDate = LocalDate.parse(str.split(" ")[1]);
|
||||
return ChronoUnit.DAYS.between(foafDate, LocalDate.now());
|
||||
} catch (DateTimeParseException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getNormalHumanDate(String str) {
|
||||
String[] split = str.split("T");
|
||||
return split[1].split("\\+")[0] + " " + split[0];
|
||||
OffsetDateTime dateTime = OffsetDateTime.parse(str);
|
||||
return dateTime.format(DateTimeFormatter.ofPattern("HH:mm:ss yyyy-MM-dd"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user