def __init__(self): profile_path = Utils.unicode( KodiUtils.translate_path(KodiUtils.get_addon_info('profile'))) ini_path = os.path.join(profile_path, 'onedrive.ini') if os.path.exists(ini_path): config = ConfigParser() account_manager = AccountManager(profile_path) config.read(ini_path) for driveid in config.sections(): Logger.notice('Migrating drive %s...' % driveid) account = {'id': driveid, 'name': config.get(driveid, 'name')} account['drives'] = [{ 'id': driveid, 'name': '', 'type': 'migrated' }] account['access_tokens'] = { 'access_token': config.get(driveid, 'access_token'), 'refresh_token': config.get(driveid, 'refresh_token'), 'expires_in': 0, 'date': 0 } try: account_manager.add_account(account) except Exception as e: raise UIException(32021, e) os.remove(ini_path) KodiUtils.set_addon_setting('migrated', 'true')
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(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.add_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.load() 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')