BarInsta/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java

84 lines
3.1 KiB
Java
Executable File

package awais.instagrabber.asyncs;
import android.os.AsyncTask;
import android.util.Log;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;
import java.net.HttpURLConnection;
import java.net.URL;
import awais.instagrabber.BuildConfig;
import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.models.ProfileModel;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.Utils;
import awaisomereport.LogCollector;
import static awais.instagrabber.utils.Utils.logCollector;
public final class ProfileFetcher extends AsyncTask<Void, Void, ProfileModel> {
private final FetchListener<ProfileModel> fetchListener;
private final String userName;
public ProfileFetcher(String userName, FetchListener<ProfileModel> fetchListener) {
this.userName = userName;
this.fetchListener = fetchListener;
}
@Nullable
@Override
protected ProfileModel doInBackground(final Void... voids) {
ProfileModel result = null;
try {
final HttpURLConnection conn = (HttpURLConnection) new URL("https://www.instagram.com/" + userName + "/?__a=1").openConnection();
conn.setUseCaches(true);
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
final JSONObject user = new JSONObject(Utils.readFromConnection(conn)).getJSONObject("graphql").getJSONObject(Constants.EXTRAS_USER);
boolean isPrivate = user.getBoolean("is_private");
final JSONObject timelineMedia = user.getJSONObject("edge_owner_to_timeline_media");
if (timelineMedia.has("edges")) {
final JSONArray edges = timelineMedia.getJSONArray("edges");
if (edges.length() > 0) isPrivate = false;
}
String url = user.optString("external_url");
if (Utils.isEmpty(url)) url = null;
result = new ProfileModel(isPrivate,
user.getBoolean("is_verified"),
user.getString(Constants.EXTRAS_ID),
userName,
user.getString("full_name"),
user.getString("biography"),
url,
user.getString("profile_pic_url"),
user.getString("profile_pic_url_hd"),
timelineMedia.getLong("count"),
user.getJSONObject("edge_followed_by").getLong("count"),
user.getJSONObject("edge_follow").getLong("count"));
}
conn.disconnect();
} catch (final Exception e) {
if (logCollector != null)
logCollector.appendException(e, LogCollector.LogFile.ASYNC_PROFILE_FETCHER, "doInBackground");
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
}
return result;
}
@Override
protected void onPostExecute(final ProfileModel result) {
if (fetchListener != null) fetchListener.onResult(result);
}
}