Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove lexeme id fallback translation method; use capitalized word as fallback method #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions duolingo_sync/duolingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,23 +539,6 @@ def get_related_words(self, word, language_abbr=None):
return [w for w in overview['vocab_overview']
if w['lexeme_id'] in related_lexemes]

def get_word_definition_by_id(self, lexeme_id):
"""
Get the dictionary entry from
``https://www.duolingo.com/api/1/dictionary_page?lexeme_id=``<lexeme_id>``
:param lexeme_id: Identifier of the word
:type: str
:return: The dictionary entry for the given word
"""
url = "https://www.duolingo.com/api/1/dictionary_page?lexeme_id=%s" % lexeme_id

request = self.session.get(url)

try:
return request.json()
except:
raise Exception('Could not get word definition')


attrs = [
'settings', 'languages', 'user_info', 'certificates', 'streak_info',
Expand Down
19 changes: 9 additions & 10 deletions duolingo_sync/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,19 @@ def add_vocab(retrieve_result: VocabRetrieveResult) -> AddVocabResult:

words_processed = 0
for word_chunk in word_chunks:
lexeme_ids = {vocab['word_string']: vocab['id'] for vocab in word_chunk}
needs_capitalization = set()
translations = lingo.get_translations([vocab['word_string'] for vocab in word_chunk])

# The `get_translations` endpoint might not always return a translation. In this case, try
# a couple of fallback methods
for word_string, translation in translations.items():
fallback_translation = "Translation not found for '{}'. Edit this card to add it.".format(word_string)
if not translation:
fallback_translation = "Translation not found for '{}'. Edit this card to add it.".format(word_string)
try:
new_translation = lingo.get_word_definition_by_id(lexeme_ids[word_string])['translations']
except Exception:
new_translation = fallback_translation

translations[word_string] = [new_translation if new_translation else fallback_translation]
_, new_translation = dict(lingo.get_translations([word_string.capitalize()])).popitem()
if new_translation:
needs_capitalization.add(word_string)
translations[word_string] = new_translation
else:
translations[word_string] = fallback_translation

for vocab in word_chunk:
n = mw.col.newNote()
Expand All @@ -148,7 +147,7 @@ def add_vocab(retrieve_result: VocabRetrieveResult) -> AddVocabResult:
n['Gid'] = vocab['id']
n['Gender'] = vocab['gender'] if vocab['gender'] else ''
n['Source'] = '; '.join(translations[vocab['word_string']])
n['Target'] = vocab['word_string']
n['Target'] = vocab['word_string'].capitalize() if vocab["word_string"] in needs_capitalization else vocab['word_string']
n['Pronunciation'] = vocab['normalized_string'].strip()
n['Target Language'] = retrieve_result.language_string
n.addTag(retrieve_result.language_string)
Expand Down