/
duolingo.py
93 lines (79 loc) · 3.33 KB
/
duolingo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
import json
import requests
# import duolingo
def post_answer(answer, element):
post_data = {
"session_element":{
"sentence": element["sentence"],
"solution_key": element["solution_key"],
"source_language":"eo",
"target_language_name":"English",
"wrong_tokens":"null",
"unknown_words":[ ],
"activity_uuid": element["activity_uuid"],
"specific_type": element["specific_type"],
"UNKNOWN":"false",
"generator_id": element["generator_id"],
"text": element["sentence"],
"type":"translate",
"new_explanation_ids":[],
"teaches_lexeme_ids":[
element["teaches_lexeme_ids"]
],
"tokens":"null",
"sentence_id":element["sentence_id"],
"translation_key":"null",
"known":"true",
"tts_id": element["tts_id"],
"has_tts":"true",
"has_accents":"false",
"context":"",
"highlight":[
],
"knowledge_components":[
"6bdf85dbf40e09338dd51a301633734e"
],
"explanations":{
},
"target_language":"en",
"hovered_words":[],
"hovered_lexeme_ids":{
}
},
"value":answer,
"use_report_messages":"true",
"time_taken":"13508",
"learning_language":"eo",
"session_type":"practice",
"from_language":"en",
"type":"translate",
"solution_key": element["solution_key"]
}
r = requests.post("https://www.duolingo.com/session_element_solutions/practice/translate", data = post_data, headers = get_header)
print r.request.headers
get_header = {
"host": "www.duolingo.com",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
"Accept": 'application/json, text/javascript, */*; q=0.01',
"Accept-Language": 'en-US,en;q=0.5',
"Accept-Encoding": 'gzip, deflate',
"DNT": '1',
"X-Requested-With": 'XMLHttpRequest',
"Referer": 'https://www.duolingo.com/practice',
"Cookie": 'lang=en; duo_ab="7cfeab6c3812b8936973933a88b058db12969d02eyJjb3Vyc2VfdGl0bGVfZXhwZXJpbWVudCI6IGZhbHNlLCAic2Nob29sc19zcGxhc2hfZXhwZXJpbWVudCI6ICJjb250cm9sIiwgImNvdXJzZV9idXR0b25fZXhwZXJpbWVudDIiOiB0cnVlLCAic2lnbnVwX21vZGFsX2J1dHRvbnNfZXhwZXJpbWVudCI6IGZhbHNlLCAibG9naW5fbW9kYWxfZXhwZXJpbWVudCI6IGZhbHNlfQ\075\075"; wuuid=02814935-0141-43fa-9a19-ee32895f5158; auth_tkt="417c3876268c646aef62e1ea1491009555fd97e43111651!userid_type:int"',
"Connection": 'keep-alive'
}
url = "https://www.duolingo.com/sessions/588730680758?language=eo&web_speech_enabled=false&kc_strength_model_version=3&type=global_practice&speak_capable=true&_=1442691915911"
r = requests.get(url, headers = get_header)
r_json = json.loads(r.text)
# print (r_json)
for session_element in range (0, len(r_json['session_elements'])):
element = r_json["session_elements"][session_element]
if element["type"] == "translate":
print "Please translate: " + element["sentence"]
return_sentence = raw_input()
post_answer(return_sentence, element)
else:
print "Skipping non-translation problem."
print "The quiz completed successfully."