-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduolingo_client.py
More file actions
executable file
·147 lines (124 loc) · 4.41 KB
/
duolingo_client.py
File metadata and controls
executable file
·147 lines (124 loc) · 4.41 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3
import json
import os.path
import requests
import textwrap
BASE_URL = 'https://www.duolingo.com'
LOGIN_URL = BASE_URL + '/login'
URL = BASE_URL + '/2017-06-30/users/%s?fields=currentCourse,xpGains'
SESSION_URL = BASE_URL + '/2017-06-30/sessions'
def get_password():
with open(os.path.expanduser('~/.duolingo-password')) as f:
return f.read().strip()
class Duo:
LESSONS = [1, 1, 2, 3, 5]
LESSONS_TOTAL = []
def __init__(self, username, password=get_password()):
self.LESSONS_TOTAL.append(0)
i = 0
for _ in self.LESSONS:
self.LESSONS_TOTAL.append(self.LESSONS_TOTAL[i] + self.LESSONS[i])
i += 1
login_response = requests.post(LOGIN_URL, data={'login': username, 'password': password})
self._token = login_response.headers['jwt']
user_id = json.loads(login_response.content.decode("utf-8"))['user_id']
url = URL % user_id
self._data = json.loads(requests.get(url, headers=self._get_headers()).content.decode("utf-8"))
self.skills = []
for row in self._data['currentCourse']['skills']:
for skill in row:
if skill.get('accessible', False):
self.skills.append(skill)
def _get_headers(self):
return {'authorization': 'Bearer %s' % self._token}
def get_data(self):
return self._data
def get_skills(self):
return self.skills
def get_xp_gains(self):
return self._data['xpGains']
def print_topics(self):
course_total = 0
course_finished = 0
for skill in self.skills:
name = skill['name']
strength = int(skill['strength'] * 100)
levels = skill['levels']
lessons = skill['lessons']
finished_levels = skill['finishedLevels']
finished_lessons = skill['finishedLessons']
if finished_levels == levels:
skill_total = finished_lessons
skill_finished = finished_lessons
else:
base_lessons = lessons / self.LESSONS[finished_levels]
skill_total = base_lessons * self.LESSONS_TOTAL[levels]
skill_finished = self.LESSONS_TOTAL[finished_levels] * base_lessons + finished_lessons
course_total += skill_total
course_finished += skill_finished
print('%-40s %3d/%3d (%d) @ %d' % (name, skill_finished, skill_total, skill_total - skill_finished,
strength))
print('%-40s %3d/%3d (%d)' % ('Total', course_finished, course_total, course_total - course_finished))
def get_tips(self):
i = 0
for skill in self.skills:
explanation = skill.get('explanation', None)
if explanation:
i += 1
title = explanation['title']
print('Fetching %s' % title)
filename = '%02d-%s.txt' % (i, title.replace(' ', '-'))
url = explanation['url']
tips = json.loads(requests.get(url).content)
elements = tips['elements']
with open(filename, 'w') as f:
first = True
for element in elements:
if element['type'] == 'text':
styled_string = element['element']['styledString']
text = styled_string['text']
styling = styled_string['styling']
if not first and styling[0]['to'] == len(text):
f.write('\n')
f.write('%s\n' % textwrap.fill(text.encode('utf8')))
first = False
def get_session(self):
data = {
"fromLanguage": "en",
"learningLanguage": "es",
"challengeTypes": [
"characterIntro",
"characterMatch",
"characterSelect",
"dialogue",
"form",
"gapFill",
# "judge",
# "listen",
"name",
"listenComprehension",
"listenTap",
"readComprehension",
"select",
"selectPronunciation",
"selectRecording",
"selectTranscription",
"tapCloze",
"translate",
],
"type": "GLOBAL_PRACTICE",
"juicy": False
}
content = requests.post(SESSION_URL, headers=self._get_headers(), data=json.dumps(data)).content.decode("utf-8")
session = json.loads(content)
challenges = session['challenges']
for i, challenge in enumerate(challenges):
print('%d: %s from %s' % (i, challenge['type'], challenge['sourceLanguage']))
# print(json.dumps(challenges[0], indent=2))
if __name__ == '__main__':
duo = Duo('AlonAlbert')
# duo.get_tips()
print(len(duo.get_skills()))
# print(json.dumps(duo.get_data(), indent=2))
duo.get_session()
# duo.print_topics()