Coverage for modules/org/openteacher/interfaces/qt/webServices/quizletApi/quizletApi : 56%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
#! /usr/bin/env python3 # -*- coding: utf-8 -*-
# Copyright 2012-2013, Marten de Vries # # This file is part of OpenTeacher. # # OpenTeacher is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # OpenTeacher is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with OpenTeacher. If not, see <http://www.gnu.org/licenses/>.
"""See for documentation of the API this communicates with: https://quizlet.com/api/2.0/docs/ (requires login)
""" super().__init__(*args, **kwargs)
self._appId = appId self._parse = parse self._getLanguageName = getLanguageName
self._baseUrl = "https://api.quizlet.com/2.0"
kwargs["client_id"] = self._appId kwargs["whitespace"] = 0 fullUrl = self._baseUrl + url + "?" + urllib.parse.urlencode(kwargs) return io.TextIOWrapper(urllib.request.urlopen(fullUrl), encoding='UTF-8')
try: fd = self._open("/search/sets", q=searchTerm) except urllib.error.HTTPError as e: logger.debug(e) logger.debug(e.read()) return {"sets": []} return json.load(fd)
fd = self._open("/sets/%s" % id) data = json.load(fd)
created = datetime.datetime.fromtimestamp(data["created_date"])
list = {} list["title"] = data["title"] list["questionLanguage"] = self._getLanguageName(data["lang_terms"]) or u'' list["answerLanguage"] = self._getLanguageName(data["lang_definitions"]) or u''
list["items"] = [] for card in data["terms"]: list["items"].append({ "id": card["id"], "created": created, "questions": self._parse(card["term"]), "answers": self._parse(card["definition"]), })
return { "list": list, "resources": {}, }
global Model, SearchDialog
"""Choices should be an iterable object of tuples of size two, with in it first the text to display and second the value to return by getChoice().
""" self.beginResetModel() self._choices = choices self.endResetModel()
return len(self._choices)
if not (index.isValid() and role == QtCore.Qt.DisplayRole): return
return self._choices[index.row()][0]
return self._choices[index.row()][1]
QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok, parent=self )
def chosenResults(self): return [self._model.getChoice(i) for i in self._listView.selectedIndexes()]
self._model.update(results)
def searchTerm(self): return self._searchBox.text()
if event.key() != QtCore.Qt.Key_Return: #prevent the Ok button from triggering, most people #would expect it to be the search shortcut. (Which it #isn't, either.) return super().keyPressEvent(event)
self._mm.mods(type="ui"), self._mm.mods(type="buttonRegister"), self._mm.mods(type="wordsStringParser"), self._mm.mods(type="languageCodeGuesser"), ) self._mm.mods(type="translator"), self._mm.mods(type="loader"), )
"default": 527, }
global QtCore, QtWidgets except ImportError: return
except IndexError: pass else:
def _getLanguageName(self): return self._modules.default("active", type="languageCodeGuesser").getLanguageName
def _api(self): #Just kidding... p, x, k, a, s = str, eval, 26, "base64", "ascii" y = bytes.fromhex(p(0x1b84) + ("6%s253" % k) + x("\"6e6d5\"") + "07152").decode(s)
return QuizletApi(y, self._parse, self._getLanguageName)
global _ global ngettext
#Install translator except IndexError: _, ngettext = str, lambda a, b, n: a if n == 1 else b else: self._mm.resourcePath("translations") )
#Translate all active dialogs
def _parse(self): return self._modules.default("active", type="wordsStringParser").parse
try: data = self._api.searchSets(self._dialog.searchTerm) except urllib.error.URLError as e: logger.debug(e, exc_info=True) self._noConnection() return results = [] for result in data["sets"]: results.append((result["title"], result["id"])) self._dialog.setResults(results)
QtWidgets.QMessageBox.warning( self._uiModule.qtParent, _("No Quizlet connection"), _("Quizlet didn't accept the connection. Are you sure that your internet connection works and quizlet.com is online?") )
except urllib.error.URLError as e: #for debugging purposes logger.debug(e, exc_info=True) self._noConnection()
try: for setId in self._dialog.chosenResults: list = self._api.downloadSet(setId) try: self._loadList(list) except NotImplementedError: return except urllib.error.URLError as e: #for debugging purposes logger.debug(e, exc_info=True) self._noConnection() return
#everything went well self._uiModule.statusViewer.show(_("The word list was imported from Quizlet successfully."))
self._modules.default("active", type="loaderGui").loadFromLesson("words", list)
|