1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-09-20 18:17:29 +00:00
BarInsta/app/src/main/java/awais/instagrabber/asyncs/ProfilePictureFetcher.java

108 lines
4.3 KiB
Java
Raw Normal View History

2020-07-01 17:08:28 +00:00
package awais.instagrabber.asyncs;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Pair;
import org.json.JSONObject;
2020-08-04 02:28:42 +00:00
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
2020-07-01 17:08:28 +00:00
import java.net.HttpURLConnection;
import java.net.URL;
import awais.instagrabber.BuildConfig;
import awais.instagrabber.interfaces.FetchListener;
2020-07-31 19:53:32 +00:00
import awais.instagrabber.utils.Constants;
2020-07-01 17:08:28 +00:00
import awais.instagrabber.utils.Utils;
import awaisomereport.LogCollector;
2020-07-31 19:53:32 +00:00
2020-07-01 17:08:28 +00:00
import static awais.instagrabber.utils.Utils.logCollector;
public final class ProfilePictureFetcher extends AsyncTask<Void, Void, String> {
private final FetchListener<String> fetchListener;
2020-07-25 21:10:26 +00:00
private final String userName, userId, picUrl;
private final boolean isHashtag;
2020-07-01 17:08:28 +00:00
public ProfilePictureFetcher(final String userName, final String userId, final FetchListener<String> fetchListener,
2020-07-31 19:53:32 +00:00
final String picUrl, final boolean isHashtag) {
2020-07-01 17:08:28 +00:00
this.fetchListener = fetchListener;
this.userName = userName;
this.userId = userId;
2020-07-25 21:10:26 +00:00
this.picUrl = picUrl;
this.isHashtag = isHashtag;
2020-07-01 17:08:28 +00:00
}
@Override
protected String doInBackground(final Void... voids) {
2020-08-04 02:28:42 +00:00
String out = null;
if (isHashtag) out = picUrl;
else try {
final HttpURLConnection conn =
(HttpURLConnection) new URL("https://i.instagram.com/api/v1/users/"+userId+"/info/").openConnection();
2020-07-01 17:08:28 +00:00
conn.setUseCaches(false);
2020-07-31 19:53:32 +00:00
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", Constants.USER_AGENT);
2020-07-01 17:08:28 +00:00
final String result = conn.getResponseCode() == HttpURLConnection.HTTP_OK ? Utils.readFromConnection(conn) : null;
conn.disconnect();
if (!Utils.isEmpty(result)) {
2020-07-31 19:53:32 +00:00
JSONObject data = new JSONObject(result).getJSONObject("user");
if (data.has("hd_profile_pic_url_info"))
out = data.getJSONObject("hd_profile_pic_url_info").optString("url");
2020-07-01 17:08:28 +00:00
}
2020-08-04 02:28:42 +00:00
2020-08-07 02:26:23 +00:00
if (Utils.isEmpty(out) && Utils.settingsHelper.getBoolean(Constants.INSTADP)) {
2020-08-04 02:28:42 +00:00
final HttpURLConnection backup =
(HttpURLConnection) new URL("https://instadp.com/fullsize/" + userName).openConnection();
backup.setUseCaches(false);
backup.setRequestMethod("GET");
2020-08-07 02:26:23 +00:00
backup.setRequestProperty("User-Agent", Constants.A_USER_AGENT);
2020-08-04 02:28:42 +00:00
final String instadp = backup.getResponseCode() == HttpURLConnection.HTTP_OK ? Utils.readFromConnection(backup) : null;
backup.disconnect();
if (!Utils.isEmpty(instadp)) {
final Document doc = Jsoup.parse(instadp);
boolean fallback = false;
final int imgIndex = instadp.indexOf("preloadImg('"), lastIndex;
Element element = doc.selectFirst(".instadp");
if (element != null && (element = element.selectFirst(".picture")) != null)
out = element.attr("src");
else if ((element = doc.selectFirst(".download-btn")) != null)
out = element.attr("href");
else if (imgIndex != -1 && (lastIndex = instadp.indexOf("')", imgIndex)) != -1)
out = instadp.substring(imgIndex + 12, lastIndex);
else {
final Elements imgs = doc.getElementsByTag("img");
for (final Element img : imgs) {
final String imgStr = img.toString();
if (imgStr.contains("cdninstagram.com")) out = img.attr("src");
}
}
}
}
2020-07-01 17:08:28 +00:00
} catch (final Exception e) {
if (logCollector != null)
2020-07-31 19:53:32 +00:00
logCollector.appendException(e, LogCollector.LogFile.ASYNC_PROFILE_PICTURE_FETCHER, "doInBackground");
2020-07-01 17:08:28 +00:00
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
}
return out;
}
@Override
protected void onPreExecute() {
if (fetchListener != null) fetchListener.doBefore();
}
@Override
protected void onPostExecute(final String result) {
if (fetchListener != null) fetchListener.onResult(result);
}
}