forked from DevilXD/TwitchDropsMiner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate.py
488 lines (431 loc) · 14.7 KB
/
translate.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
from __future__ import annotations
from collections import abc
from typing import Any, TypedDict, TYPE_CHECKING
from exceptions import MinerException
from utils import json_load, json_save
from constants import IS_PACKAGED, LANG_PATH, DEFAULT_LANG
if TYPE_CHECKING:
from typing_extensions import NotRequired
class StatusMessages(TypedDict):
terminated: str
watching: str
goes_online: str
goes_offline: str
claimed_drop: str
claimed_points: str
earned_points: str
no_channel: str
no_campaign: str
class ChromeMessages(TypedDict):
startup: str
login_to_complete: str
no_token: str
closed_window: str
class LoginMessages(TypedDict):
chrome: ChromeMessages
error_code: str
unexpected_content: str
email_code_required: str
twofa_code_required: str
incorrect_login_pass: str
incorrect_email_code: str
incorrect_twofa_code: str
class ErrorMessages(TypedDict):
captcha: str
no_connection: str
site_down: str
class GUIStatus(TypedDict):
name: str
idle: str
exiting: str
terminated: str
cleanup: str
gathering: str
switching: str
fetching_inventory: str
fetching_campaigns: str
adding_campaigns: str
class GUITabs(TypedDict):
main: str
inventory: str
settings: str
help: str
class GUITray(TypedDict):
notification_title: str
minimize: str
show: str
quit: str
class GUILoginForm(TypedDict):
name: str
labels: str
logging_in: str
logged_in: str
logged_out: str
request: str
required: str
username: str
password: str
twofa_code: str
button: str
class GUIWebsocket(TypedDict):
name: str
websocket: str
initializing: str
connected: str
disconnected: str
connecting: str
disconnecting: str
reconnecting: str
class GUIProgress(TypedDict):
name: str
drop: str
game: str
campaign: str
remaining: str
drop_progress: str
campaign_progress: str
class GUIChannelHeadings(TypedDict):
channel: str
status: str
game: str
points: str
viewers: str
class GUIChannels(TypedDict):
name: str
switch: str
load_points: str
online: str
pending: str
offline: str
headings: GUIChannelHeadings
class GUIInvFilter(TypedDict):
name: str
show: str
not_linked: str
upcoming: str
expired: str
excluded: str
finished: str
refresh: str
class GUIInvStatus(TypedDict):
linked: str
not_linked: str
active: str
expired: str
upcoming: str
claimed: str
ready_to_claim: str
class GUIInventory(TypedDict):
filter: GUIInvFilter
status: GUIInvStatus
starts: str
ends: str
allowed_channels: str
all_channels: str
and_more: str
percent_progress: str
minutes_progress: str
class GUISettingsGeneral(TypedDict):
name: str
autostart: str
tray: str
tray_notifications: str
priority_mode: str
unlinked_campaigns: str
proxy: str
class GUIPriorityModes(TypedDict):
priority_only: str
ending_soonest: str
low_availability: str
class GUISettings(TypedDict):
general: GUISettingsGeneral
priority_modes: GUIPriorityModes
game_name: str
priority: str
exclude: str
reload: str
reload_text: str
class GUIHelpLinks(TypedDict):
name: str
inventory: str
campaigns: str
class GUIHelp(TypedDict):
links: GUIHelpLinks
how_it_works: str
how_it_works_text: str
getting_started: str
getting_started_text: str
class GUIMessages(TypedDict):
output: str
status: GUIStatus
tabs: GUITabs
tray: GUITray
login: GUILoginForm
websocket: GUIWebsocket
progress: GUIProgress
channels: GUIChannels
inventory: GUIInventory
settings: GUISettings
help: GUIHelp
class Translation(TypedDict):
language_name: NotRequired[str]
english_name: str
status: StatusMessages
login: LoginMessages
error: ErrorMessages
gui: GUIMessages
default_translation: Translation = {
"english_name": "English",
"status": {
"terminated": "\nApplication Terminated.\nClose the window to exit the application.",
"watching": "Watching: {channel}",
"goes_online": "{channel} goes ONLINE, switching...",
"goes_offline": "{channel} goes OFFLINE, switching...",
"claimed_drop": "Claimed drop: {drop}",
"claimed_points": "Claimed bonus points: {points}",
"earned_points": "Earned points for watching: {points}, total: {balance}",
"no_channel": "No available channels to watch. Waiting for an ONLINE channel...",
"no_campaign": "No active campaigns to mine drops for. Waiting for an active campaign...",
},
"login": {
"unexpected_content": (
"Unexpected content type returned, usually due to being redirected. "
"Do you need to login for internet access?"
),
"chrome": {
"startup": "Opening Chrome...",
"login_to_complete": (
"Complete the login procedure manually by pressing the Login button again."
),
"no_token": "No authorization token could be found.",
"closed_window": (
"The Chrome window was closed before the login procedure could be completed."
),
},
"error_code": "Login error code: {error_code}",
"incorrect_login_pass": "Incorrect username or password.",
"incorrect_email_code": "Incorrect email code.",
"incorrect_twofa_code": "Incorrect 2FA code.",
"email_code_required": "Email code required. Check your email.",
"twofa_code_required": "2FA token required.",
},
"error": {
"captcha": "Your login attempt was denied by CAPTCHA.\nPlease try again in 12+ hours.",
"site_down": "Twitch is down, retrying in {seconds} seconds...",
"no_connection": "Cannot connect to Twitch, retrying in {seconds} seconds...",
},
"gui": {
"output": "Output",
"status": {
"name": "Status",
"idle": "Idle",
"exiting": "Exiting...",
"terminated": "Terminated",
"cleanup": "Cleaning up channels...",
"gathering": "Gathering channels...",
"switching": "Switching the channel...",
"fetching_inventory": "Fetching inventory...",
"fetching_campaigns": "Fetching campaigns...",
"adding_campaigns": "Adding campaigns to inventory... {counter}",
},
"tabs": {
"main": "Main",
"inventory": "Inventory",
"settings": "Settings",
"help": "Help",
},
"tray": {
"notification_title": "Mined Drop",
"minimize": "Minimize to Tray",
"show": "Show",
"quit": "Quit",
},
"login": {
"name": "Login Form",
"labels": "Status:\nUser ID:",
"logged_in": "Logged in",
"logged_out": "Logged out",
"logging_in": "Logging in...",
"required": "Login required",
"request": "Please log in to continue.",
"username": "Username",
"password": "Password",
"twofa_code": "2FA code (optional)",
"button": "Login",
},
"websocket": {
"name": "Websocket Status",
"websocket": "Websocket #{id}:",
"initializing": "Initializing...",
"connected": "Connected",
"disconnected": "Disconnected",
"connecting": "Connecting...",
"disconnecting": "Disconnecting...",
"reconnecting": "Reconnecting...",
},
"progress": {
"name": "Campaign Progress",
"drop": "Drop:",
"game": "Game:",
"campaign": "Campaign:",
"remaining": "{time} remaining",
"drop_progress": "Progress:",
"campaign_progress": "Progress:",
},
"channels": {
"name": "Channels",
"switch": "Switch",
"load_points": "Load Points",
"online": "ONLINE ✔",
"pending": "OFFLINE ⏳",
"offline": "OFFLINE ❌",
"headings": {
"channel": "Channel",
"status": "Status",
"game": "Game",
"viewers": "Viewers",
"points": "Points",
},
},
"inventory": {
"filter": {
"name": "Filter",
"show": "Show:",
"not_linked": "Not linked",
"upcoming": "Upcoming",
"expired": "Expired",
"excluded": "Excluded",
"finished": "Finished",
"refresh": "Refresh",
},
"status": {
"linked": "Linked ✔",
"not_linked": "Not Linked ❌",
"active": "Active ✔",
"upcoming": "Upcoming ⏳",
"expired": "Expired ❌",
"claimed": "Claimed ✔",
"ready_to_claim": "Ready to claim ⏳",
},
"starts": "Starts: {time}",
"ends": "Ends: {time}",
"allowed_channels": "Allowed Channels:",
"all_channels": "All",
"and_more": "and {amount} more...",
"percent_progress": "{percent} of {minutes} minutes",
"minutes_progress": "{minutes} minutes",
},
"settings": {
"general": {
"name": "General",
"autostart": "Autostart: ",
"tray": "Autostart into tray: ",
"tray_notifications": "Tray notifications: ",
"priority_mode": "Priority mode: ",
"unlinked_campaigns": "Allow Unlinked Campaigns: ",
"proxy": "Proxy (requires restart):",
},
"priority_modes": {
"priority_only": "Priority list only",
"ending_soonest": "Ending soonest",
"low_availability": "Low availability first",
},
"game_name": "Game name",
"priority": "Priority",
"exclude": "Exclude",
"reload": "Reload",
"reload_text": "Most changes require a reload to take an immediate effect: ",
},
"help": {
"links": {
"name": "Useful Links",
"inventory": "See Twitch inventory",
"campaigns": "See all campaigns and manage account links",
},
"how_it_works": "How It Works",
"how_it_works_text": (
"Every several seconds, the application pretends to watch a particular stream "
"by fetching stream metadata - this is enough to advance the drops. "
"Note that this completely bypasses the need to download "
"any actual stream of video and sound. "
"To keep the status (ONLINE or OFFLINE) of the channels up-to-date, "
"there's a websocket connection established that receives events about streams "
"going up or down, or updates regarding the current number of viewers."
),
"getting_started": "Getting Started",
"getting_started_text": (
"1. Login to the application.\n"
"2. Ensure your Twitch account is linked to all campaigns "
"you're interested in mining.\n"
"3. If you're interested in mining everything possible, "
"change the Priority Mode to anything other than \"Priority list only\" "
"and press on \"Reload\".\n"
"4. If you want to mine specific games first, use the \"Priority\" list "
"to set up an ordered list of games of your choice. "
"Games from the top of the list will be attempted to be mined first, "
"before the ones lower down the list.\n"
"5. Keep the \"Priority mode\" selected as \"Priority list only\", "
"to avoid mining games that are not on the priority list. "
"Or not - it's up to you.\n"
"6. Use the \"Exclude\" list to tell the application "
"which games should never be mined.\n"
"7. Changing the contents of either of the lists, or changing "
"the \"Priority mode\", requires you to press on \"Reload\" "
"for the changes to take an effect."
),
},
},
}
class Translator:
def __init__(self) -> None:
self._langs: list[str] = []
# start with (and always copy) the default translation
self._translation: Translation = default_translation.copy()
# if we're in dev, update the template English.json file
if not IS_PACKAGED:
default_langpath = LANG_PATH.joinpath(f"{DEFAULT_LANG}.json")
json_save(default_langpath, default_translation)
self._translation["language_name"] = DEFAULT_LANG
# load available translation names
for filepath in LANG_PATH.glob("*.json"):
self._langs.append(filepath.stem)
self._langs.sort()
if DEFAULT_LANG in self._langs:
self._langs.remove(DEFAULT_LANG)
self._langs.insert(0, DEFAULT_LANG)
@property
def languages(self) -> abc.Iterable[str]:
return iter(self._langs)
@property
def current(self) -> str:
return self._translation["language_name"]
def set_language(self, language: str):
if language not in self._langs:
raise ValueError("Unrecognized language")
elif self._translation["language_name"] == language:
# same language as loaded selected
return
elif language == DEFAULT_LANG:
# default language selected - use the memory value
self._translation = default_translation.copy()
else:
self._translation = json_load(
LANG_PATH.joinpath(f"{language}.json"), default_translation
)
if "language_name" in self._translation:
raise ValueError("Translations cannot define 'language_name'")
self._translation["language_name"] = language
def __call__(self, *path: str) -> str:
if not path:
raise ValueError("Language path expected")
v: Any = self._translation
try:
for key in path:
v = v[key]
except KeyError:
# this can only really happen for the default translation
raise MinerException(
f"{self.current} translation is missing the '{' -> '.join(path)}' translation key"
)
return v
_ = Translator()