Coverage for modules/org/openteacher/interfaces/qt/webServices/courseHeroApi/courseHeroApi : 63%
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: http://www.coursehero.com/api/intro.php
"""
kwargs["api_key"] = API_KEY fullUrl = self._baseUrl + url + "?" + urllib.parse.urlencode(kwargs) logger.info("Opening URL: %s" % fullUrl) return io.TextIOWrapper(self._opener.open(fullUrl), encoding='UTF-8')
try: fd = self._open("/sets/", query=searchTerm) except urllib.error.HTTPError as e: logger.debug(e) logger.debug(e.read()) return {"data": []} return json.load(fd)
fd = self._open("/sets/%s/" % id) data = json.load(fd)["data"]
created = datetime.datetime.fromtimestamp(data["created"])
list = {} list["title"] = data["title"]
list["items"] = [ { "id": card["fcid"], "created": created, "questions": self._parse(card["term"]), "answers": self._parse(card["definition"]), "commentfterAnswering": card["example"], } for card in data["flashcards"] ]
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):
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="loaderGui"), self._mm.mods(type="languageCodeGuesser"), self._mm.mods(type="metadata"), ) self._mm.mods(type="translator"), )
"default": 630, }
def _getLanguageName(self):
global QtCore, QtWidgets except ImportError: return
except IndexError: pass else:
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):
try: data = self._api.searchSets(self._dialog.searchTerm) except urllib.error.URLError as e: logger.debug(e, exc_info=True) self._noConnection() return self._dialog.setResults([ (result["title"], result["fcsid"]) for result in data["data"] ])
QtWidgets.QMessageBox.warning( self._uiModule.qtParent, _("No coursehero.com connection"), _("coursehero.com didn't accept the connection. Are you sure that your internet connection works and coursehero.com is online?") )
list = self._api.downloadSet(setId) try: self._loadList(list) except NotImplementedError: return except urllib.error.URLError as e: logger.debug(e, exc_info=True) self._noConnection() return
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: logger.debug(e, exc_info=True) self._noConnection() return
#everything went well self._uiModule.statusViewer.show(_("The word list was imported from coursehero.com successfully."))
self._modules.default("active", type="loaderGui").loadFromLesson("words", list)
|