Created
May 31, 2019 11:40
-
-
Save bhanuraja/1f19d7d924c12d59ee2ae2a3c754d5d8 to your computer and use it in GitHub Desktop.
Chegg question skipping android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment