#############################################################
# Author: Nill/r00t3r                                       #
# Target: https://www.pariksha.co/                          #
# Created: Fri Sep 2, 14:03:25                              #
#############################################################
#
#	This script is just a proof of concept so only 
#	supports automating one test at a time for which
#	the test id (Lo_id) and category id is hardcoded.
#
#	run script two times for a test to complete the test
#
#!/usr/bin/python

import requests, pprint, json, sys

ppid = int(sys.argv[1])
pid = int(sys.argv[2])
cid = int(sys.argv[3])

# make a request to get next question
url = 'https://practice.pariksha.co/questions/next'

# first probe for next question
r = requests.options(url)
#print r.status_code

# we havent answered any question yet so Qid will be -1 ;)
json_data = { 
				# TO-DO change the Lo_id after 30 questions
				"Lo_id":cid,
				"Qid":-1,
				"Userid":<your userid>, # not your username!, your userid as an integer 
				"Language":"en",
				"Correctness":0,
				"Timespent":0,
				"Exam":10001,
				"Marked_Answer":[0],
				"Marked_Answer_Matrix":[],
				"Lo_status":0,
				"Qcount":30,
				"Parents":[pid,ppid,54498,10001],
				"Dep_Lo":[]
				}

headers = {
				'Content-type': 'application/json;charset=utf-8',
				'Accept': 'application/json, text/plain, */*',
				'Accept-Language': 'en-US,en;q=0.5',
				'Accept-Encoding': 'gzip, deflate',
				'Referer': 'https://www.pariksha.co/students/index/chameleon/' + str(ppid) + '/' + str(pid) +'/',
				'origin': 'https://www.pariksha.co',
				'Connection': 'close'
				}

# change the cookies for each logged on session ;)
cookie = {
			'_ga': 'GA1.2.556712606.1472438710',
			'par_practice_tkn': '<your session token>',
			'_gat': '1'
			}				

# get the response from the server
r = requests.post(url, data=json.dumps(json_data), headers=headers, cookies=cookie)		
binary = r.content
output = json.loads(binary)

# change referer for the upcoming requests
addreferer = str(json_data["Lo_id"]) + '/'
headers['Referer'] += addreferer

# extract the answer for the next question and the next question id
next_answer = output['Question']['ans']
next_qid = output['Question']['inf']['qid']

for i in range(1,11):
	
	# set the json_data to answer the incoming question 
	json_data["Qid"] = next_qid
	json_data["Correctness"] = 1
	
	# we dont want server to suspect as a robot so timespent will not be zero lol	
	json_data["Timespent"] = 1
	json_data["Marked_Answer"] = [next_answer]

	# get the response from the server
	r = requests.post(url, data=json.dumps(json_data), headers=headers, cookies=cookie)		
	binary = r.content
	output = json.loads(binary)
	# extract the answer for the next question and the next question id
	next_answer = output['Question']['ans']
	next_qid = output['Question']['inf']['qid']


# now we need to finish the game to get XPs 
url = 'https://practice.pariksha.co/game/end'

headers = {
				'Content-type': 'application/json;charset=utf-8',
				'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
				'Accept-Language': 'en-US,en;q=0.5',
				'Accept-Encoding': 'gzip, deflate',
				'Access-Control-Request-Method': 'POST',
				'Access-Control-Request-Headers': 'content-type',
				'origin': 'https://www.pariksha.co',
				'Connection': 'close'
				}	

# first probe request to end the game
r = requests.options(url)

headers = {
				'Content-type': 'application/json;charset=utf-8',
				'Accept': 'application/json, text/plain, */*',
				'Accept-Language': 'en-US,en;q=0.5',
				'Accept-Encoding': 'gzip, deflate',
				'Referer': 'https://www.pariksha.co/students/index/chameleon/' + str(ppid) + '/' + str(pid) + '/',
				'origin': 'https://www.pariksha.co',
				'Connection': 'close'
				}

headers['Referer'] += addreferer

json_data_end = {
					"Userid":<your userid>,
					"Exam":10001,
					"IsMastered":True # doesn't matter anymore 
					}

# end the boring game lol
r = requests.post(url, data=json.dumps(json_data_end), headers=headers, cookies=cookie)		
binary = r.content
output = json.loads(binary)	

# print the new total XPs
print "new total XPs: " + str(output['Xp'])		
