def create_pin(self, provider_name, request_params=None): request_params = Utils.default(request_params, {}) headers = {'addon': self.get_addon_header()} body = urllib.parse.urlencode({'provider': provider_name}) return Request(KodiUtils.get_signin_server() + '/pin', body, headers, **request_params).request_json()
def _add_account(self): request_params = { 'waiting_retry': lambda request, remaining: self._progress_dialog_bg. update(int((request.current_delay - remaining) / request. current_delay * 100), heading=self._common_addon.getLocalizedString(32043) % ('' if request.current_tries == 1 else ' again'), message=self._common_addon.getLocalizedString(32044) % str( int(remaining) ) + ' ' + self._common_addon.getLocalizedString(32045) % (str(request.current_tries + 1), str(request.tries))), 'on_complete': lambda request: (self._progress_dialog.close(), self._progress_dialog_bg.close()), 'cancel_operation': self.cancel_operation, 'wait': self._system_monitor.waitForAbort } provider = self.get_provider() self._progress_dialog.update( 0, self._common_addon.getLocalizedString(32008)) self._ip_before_pin = Request(KodiUtils.get_signin_server() + '/ip', None).request() pin_info = provider.create_pin(request_params) self._progress_dialog.close() if self.cancel_operation(): return if not pin_info: raise Exception('Unable to retrieve a pin code') tokens_info = {} request_params[ 'on_complete'] = lambda request: self._progress_dialog_bg.close() self._pin_dialog = QRDialogProgress.create( self._addon_name, KodiUtils.get_signin_server() + '/signin/%s' % pin_info['pin'], self._common_addon.getLocalizedString(32009), self._common_addon.getLocalizedString(32010) % ('[B]%s[/B]' % KodiUtils.get_signin_server(), '[B][COLOR lime]%s[/COLOR][/B]' % pin_info['pin'])) self._pin_dialog.show() max_waiting_time = time.time() + self._DEFAULT_SIGNIN_TIMEOUT while not self.cancel_operation() and max_waiting_time > time.time(): remaining = round(max_waiting_time - time.time()) percent = int(remaining / self._DEFAULT_SIGNIN_TIMEOUT * 100) self._pin_dialog.update( percent, line3='[CR]' + self._common_addon.getLocalizedString(32011) % str(int(remaining)) + '[CR][CR]Your source id is: %s' % Utils.get_source_id(Utils.unicode(self._ip_before_pin))) if int(remaining) % 5 == 0 or remaining == 1: tokens_info = provider.fetch_tokens_info( pin_info, request_params=request_params) if self.cancel_operation() or tokens_info: break if self._system_monitor.waitForAbort(1): break self._pin_dialog.close() if self.cancel_operation() or time.time() >= max_waiting_time: return if not tokens_info: raise Exception('Unable to retrieve the auth2 tokens') self._progress_dialog.update( 25, self._common_addon.getLocalizedString(32064), ' ', ' ') try: account = provider.get_account(request_params=request_params, access_tokens=tokens_info) except Exception as e: raise UIException(32065, e) if self.cancel_operation(): return self._progress_dialog.update( 50, self._common_addon.getLocalizedString(32017)) try: account['drives'] = provider.get_drives( request_params=request_params, access_tokens=tokens_info) except Exception as e: raise UIException(32018, e) if self.cancel_operation(): return self._progress_dialog.update( 75, self._common_addon.getLocalizedString(32020)) try: account['access_tokens'] = tokens_info self._account_manager.save_account(account) except Exception as e: raise UIException(32021, e) if self.cancel_operation(): return self._progress_dialog.update(90) try: accounts = self._account_manager.get_accounts() for drive in account['drives']: driveid = drive['id'] Logger.debug('Looking for account %s...' % driveid) if driveid in accounts: drive = accounts[driveid]['drives'][0] Logger.debug(drive) if drive['id'] == driveid and drive['type'] == 'migrated': Logger.debug('Account %s removed.' % driveid) self._account_manager.remove_account(driveid) except Exception as e: pass if self.cancel_operation(): return self._progress_dialog.close() KodiUtils.executebuiltin('Container.Refresh')
def _handle_exception(self, ex, show_error_dialog=True): stacktrace = ExceptionUtils.full_stacktrace(ex) rex = ExceptionUtils.extract_exception(ex, RequestException) uiex = ExceptionUtils.extract_exception(ex, UIException) httpex = ExceptionUtils.extract_exception(ex, HTTPError) urlex = ExceptionUtils.extract_exception(ex, URLError) line1 = self._common_addon.getLocalizedString(32027) line2 = Utils.unicode(ex) line3 = self._common_addon.getLocalizedString(32016) if uiex: line1 = self._common_addon.getLocalizedString(int(Utils.str(uiex))) line2 = Utils.unicode(uiex.root_exception) elif rex and rex.response: line1 += ' ' + Utils.unicode(rex) line2 = ExceptionUtils.extract_error_message(rex.response) send_report = True add_account_cmd = 'RunPlugin(' + self._addon_url + '?' + urllib.urlencode( { 'action': '_add_account', 'content_type': self._content_type }) + ')' if isinstance(ex, AccountNotFoundException) or isinstance( ex, DriveNotFoundException): show_error_dialog = False if self._dialog.yesno( self._addon_name, self._common_addon.getLocalizedString(32063) % '\n'): KodiUtils.executebuiltin(add_account_cmd) elif rex and httpex: if httpex.code >= 500: line1 = self._common_addon.getLocalizedString(32035) line2 = None line3 = self._common_addon.getLocalizedString(32038) elif httpex.code >= 400: driveid = Utils.get_safe_value(self._addon_params, 'driveid') if driveid: self._account_manager.load() account = self._account_manager.get_account_by_driveid( driveid) drive = self._account_manager.get_drive_by_driveid(driveid) if KodiUtils.get_signin_server( ) in rex.request or httpex.code == 401: send_report = False show_error_dialog = False if self._dialog.yesno( self._addon_name, self._common_addon.getLocalizedString(32046) % (self._get_display_name(account, drive, True), '\n')): KodiUtils.executebuiltin(add_account_cmd) elif httpex.code == 403: line1 = self._common_addon.getLocalizedString(32019) line2 = line3 = None elif httpex.code == 404: send_report = False line1 = self._common_addon.getLocalizedString(32037) line2 = line2 = None else: line1 = self._common_addon.getLocalizedString(32036) line3 = self._common_addon.getLocalizedString(32038) else: if KodiUtils.get_signin_server( ) + '/pin/' in rex.request and httpex.code == 404 and self._ip_before_pin: ip_after_pin = Request( KodiUtils.get_signin_server() + '/ip', None).request() if self._ip_before_pin != ip_after_pin: send_report = False line1 = self._common_addon.getLocalizedString( 32072) line2 = self._common_addon.getLocalizedString( 32073) % ( self._ip_before_pin, ip_after_pin, ) elif urlex: reason = Utils.str(urlex.reason) line3 = self._common_addon.getLocalizedString(32074) if '[Errno 101]' in reason: line1 = self._common_addon.getLocalizedString(32076) elif '[Errno 11001]' in reason: line1 = self._common_addon.getLocalizedString(32077) elif 'CERTIFICATE_VERIFY_FAILED' in reason: line1 = self._common_addon.getLocalizedString(32078) else: line1 = self._common_addon.getLocalizedString(32075) report = '[%s] [%s]/[%s]\n\n%s\n%s\n%s\n\n%s' % ( self._addonid, self._addon_version, self._common_addon_version, line1, line2, line3, stacktrace) if rex: report += '\n\n%s\nResponse:\n%s' % (rex.request, rex.response) report += '\n\nshow_error_dialog: %s' % show_error_dialog Logger.error(report) if show_error_dialog: self._dialog.ok(self._addon_name, line1, line2, line3) if send_report: report_error = KodiUtils.get_addon_setting( 'report_error', self._common_addon_id) == 'true' report_error_invite = KodiUtils.get_addon_setting( 'report_error_invite', self._common_addon_id) == 'true' if not report_error and not report_error_invite: if not self._dialog.yesno( self._addon_name, self._common_addon.getLocalizedString(32050), None, None, self._common_addon.getLocalizedString(32012), self._common_addon.getLocalizedString(32013)): KodiUtils.set_addon_setting('report_error', 'true', self._common_addon_id) KodiUtils.set_addon_setting('report_error_invite', 'true', self._common_addon_id) ErrorReport.send_report(report)