-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
81 lines (62 loc) · 2.22 KB
/
main.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
from json import dumps
from duolingo import Duolingo
from requests import get, post
from selectolax.parser import HTMLParser
from deep_translator import GoogleTranslator
USERNAME = ''
PASSWORD = ''
MAIN_LANGUAGE = ''
TARGET_LANGUAGE = ''
DECK_NAME = f'[duolingo2anki] {MAIN_LANGUAGE} - {TARGET_LANGUAGE}'
NOTE_TYPE = ''
WORD_FIELD = ''
MEANING_FIELD = ''
SPELLING_FIELD = ''
def log(message: str):
print(f'[duolingo2anki] - {message}')
def request(action, **params):
return {'action': action, 'params': params, 'version': 6}
def invoke(action, **params):
data = dumps(request(action, **params))
response = post('http://localhost:8765', data=data.encode()).json()
return response
def main():
log('Connecting to the API...')
api = Duolingo(USERNAME, PASSWORD)
log('Getting words...')
words = [i['word_string'] for i in api.get_vocabulary(TARGET_LANGUAGE)['vocab_overview']]
url = F'https://duome.eu/vocabulary/{MAIN_LANGUAGE}/{TARGET_LANGUAGE}'
content = get(url).content
parser = HTMLParser(content)
lis = parser.css_first('.plain').css('li:not(.single)')
log('Getting meanings and spellings...')
vocabulary = []
gt = GoogleTranslator(source=TARGET_LANGUAGE, target=MAIN_LANGUAGE)
for li in lis:
if (info := li.css_first('.wA')).text() in words:
title = info.attributes['title']
spelling, meaning = title.split(']')
vocabulary.append(
{
'word': info.text().strip(),
'spelling': spelling[1:].strip(),
'meaning': meaning.strip() if len(meaning.strip()) > 0 else gt.translate(info.text().strip())
}
)
invoke('createDeck', deck=DECK_NAME)
log('Creating notes...')
notes = [
{
'deckName': DECK_NAME,
'modelName': NOTE_TYPE,
'fields': {
WORD_FIELD: w['word'],
MEANING_FIELD: w['meaning'],
SPELLING_FIELD: w['spelling']
}
} for w in vocabulary
]
invoke('addNotes', notes=notes)
log('Done.')
if __name__ == '__main__':
main()