Skip to content

Instantly share code, notes, and snippets.

@bhanuraja
Created May 31, 2019 11:40
Show Gist options
  • Save bhanuraja/1f19d7d924c12d59ee2ae2a3c754d5d8 to your computer and use it in GitHub Desktop.
Save bhanuraja/1f19d7d924c12d59ee2ae2a3c754d5d8 to your computer and use it in GitHub Desktop.
Chegg question skipping android
package com.example.myapp;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity {
SiCookieStore2 siCookieStore ;
CookieManager cookieManager;//
public static final String MyPREFERENCES = "MyPrefs" ;
CountDownTimer countDownTimer;
WebView webView;
String questionID;
String choice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
siCookieStore = new SiCookieStore2(getApplicationContext());//Sicookie store cookies as persistant data
cookieManager = new CookieManager((CookieStore) siCookieStore, CookiePolicy.ACCEPT_ALL);//for retriving those cookies
CookieHandler.setDefault(cookieManager);
countDownTimer = new CountDownTimer(15000,1000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
}
};
webView = (WebView)findViewById(R.id.myview);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(true);
mytask mytask = new mytask();
mytask.execute();
}
public void Login() throws Exception{//Login in case cookies expired
SiCookieStore2 siCookieStore = new SiCookieStore2(getApplicationContext());
CookieManager cookieManager = new CookieManager((CookieStore) siCookieStore, CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
URL url = new URL("https://auth.chegg.com/_ajax/auth/v1/login?clientId=CHGG");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
String s ="clientId=CHGG&redirect_uri=&state=&responseType=&email=darshanpriya0%40gmail.com&password=Trivial!123&version=2.104.1&profileId=CHGG";
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept","application/json, text/javascript, */*; q=0.01");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
//send post request
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(s);
wr.flush();
wr.close();
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Login","done");
}
public String[] loadQuestion() throws Exception{//method for loadquestion this methods returns <div> element of question and question ID
URL url1 = new URL("https://www.chegg.com/homework-help/expertquestion");//questionID is used in skipping question
// HttpsURLConnection connection1 = (HttpsURLConnection) url1.openConnection();
HttpsURLConnection connection1 = (HttpsURLConnection) url1.openConnection();
connection1.setRequestMethod("GET");
// connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
BufferedReader in = new BufferedReader(
new InputStreamReader(connection1.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String answer = response.toString();
System.out.println(response.toString());
int start = answer.indexOf("<div class=\"question\">");
int end = answer.indexOf("<div class=\"skipLeaveErrorMsg\">");
int index = answer.indexOf("data-qid");
questionID =answer.substring(index+10,index+18);
answer = answer.substring(start,end);
// webView.loadData(answer,"text/html",null);
Log.d("Status", "Question had been loaded");
return new String[]{answer,questionID};
}
public void skipQuestion(String question) throws Exception{// Method for skipping question
URL url2 = new URL("https://www.chegg.com/study/_ajax/expertquestion");
HttpsURLConnection connection2 = (HttpsURLConnection)url2.openConnection();
String s1 ="questionId="+question+"&skipTime=452&skipReason=InsufficientKnowledge&skipReasonInfo=60&skipSource=skipGetNextQuestion&questionSkipSource=d";
connection2.setRequestMethod("POST");
connection2.setRequestProperty("Accept","application/json, text/javascript, */*; q=0.01");
connection2.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection2.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
//send post request
connection2.setDoOutput(true);
DataOutputStream wr2 = new DataOutputStream(connection2.getOutputStream());
wr2.writeBytes(s1);
wr2.flush();
wr2.close();
System.out.println(connection2.getResponseCode());
}
public void Start() throws Exception{
//checks for cookies
try {
if(!(getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE).getString("Login","none")=="done"))
Login();
} catch (Exception e) {
e.printStackTrace();
}
String s = loadQuestion()[1];
skipQuestion(s);
}
private class mytask extends AsyncTask{//this asynctask is to perform network operations on seperate thread
String myanswer;//Since performing on main thread creates an error.
@Override
protected Object doInBackground(Object[] objects) {
System.out.println("do in background executing");
try {
String s[]= loadQuestion();
myanswer = s[0];
if(choice=="skip")
skipQuestion(s[1]);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
webView.loadData(myanswer,"text/html",null);
}
}
public void checkCookies(CookieManager cookieManager){
// cookieManager.getCookieStore().get("htt")
}
}
package com.example.myapp;
import java.net.CookieStore;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
//import com.orb.common.Log;
/**
* Implementation of {@link CookieStore} for persistence cookies, uses shared
* preference for storing cookies.
*
* @author Manish
*
*/
public class PersistentCookieStore implements CookieStore {
private static final String LOG_TAG = "SICookieStore2";
private static final String COOKIE_PREFS = "com.example.myapp.cookieprefs";
private static final String COOKIE_DOMAINS_STORE = "com.example.myapp.CookieStore.domain";
private static final String COOKIE_DOMAIN_PREFIX = "com.example.myapp.CookieStore.domain_";
private static final String COOKIE_NAME_PREFIX = "com.example.myapp.CookieStore.cookie_";
/*This map here will store all domain to cookies bindings*/
private final CookieMap map;
private final SharedPreferences cookiePrefs;
/**
* Construct a persistent cookie store.
*
* @param context
* Context to attach cookie store to
*/
public PersistentCookieStore(Context context) {
cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0);
map = new CookieMap();
// Load any previously stored domains into the store
String storedCookieDomains = cookiePrefs.getString(COOKIE_DOMAINS_STORE, null);
if (storedCookieDomains != null) {
String[] storedCookieDomainsArray = TextUtils.split(storedCookieDomains, ",");
//split this domains and get cookie names stored for each domain
for (String domain : storedCookieDomainsArray) {
String storedCookiesNames = cookiePrefs.getString(COOKIE_DOMAIN_PREFIX + domain,
null);
//so now we have these cookie names
if (storedCookiesNames != null) {
//split these cookie names and get serialized cookie stored
String[] storedCookieNamesArray = TextUtils.split(storedCookiesNames, ",");
if (storedCookieNamesArray != null) {
//in this list we store all cookies under one URI
List<HttpCookie> cookies = new ArrayList<HttpCookie>();
for (String cookieName : storedCookieNamesArray) {
String encCookie = cookiePrefs.getString(COOKIE_NAME_PREFIX + domain
+ cookieName, null);
//now we deserialize or unserialize (whatever you call it) this cookie
//and get HttpCookie out of it and pass it to List
if (encCookie != null)
cookies.add(decodeCookie(encCookie));
}
map.put(URI.create(domain), cookies);
}
}
}
}
}
public synchronized void add(URI uri, HttpCookie cookie) {
if (cookie == null) {
throw new NullPointerException("cookie == null");
}
uri = cookiesUri(uri);
List<HttpCookie> cookies = map.get(uri);
if (cookies == null) {
cookies = new ArrayList<HttpCookie>();
map.put(uri, cookies);
} else {
cookies.remove(cookie);
}
cookies.add(cookie);
// Save cookie into persistent store
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.putString(COOKIE_DOMAINS_STORE, TextUtils.join(",", map.keySet()));
Set<String> names = new HashSet<String>();
for (HttpCookie cookie2 : cookies) {
names.add(cookie2.getName());
prefsWriter.putString(COOKIE_NAME_PREFIX + uri + cookie2.getName(),
encodeCookie(new SerializableCookie(cookie2)));
}
prefsWriter.putString(COOKIE_DOMAIN_PREFIX + uri, TextUtils.join(",", names));
prefsWriter.commit();
}
public synchronized List<HttpCookie> get(URI uri) {
if (uri == null) {
throw new NullPointerException("uri == null");
}
List<HttpCookie> result = new ArrayList<HttpCookie>();
// get cookies associated with given URI. If none, returns an empty list
List<HttpCookie> cookiesForUri = map.get(uri);
if (cookiesForUri != null) {
for (Iterator<HttpCookie> i = cookiesForUri.iterator(); i.hasNext();) {
HttpCookie cookie = i.next();
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else {
result.add(cookie);
}
}
}
// get all cookies that domain matches the URI
for (Map.Entry<URI, List<HttpCookie>> entry : map.entrySet()) {
if (uri.equals(entry.getKey())) {
continue; // skip the given URI; we've already handled it
}
List<HttpCookie> entryCookies = entry.getValue();
for (Iterator<HttpCookie> i = entryCookies.iterator(); i.hasNext();) {
HttpCookie cookie = i.next();
if (!HttpCookie.domainMatches(cookie.getDomain(), uri.getHost())) {
continue;
}
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else if (!result.contains(cookie)) {
result.add(cookie);
}
}
}
return Collections.unmodifiableList(result);
}
public synchronized List<HttpCookie> getCookies() {
List<HttpCookie> result = new ArrayList<HttpCookie>();
for (List<HttpCookie> list : map.values()) {
for (Iterator<HttpCookie> i = list.iterator(); i.hasNext();) {
HttpCookie cookie = i.next();
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else if (!result.contains(cookie)) {
result.add(cookie);
}
}
}
return Collections.unmodifiableList(result);
}
public synchronized List<URI> getURIs() {
List<URI> result = new ArrayList<URI>(map.getAllURIs());
result.remove(null); // sigh
return Collections.unmodifiableList(result);
}
public synchronized boolean remove(URI uri, HttpCookie cookie) {
if (cookie == null) {
throw new NullPointerException("cookie == null");
}
if (map.removeCookie(uri, cookie)) {
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.putString(COOKIE_DOMAIN_PREFIX + uri,
TextUtils.join(",", map.getAllCookieNames(uri)));
prefsWriter.remove(COOKIE_NAME_PREFIX + uri + cookie.getName());
prefsWriter.apply();
return true;
}
return false;
}
public synchronized boolean removeAll() {
// Clear cookies from persistent store
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.clear();
prefsWriter.commit();
// Clear cookies from local store
boolean result = !map.isEmpty();
map.clear();
return result;
}
/**
* Serializes HttpCookie object into String
*
* @param cookie
* cookie to be encoded, can be null
* @return cookie encoded as String
*/
protected String encodeCookie(SerializableCookie cookie) {
if (cookie == null)
return null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ObjectOutputStream outputStream = new ObjectOutputStream(os);
outputStream.writeObject(cookie);
} catch (IOException e) {
Log.e(LOG_TAG, "IOException in encodeCookie", e);
return null;
}
return byteArrayToHexString(os.toByteArray());
}
/**
* Returns HttpCookie decoded from cookie string
*
* @param cookieString
* string of cookie as returned from http request
* @return decoded cookie or null if exception occured
*/
protected HttpCookie decodeCookie(String cookieString) {
byte[] bytes = hexStringToByteArray(cookieString);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
HttpCookie cookie = null;
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
cookie = ((SerializableCookie) objectInputStream.readObject()).getCookie();
} catch (IOException e) {
Log.e(LOG_TAG, "IOException in decodeCookie", e);
} catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
}
return cookie;
}
/**
* Using some super basic byte array &lt;-&gt; hex conversions so we don't
* have to rely on any large Base64 libraries. Can be overridden if you
* like!
*
* @param bytes
* byte array to be converted
* @return string containing hex values
*/
protected String byteArrayToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte element : bytes) {
int v = element & 0xff;
if (v < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString().toUpperCase(Locale.US);
}
/**
* Converts hex values from strings to byte arra
*
* @param hexString
* string of hex-encoded values
* @return decoded byte array
*/
protected byte[] hexStringToByteArray(String hexString) {
int len = hexString.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
.digit(hexString.charAt(i + 1), 16));
}
return data;
}
/**
* Utility function to male sure that every time you get consistent URI
* @param uri
* @return
*/
private URI cookiesUri(URI uri) {
if (uri == null) {
return null;
}
try {
return new URI(uri.getScheme(), uri.getHost(), null, null);
} catch (URISyntaxException e) {
return uri;
}
}
/**
* A implementation of {@link Map} for utility class for storing URL cookie map
* @author Manish
*
*/
private class CookieMap implements Map<URI, List<HttpCookie>> {
private final Map<URI, List<HttpCookie>> map;
/**
*
*/
public CookieMap() {
map = new HashMap<URI, List<HttpCookie>>();
}
/*
* (non-Javadoc)
*
* @see java.util.Map#clear()
*/
@Override
public void clear() {
map.clear();
}
/*
* (non-Javadoc)
*
* @see java.util.Map#containsKey(java.lang.Object)
*/
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
/*
* (non-Javadoc)
*
* @see java.util.Map#containsValue(java.lang.Object)
*/
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
/*
* (non-Javadoc)
*
* @see java.util.Map#entrySet()
*/
@Override
public Set<java.util.Map.Entry<URI, List<HttpCookie>>> entrySet() {
return map.entrySet();
}
/*
* (non-Javadoc)
*
* @see java.util.Map#get(java.lang.Object)
*/
@Override
public List<HttpCookie> get(Object key) {
return map.get(key);
}
/*
* (non-Javadoc)
*
* @see java.util.Map#isEmpty()
*/
@Override
public boolean isEmpty() {
return map.isEmpty();
}
/*
* (non-Javadoc)
*
* @see java.util.Map#keySet()
*/
@Override
public Set<URI> keySet() {
return map.keySet();
}
/*
* (non-Javadoc)
*
* @see java.util.Map#put(java.lang.Object, java.lang.Object)
*/
@Override
public List<HttpCookie> put(URI key, List<HttpCookie> value) {
return map.put(key, value);
}
/*
* (non-Javadoc)
*
* @see java.util.Map#putAll(java.util.Map)
*/
@Override
public void putAll(Map<? extends URI, ? extends List<HttpCookie>> map) {
this.map.putAll(map);
}
/*
* (non-Javadoc)
*
* @see java.util.Map#remove(java.lang.Object)
*/
@Override
public List<HttpCookie> remove(Object key) {
return map.remove(key);
}
/*
* (non-Javadoc)
*
* @see java.util.Map#size()
*/
@Override
public int size() {
return map.size();
}
/*
* (non-Javadoc)
*
* @see java.util.Map#values()
*/
@Override
public Collection<List<HttpCookie>> values() {
return map.values();
}
/**
* List all URIs for which cookies are stored in map
* @return
*/
public Collection<URI> getAllURIs() {
return map.keySet();
}
/**
* Get all cookies names stored for given URI
* @param uri
* @return
*/
public Collection<String> getAllCookieNames(URI uri) {
List<HttpCookie> cookies = map.get(uri);
Set<String> cookieNames = new HashSet<String>();
for (HttpCookie cookie : cookies) {
cookieNames.add(cookie.getName());
}
return cookieNames;
}
/**
* Removes requested {@link HttpCookie} {@code httpCookie} from given {@code uri} value
* @param uri
* @param httpCookie
* @return
*/
public boolean removeCookie(URI uri, HttpCookie httpCookie) {
if (map.containsKey(uri)) {
return map.get(uri).remove(httpCookie);
} else {
return false;
}
}
}
}
package com.example.myapp;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.HttpCookie;
/**
* A simple wrapper for {@link HttpCookie} to work with {@link PersistentCookieStore}
* Gives power of serialization-deserialization to {@link HttpCookie}
* @author Manish
*
*/
// taken from https://github.com/android-async-http/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/SerializableCookie.java
public class SerializableCookie implements Serializable {
private HttpCookie mHttpCookie;
/**
*
*/
private static final long serialVersionUID = 2532101328282342578L;
/**
*
*/
public SerializableCookie(HttpCookie cookie) {
this.mHttpCookie = cookie;
}
public HttpCookie getCookie() {
return mHttpCookie;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(mHttpCookie.getName());
out.writeObject(mHttpCookie.getValue());
out.writeObject(mHttpCookie.getComment());
out.writeObject(mHttpCookie.getCommentURL());
out.writeBoolean(mHttpCookie.getDiscard());
out.writeObject(mHttpCookie.getDomain());
out.writeLong(mHttpCookie.getMaxAge());
out.writeObject(mHttpCookie.getPath());
out.writeObject(mHttpCookie.getPortlist());
out.writeBoolean(mHttpCookie.getSecure());
out.writeInt(mHttpCookie.getVersion());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
mHttpCookie = new HttpCookie(name, value);
mHttpCookie.setComment((String) in.readObject());
mHttpCookie.setCommentURL((String) in.readObject());
mHttpCookie.setDiscard(in.readBoolean());
mHttpCookie.setDomain((String) in.readObject());
mHttpCookie.setMaxAge(in.readLong());
mHttpCookie.setPath((String) in.readObject());
mHttpCookie.setPortlist((String) in.readObject());
mHttpCookie.setSecure(in.readBoolean());
mHttpCookie.setVersion(in.readInt());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment