-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsaver.py
More file actions
355 lines (327 loc) · 15.4 KB
/
saver.py
File metadata and controls
355 lines (327 loc) · 15.4 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import json, pytz, sys, traceback, time, requests, random
_print = print
from rich import print
from tzlocal import get_localzone
from datetime import datetime, timezone
from utils import get_duo_info, get_headers, clear, current_time
VERSION = "v0.1.2 Beta"
TIMEZONE = str(get_localzone())
with open("config.json", "r") as f:
config: dict = json.load(f)
DEBUG = config['debug']
title_string = f'\n [bold][bright_green]Duo[/][bright_blue]KLI[/] [bright_green]Saver[/] [white]{VERSION}[/]{" [magenta][Debug Mode Enabled][/]" if DEBUG else ""}[/]'
def farm_xp(account, amount):
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Starting to farm {amount} XP for {config['accounts'][account]['username']}")
original_amount = amount
token = config['accounts'][account]['token']
headers = {
"authorization": f"Bearer {token}",
"cookie": f"jwt_token={token}",
"connection": "Keep-Alive",
"content-type": "application/json",
"user-agent": "DuoKLI/1.0",
"device-platform": "web",
"x-duolingo-device-platform": "web",
"x-duolingo-app-version": "1.0.0",
"x-duolingo-application": "chrome",
"x-duolingo-client-version": "web",
"accept": "application/json"
}
while True:
now_ts = int(datetime.now(timezone.utc).timestamp())
duration = random.randint(300, 420)
base_xp = 30
max_happy_hour = 469
happy_hour_bonus = min(max_happy_hour, amount - base_xp) if amount > base_xp else 0
dataget = {
"awardXp": True,
"completedBonusChallenge": True,
"fromLanguage": "en",
"hasXpBoost": False,
"illustrationFormat": "svg",
"isFeaturedStoryInPracticeHub": True,
"isLegendaryMode": True,
"isV2Redo": False,
"isV2Story": False,
"learningLanguage": "fr",
"masterVersion": True,
"maxScore": 0,
"score": 0,
"happyHourBonusXp": happy_hour_bonus,
"startTime": now_ts,
"endTime": now_ts + duration,
}
response = requests.post('https://stories.duolingo.com/api2/stories/fr-en-le-passeport/complete', headers=headers, json=dataget, timeout=10)
if response.status_code == 200:
response_data = response.json()
amount -= response_data.get('awardedXp', 0)
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Farmed {response_data.get('awardedXp', 0)} XP")
else:
if DEBUG:
_print("\a", end="")
print(
f"{current_time()} [bold magenta][DEBUG][/] Failed to farm {base_xp + happy_hour_bonus} XP\n"
f"{current_time()} [bold magenta][DEBUG][/] Status code: {response.status_code}\n"
f"{current_time()} [bold magenta][DEBUG][/] Content: {response.text}"
)
if amount <= 0:
break
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Finished farming {original_amount - amount} XP for {config['accounts'][account]['username']}")
def leaderboard_registration(account):
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Attempting to enter a leaderboard for {config['accounts'][account]['username']}")
duo_id = int(config['accounts'][account]['id'])
headers = get_headers(account)
url = f"https://www.duolingo.com/2017-06-30/users/{duo_id}/privacy-settings"
response = requests.get(url, headers=headers, timeout=10)
if response.status_code != 200:
print(f"{current_time()} [red]Failed to get privacy settings.[/]")
if DEBUG:
_print("\a", end="")
print(
f"{current_time()} [bold magenta][DEBUG][/] Status code: {response.status_code}\n"
f"{current_time()} [bold magenta][DEBUG][/] Content: {response.text}"
)
return
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Fetched privacy settings")
data = response.json()
privacy_settings = data.get('privacySettings', [])
social_setting = next((setting for setting in privacy_settings if setting['id'] == 'disable_social'), None)
was_private = social_setting['enabled'] if social_setting else False
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] {was_private = }")
if was_private:
url = f"https://www.duolingo.com/2017-06-30/users/{duo_id}/privacy-settings?fields=privacySettings"
payload = {"DISABLE_SOCIAL": False}
response = requests.patch(url, headers=headers, json=payload, timeout=10)
if response.status_code != 200:
print(f"{current_time()} [red]Failed to set profile to public.[/]")
if DEBUG:
_print("\a", end="")
print(
f"{current_time()} [bold magenta][DEBUG][/] Status code: {response.status_code}\n"
f"{current_time()} [bold magenta][DEBUG][/] Content: {response.text}"
)
return
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Set profile to public")
time.sleep(2)
farm_xp(account, 30)
if was_private:
url = f"https://www.duolingo.com/2017-06-30/users/{duo_id}/privacy-settings?fields=privacySettings"
payload = {"DISABLE_SOCIAL": True}
response = requests.patch(url, headers=headers, json=payload, timeout=10)
if response.status_code != 200:
print(f"{current_time()} [red]Failed to restore privacy settings.[/]")
if DEBUG:
_print("\a", end="")
print(
f"{current_time()} [bold magenta][DEBUG][/] Status code: {response.status_code}\n"
f"{current_time()} [bold magenta][DEBUG][/] Content: {response.text}"
)
return
def save_streak(account):
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Checking streak for {config['accounts'][account]['username']}")
duo_info = get_duo_info(account, DEBUG)
headers = get_headers(account)
user_tz = pytz.timezone(TIMEZONE)
now = datetime.now(user_tz)
streak_data = duo_info.get('streakData', {})
current_streak = streak_data.get('currentStreak', {})
should_do_lesson = True
if current_streak:
last_extended = current_streak.get('lastExtendedDate')
if last_extended:
last_extended = datetime.strptime(last_extended, "%Y-%m-%d")
last_extended = user_tz.localize(last_extended)
should_do_lesson = last_extended.date() < now.date()
if not should_do_lesson:
return
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Attempting to save streak")
fromLanguage = duo_info.get('fromLanguage', 'Unknown')
learningLanguage = duo_info.get('learningLanguage', 'Unknown')
session_payload = {
"challengeTypes": [
"assist", "characterIntro", "characterMatch", "characterPuzzle",
"characterSelect", "characterTrace", "characterWrite",
"completeReverseTranslation", "definition", "dialogue",
"extendedMatch", "extendedListenMatch", "form", "freeResponse",
"gapFill", "judge", "listen", "listenComplete", "listenMatch",
"match", "name", "listenComprehension", "listenIsolation",
"listenSpeak", "listenTap", "orderTapComplete", "partialListen",
"partialReverseTranslate", "patternTapComplete", "radioBinary",
"radioImageSelect", "radioListenMatch", "radioListenRecognize",
"radioSelect", "readComprehension", "reverseAssist",
"sameDifferent", "select", "selectPronunciation",
"selectTranscription", "svgPuzzle", "syllableTap",
"syllableListenTap", "speak", "tapCloze", "tapClozeTable",
"tapComplete", "tapCompleteTable", "tapDescribe", "translate",
"transliterate", "transliterationAssist", "typeCloze",
"typeClozeTable", "typeComplete", "typeCompleteTable",
"writeComprehension"
],
"fromLanguage": fromLanguage,
"isFinalLevel": False,
"isV2": True,
"juicy": True,
"learningLanguage": learningLanguage,
"smartTipsVersion": 2,
"type": "GLOBAL_PRACTICE"
}
session_url = "https://www.duolingo.com/2017-06-30/sessions"
response = requests.post(session_url, headers=headers, json=session_payload, timeout=10)
if response.status_code == 200:
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Created session")
session_data = response.json()
else:
print(f"{current_time()} [red]An error has occurred while trying to create a session.[/]")
if DEBUG:
_print("\a", end="")
print(
f"{current_time()} [bold magenta][DEBUG][/] Status code: {response.status_code}\n"
f"{current_time()} [bold magenta][DEBUG][/] Content: {response.text}"
)
return
if 'id' not in session_data:
print(f"{current_time()} [red]Session ID not found in response data.[/]")
if DEBUG:
_print("\a", end="")
return
start_time = now.timestamp()
end_time = datetime.now(user_tz).timestamp()
update_payload = {
**session_data,
"heartsLeft": 5,
"startTime": start_time,
"endTime": end_time,
"enableBonusPoints": False,
"failed": False,
"maxInLessonStreak": 9,
"shouldLearnThings": True
}
update_url = f"https://www.duolingo.com/2017-06-30/sessions/{session_data['id']}"
response = requests.put(update_url, headers=headers, json=update_payload, timeout=10)
if response.status_code == 200:
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Updated session")
update_data = response.json()
if update_data.get('xpGain') is not None:
print(f"{current_time()} [green]Saved streak![/]")
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] {update_data.get('xpGain') = }")
else:
print(f"{current_time()} [red]Failed to save streak.[/]")
if DEBUG:
_print("\a", end="")
else:
print(f"{current_time()} [red]Failed to update session to save streak.[/]")
if DEBUG:
_print("\a", end="")
print(
f"{current_time()} [bold magenta][DEBUG][/] Status code: {response.status_code}\n"
f"{current_time()} [bold magenta][DEBUG][/] Content: {response.text}"
)
def save_league(account, position):
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Checking league position for {config['accounts'][account]['username']}")
headers = get_headers(account)
duo_id = int(config['accounts'][account]['id'])
url = (f"https://duolingo-leaderboards-prod.duolingo.com/leaderboards/7d9f5dd1-8423-491a-91f2-2532052038ce/users/{duo_id}"
f"?client_unlocked=true&get_reactions=true&_={int(time.time() * 1000)}")
while True:
response = requests.get(url, headers=headers, timeout=10)
if response.status_code != 200:
if DEBUG:
_print("\a", end="")
print(
f"{current_time()} [bold magenta][DEBUG][/] Failed to fetch user data on leaderboard\n"
f"{current_time()} [bold magenta][DEBUG][/] Status code: {response.status_code}\n"
f"{current_time()} [bold magenta][DEBUG][/] Content: {response.text}"
)
leaderboard_registration(account)
return
leaderboard_data = response.json()
if not leaderboard_data or 'active' not in leaderboard_data:
leaderboard_registration(account)
return
active_data = leaderboard_data.get('active', None)
if active_data is None or 'cohort' not in active_data:
leaderboard_registration(account)
return
cohort_data = active_data.get('cohort', {})
rankings = cohort_data.get('rankings', [])
current_user = next((user_data for user_data in rankings if user_data['user_id'] == duo_id), None)
if current_user is None:
leaderboard_registration(account)
return
current_score = current_user['score']
current_rank = next((index + 1 for index, user_data in enumerate(rankings) if user_data['user_id'] == duo_id), None)
if DEBUG:
print(
f"{current_time()} [bold magenta][DEBUG][/] {current_score = }\n"
f"{current_time()} [bold magenta][DEBUG][/] {current_rank = }"
)
if current_rank is not None and current_rank <= position:
break
target_user = rankings[position - 1] if position and position - 1 < len(rankings) else None
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] {target_user = }")
if target_user is None:
break
target_score = target_user['score']
xp_needed = (target_score - current_score) + 60
if DEBUG:
print(
f"{current_time()} [bold magenta][DEBUG][/] {target_score = }\n"
f"{current_time()} [bold magenta][DEBUG][/] {xp_needed = }"
)
if xp_needed > 0:
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] Attempting to save league position")
farm_xp(account, xp_needed)
print(f"{current_time()} [green]Saved league position![/]")
try:
clear()
print(title_string)
print("\n[yellow] Press Ctrl+C to stop the saver.[/]\n")
print("[blue] Starting saver...[/]", end="")
_print("\r", end="")
if not any(acc['autostreak'] or acc['autoleague']['active'] for acc in config['accounts']):
print("[red] There are no accounts with a saver feature enabled![/]\n")
sys.exit()
while True:
for account in range(len(config['accounts'])):
autostreak = config['accounts'][account]['autostreak']
autoleague = config['accounts'][account]['autoleague']['active']
league_pos = config['accounts'][account]['autoleague']['position']
delay = config['delay']
if autostreak or autoleague:
if DEBUG:
print(f"{current_time()} [bold magenta][DEBUG][/] ----------------------------------------")
print(f"{current_time()} [blue]Checking [bold]{config['accounts'][account]['username']}[/] ...[/]")
try:
save_streak(account) if autostreak else None
save_league(account, league_pos) if autoleague and league_pos else None
except Exception as e:
_print("\a", end="")
print(f"[red][bold]An unexpected error occurred while trying to save {config['accounts'][account]['username']}: {e}[/]\nDetailed error:[/]")
traceback.print_exc()
print(f"{current_time()} [blue]Waiting {delay} seconds...[/]")
time.sleep(delay)
except KeyboardInterrupt:
_print("\r\033[2K", end="")
print("\n [bright_red]Stopping saver...[/]\n")
sys.exit()
except Exception as e:
_print("\a", end="")
print(f"[red][bold]An unexpected error occurred: {e}[/]\nDetailed error:[/]")
traceback.print_exc()
print("\n [bright_red]Stopping saver...[/]\n")
sys.exit()