Exemple #1
0
class SciHubEVA(QObject):
    beforeRampage = pyqtSignal()
    afterRampage = pyqtSignal()

    showErrorMessage = pyqtSignal(str, str)
    showInfoMessage = pyqtSignal(str, str)

    setSaveToDir = pyqtSignal(str)
    appendLogs = pyqtSignal(str)

    def __init__(self):
        super(SciHubEVA, self).__init__()

        self._conf = SciHubConf()

        self._engine = QQmlApplicationEngine()
        self._engine.load('qrc:/ui/SciHubEVA.qml')
        self._window = self._engine.rootObjects()[0]
        self._connect()

        self._scihub_preferences = SciHubPreferences(self._conf)

        save_to_dir = self._conf.get('common', 'save_to_dir')
        if not save_to_dir or save_to_dir.strip() == '':
            self._save_to_dir = None
        else:
            self._save_to_dir = save_to_dir
            self.setSaveToDir.emit(save_to_dir)

    def _connect(self):
        # Connect QML signals to PyQt slots
        self._window.saveToDir.connect(self.saveToDir)
        self._window.rampage.connect(self.rampage)
        self._window.showWindowPreference.connect(self.showWindowPreferences)

        # Connect PyQt signals to QML slots
        self.beforeRampage.connect(self._window.beforeRampage)
        self.afterRampage.connect(self._window.afterRampage)

        self.showErrorMessage.connect(self._window.showErrorMessage)
        self.showInfoMessage.connect(self._window.showInfoMessage)

        self.setSaveToDir.connect(self._window.setSaveToDir)
        self.appendLogs.connect(self._window.appendLogs)

    @property
    def conf(self):
        return self._conf

    @pyqtSlot(str)
    def saveToDir(self, directory):
        self._save_to_dir = directory
        self._conf.set('common', 'save_to_dir', directory)

    @pyqtSlot()
    def showWindowPreferences(self):
        self._scihub_preferences.loadFromConf()
        self._scihub_preferences.showWindowPreferences.emit()

    @pyqtSlot(str)
    def rampage(self, query):
        scihub_api = SciHubAPI(query, callback=self._afterRampage, conf=self._conf, log=self._log)
        self._beforeRampage()
        scihub_api.start()

    def _beforeRampage(self):
        self.beforeRampage.emit()

    def _afterRampage(self):
        self.afterRampage.emit()

    def _log(self, message, type = None):
        if type:
            log_formater = '[{type}] - {message}'
        else:
            log_formater = '{message}'

        self.appendLogs.emit(log_formater.format(type=type, message=message))
Exemple #2
0
class SciHubEVA(QObject):
    beforeRampage = pyqtSignal()
    afterRampage = pyqtSignal()

    showErrorMessage = pyqtSignal(str, str)
    showInfoMessage = pyqtSignal(str, str)

    setSaveToDir = pyqtSignal(str)
    appendLogs = pyqtSignal(str)

    def __init__(self):
        super(SciHubEVA, self).__init__()

        self._conf = SciHubConf()

        self._engine = QQmlApplicationEngine()
        self._engine.load('qrc:/ui/SciHubEVA.qml')
        self._window = self._engine.rootObjects()[0]
        self._connect()

        self._scihub_preferences = SciHubPreferences(self._conf)
        self._scihub_captcha = SciHubCaptcha(self, log=self.log)
        self._captcha_query = None

        save_to_dir = self._conf.get('common', 'save_to_dir')
        if not save_to_dir or save_to_dir.strip() == '':
            self._save_to_dir = None
        else:
            self._save_to_dir = save_to_dir
            self.setSaveToDir.emit(save_to_dir)

    def _connect(self):
        # Connect QML signals to PyQt slots
        self._window.saveToDir.connect(self.saveToDir)
        self._window.rampage.connect(self.rampage)
        self._window.showWindowPreference.connect(self.showWindowPreferences)

        # Connect PyQt signals to QML slots
        self.beforeRampage.connect(self._window.beforeRampage)
        self.afterRampage.connect(self._window.afterRampage)

        self.showErrorMessage.connect(self._window.showErrorMessage)
        self.showInfoMessage.connect(self._window.showInfoMessage)

        self.setSaveToDir.connect(self._window.setSaveToDir)
        self.appendLogs.connect(self._window.appendLogs)

    @property
    def conf(self):
        return self._conf

    @pyqtSlot(str)
    def saveToDir(self, directory):
        self._save_to_dir = directory
        self._conf.set('common', 'save_to_dir', directory)

    @pyqtSlot()
    def showWindowPreferences(self):
        self._scihub_preferences.loadFromConf()
        self._scihub_preferences.showWindowPreferences.emit()

    @pyqtSlot(str)
    def rampage(self, input_query):
        """Download PDF with query of input

        Args:
            input_query: Query of input

        """

        scihub_api = SciHubAPI(input_query,
                               callback=self.rampage_callback,
                               rampage_type=SciHubRampageType.INPUT,
                               conf=self._conf,
                               log=self.log)
        self.beforeRampage.emit()
        scihub_api.start()

    def rampageWithCaptchar(self, captcha_answer):
        """ Download PDF with captcha query (self._captcha_query) and captcha answer

        Args:
            captcha_answer: Captcha answer

        """

        scihub_api = SciHubAPI(
            self._captcha_query,
            callback=self.rampage_callback,
            rampage_type=SciHubRampageType.PDF_CAPTCHA_RESPONSE,
            conf=self._conf,
            log=self.log,
            captcha_answer=captcha_answer)

        self.beforeRampage.emit()
        scihub_api.start()

    def rampage_callback(self, res, err):
        """Callback function

        Args:
            res: Result from last round rampage
            err: Error

        """

        if err == SciHubError.BLOCKED_BY_CAPTCHA:
            self.captcha_callback(res)
        else:
            self.afterRampage.emit()

    def captcha_callback(self, pdf_captcha_response):
        """Callback function for PDF captcha response

        Args:
            pdf_captcha_response: PDF captcha response

        """

        self._captcha_query = pdf_captcha_response
        _, captcha_img_url = SciHubAPI.get_captcha_info(pdf_captcha_response)
        self._scihub_captcha.showWindowCaptcha.emit(captcha_img_url)

    def log(self, message, level=None):
        if level:
            log_formater = '[{level}] - {message}'
        else:
            log_formater = '{message}'

        self.appendLogs.emit(log_formater.format(level=level, message=message))