Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return
except requests.exceptions.ConnectionError:
showWarning("Could not connect to Duolingo. Please check your internet connection.")
return
finally:
mw.progress.finish()
language_string = vocabulary_response['language_string']
vocabs = vocabulary_response['vocab_overview']
did = mw.col.decks.id("Default")
mw.col.decks.select(did)
deck = mw.col.decks.get(did)
deck['mid'] = model['id']
mw.col.decks.save(deck)
words_to_add = [vocab for vocab in vocabs if vocab['id'] not in gids_to_notes]
if not words_to_add:
showInfo("Successfully logged in to Duolingo, but no new words found in {} language.".format(language_string))
elif askUser("Add {} notes from {} language?".format(len(words_to_add), language_string)):
word_chunks = [words_to_add[x:x + 50] for x in range(0, len(words_to_add), 50)]
mw.progress.start(immediate=True, label="Importing from Duolingo...", max=len(words_to_add))
notes_added = 0
for word_chunk in word_chunks:
translations = lingo.get_translations([vocab['word_string'] for vocab in word_chunk])
for vocab in word_chunk:
def createDeck(deckName, modelName):
# create new deck and custom model
model = __createModel(modelName)
deckId = mw.col.decks.id(deckName)
deck = mw.col.decks.get(deckId)
mw.col.reset()
mw.reset()
# assign custom model to new deck
deck["mid"] = model["id"]
mw.col.decks.save(deck)
# assign new deck to custom model
mw.col.models.setCurrent(model)
mw.col.models.current()["did"] = deck["id"]
mw.col.models.save(model)
def getOrCreateDeck(deckName):
deck_id = mw.col.decks.id(deckName)
deck = mw.col.decks.get(deck_id)
mw.col.decks.save(deck)
mw.col.reset()
mw.reset()
return deck
def addNote(front, back, tag, model, deck, id=None):
"""
Add note with `front` and `back` to `deck` using `model`.
If `deck` doesn't exist, it is created.
If `model` doesn't exist, nothing is done.
If `id` is passed, it is used as the id
"""
model = mw.col.models.byName(model)
if model:
mw.col.decks.current()['mid'] = model['id']
else:
return None
# Creates or reuses deck with name passed using `deck`
did = mw.col.decks.id(deck)
deck = mw.col.decks.get(did)
note = mw.col.newNote()
note.model()['did'] = did
note.fields[0] = front
note.fields[1] = back
if id:
note.id = id
note.addTag(tag)
def loadFromCustomDeck(self):
log("loadFromCustomDeck")
deck = mw.col.decks.byName(self.Profile.KanjiDeckName)
cards = AnkiHelper.getCards(deck["id"])
kanjiDict = dict()
for card in cards:
note = card.note
try:
kanjiDict[note[self.Profile.KanjiExpression]] = (note[self.Profile.KanjiKeyword], card.ivl)
except: continue
self.dlmod = deck["mod"]
self.nlmod, self.clmod = AnkiHelper.getLastModified(deck["id"])
kanjiFile = open(self.kanjiCustomDictPath, "wb")
pickle.dump((self.dlmod, self.nlmod, self.clmod, kanjiDict), kanjiFile, 2)
return kanjiDict
def addNote(front, back, tag, model, deck, id=None):
"""
Add note with `front` and `back` to `deck` using `model`.
If `deck` doesn't exist, it is created.
If `model` doesn't exist, nothing is done.
If `id` is passed, it is used as the id
"""
model = mw.col.models.byName(model)
if model:
mw.col.decks.current()['mid'] = model['id']
else:
return None
# Creates or reuses deck with name passed using `deck`
did = mw.col.decks.id(deck)
deck = mw.col.decks.get(did)
note = mw.col.newNote()
note.model()['did'] = did
note.fields[0] = front
note.fields[1] = back
if id:
note.id = id
note.addTag(tag)
mw.col.addNote(note)
mw.col.save()
return note.id
def createDeck(self, name, terms):
"""create new Anki deck from downloaded data"""
#create new deck and custom model
deck = mw.col.decks.get(mw.col.decks.id(name))
model = addCustomModel(name, mw.col)
#assign custom model to new deck
mw.col.decks.select(deck["id"])
mw.col.decks.get(deck)["mid"] = model["id"]
mw.col.decks.save(deck)
#assign new deck to custom model
mw.col.models.setCurrent(model)
mw.col.models.current()["did"] = deck["id"]
mw.col.models.save(model)
for term in terms:
note = mw.col.newNote()
note["Front"] = term["term"]
note["Back"] = term["definition"]
mw.col.addNote(note)
mw.col.reset()
mw.reset()
def cleanUp():
print "Cleaning up SuperStyler junk..."
decks_to_delete = []
for deck in mw.col.decks.all():
if deck['name'].startswith(PREFIX):
decks_to_delete.append(deck)
for deck in decks_to_delete:
print "Deleting leftover deck: " + deck['name']
mw.col.decks.rem(deck['id'])
tmpls_to_delete = []
for model in mw.col.models.all():
for tmpl in model['tmpls']:
if tmpl['name'].startswith(PREFIX):
tmpls_to_delete.append((model, tmpl))
for (model, tmpl) in tmpls_to_delete:
print "Deleting leftover tmpl: " + tmpl['name'] + " -- from model: " + model['name']
mw.col.models.remTemplate(model, tmpl)