def start(self): port = self.get_port() KodiUtils.set_service_port(self.name, port) self._server = BaseServer((self._interface, port), self._handler, self, self.data) Logger.notice('Service \'%s\' started in port %s' % (self.name, port)) self._server.serve_forever()
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 __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 fetch_tokens_info(self, pin_info, request_params={}): tokens_info = super(Dropbox, self).fetch_tokens_info(pin_info, request_params) if tokens_info: tokens_info['expires_in'] = 315360000 tokens_info['refresh_token'] = '-' Logger.notice(tokens_info) return tokens_info
def start(self): Logger.notice('Service \'%s\' started.' % self.name) monitor = KodiUtils.get_system_monitor() while not self.abort: if monitor.waitForAbort(1): break del monitor del self.provider Logger.notice('Service stopped.')
def get_item(self, driveid, item_driveid=None, item_id=None, path=None, find_subtitles=False, include_download_info=False): self._provider.configure(self._account_manager, driveid) item_driveid = Utils.default(item_driveid, driveid) cache_key = self._addonid + '-drive-' + driveid + '-item_driveid-' + Utils.str( item_driveid) + '-item_id-' + Utils.str( item_id) + '-path-' + Utils.str(path) f = self._cache.get(cache_key) if not f: if item_id: path = item_id elif path == '/': path = '' self._parameters['path'] = path f = self._provider.post('/files/get_metadata', parameters=self._parameters, headers=self._headers) self._cache.set(cache_key, f, expiration=datetime.timedelta(seconds=59)) item = self._extract_item(f, driveid, include_download_info) if find_subtitles: subtitles = [] parent_path = Utils.get_parent_path(item['path_lower']) if parent_path == '/': parent_path = '' self._parameters['path'] = parent_path self._parameters['query'] = urllib.quote( Utils.remove_extension(item['name'])) self._parameters['mode'] = 'filename' del self._parameters['include_media_info'] files = self._provider.post('/files/search', parameters=self._parameters, headers=self._headers) for f in files['matches']: subtitle = self._extract_item(f['metadata'], driveid, include_download_info) if subtitle['name_extension'] == 'srt' or subtitle[ 'name_extension'] == 'sub' or subtitle[ 'name_extension'] == 'sbv': subtitles.append(subtitle) if subtitles: item['subtitles'] = subtitles Logger.notice(item) return item
def get_folder_items(self, item_driveid=None, item_id=None, path=None, on_items_page_completed=None, include_download_info=False): item_driveid = Utils.default(item_driveid, self._driveid) is_album = item_id and item_id[:6] == 'album-' if is_album: item_id = item_id[6:] parameters = self.prepare_parameters() if item_id: parameters['q'] = '\'%s\' in parents' % item_id elif path == 'sharedWithMe' or path == 'starred': parameters['q'] = path elif path != 'photos': if path == '/': parent = self._driveid if self._is_team_drive else 'root' parameters['q'] = '\'%s\' in parents' % parent elif not is_album: item = self.get_item_by_path(path, include_download_info) parameters['q'] = '\'%s\' in parents' % item['id'] parameters[ 'fields'] = 'files(%s),kind,nextPageToken' % self._get_field_parameters( ) if 'q' in parameters: parameters['q'] += ' and not trashed' if path == 'photos': self._photos_provider = GooglePhotos() Logger.notice(self._get_api_url()) self._photos_provider.configure(self._account_manager, self._driveid) files = self._photos_provider.get('/albums') elif is_album: self._photos_provider = GooglePhotos() self._photos_provider.configure(self._account_manager, self._driveid) files = self._photos_provider.post('/mediaItems:search', parameters={'albumId': item_id}) else: self.configure(self._account_manager, self._driveid) files = self.get('/files', parameters=parameters) if self.cancel_operation(): return return self.process_files(files, parameters, on_items_page_completed, include_download_info)
def _slideshow(self, driveid, item_driveid=None, item_id=None, path=None, change_token=None): new_change_token = self.new_change_token_slideshow(change_token, driveid, item_driveid, item_id, path) if self.cancel_operation(): return wait_for_slideshow = False if not change_token or change_token != new_change_token: Logger.notice('Slideshow will start. change_token: %s, new_change_token: %s' % (change_token, new_change_token)) params = {'action':'_list_folder', 'content_type': self._content_type, 'item_driveid': Utils.default(item_driveid, ''), 'item_id': Utils.default(item_id, ''), 'driveid': driveid, 'path' : Utils.default(path, '')} extra_params = ',recursive' if self._addon.getSetting('slideshow_recursive') == 'true' else '' KodiUtils.executebuiltin('SlideShow('+self._addon_url + '?' + urllib.urlencode(params) + extra_params + ')') wait_for_slideshow = True else: Logger.notice('Slideshow child count is the same, nothing to refresh...') t = threading.Thread(target=self._refresh_slideshow, args=(driveid, item_driveid, item_id, path, new_change_token, wait_for_slideshow,)) t.setDaemon(True) t.start()
def handle_exception(ex): stacktrace = ExceptionUtils.full_stacktrace(ex) rex = ExceptionUtils.extract_exception(ex, RequestException) httpex = ExceptionUtils.extract_exception(ex, HTTPError) dnf = ExceptionUtils.extract_exception(ex, DriveNotFoundException) line1 = '' line2 = Utils.unicode(ex) send_report = True log_report = True if rex and rex.response: line1 = Utils.unicode(rex) line2 = ExceptionUtils.extract_error_message(rex.response) if httpex: if httpex.code == 401: send_report = False elif httpex.code == 404: send_report = False log_report = False if dnf: send_report = False log_report = False addonid = KodiUtils.get_addon_info('id') addon_version = KodiUtils.get_addon_info('version') common_addon_version = KodiUtils.get_addon_info( 'version', 'script.module.clouddrive.common') report = '[%s] [%s]/[%s]\n\n%s\n%s\n%s\n\n%s' % ( addonid, addon_version, common_addon_version, line1, line2, '', stacktrace) if rex: report += '\n\n%s\nResponse:\n%s' % (rex.request, rex.response) if log_report: Logger.debug(report) else: Logger.debug(ex) if send_report: Logger.notice(report) Logger.notice('Report sent') ErrorReport.send_report(report)
def start(self): Logger.notice('Service \'%s\' started.' % self.name) self.cleanup_export_map() monitor = KodiUtils.get_system_monitor() startup = True while not self.abort: try: now = datetime.datetime.now() export_map = self.get_scheduled_export_map() if export_map: self.process_schedules(export_map, now, startup) self.process_watch() except Exception as e: ErrorReport.handle_exception(e) startup = False if monitor.waitForAbort(60): break del monitor del self.provider Logger.notice('Service stopped.')
def _refresh_slideshow(self, driveid, item_driveid, item_id, path, change_token, wait_for_slideshow): if wait_for_slideshow: Logger.notice( 'Waiting up to 10 minutes until the slideshow for folder %s starts...' % Utils.default(item_id, path)) max_waiting_time = time.time() + 10 * 60 while not self.cancel_operation( ) and not KodiUtils.get_cond_visibility( 'Slideshow.IsActive') and max_waiting_time > time.time(): if self._system_monitor.waitForAbort(2): break self._print_slideshow_info() interval = self._addon.getSetting('slideshow_refresh_interval') Logger.notice( 'Waiting up to %s minute(s) to check if it is needed to refresh the slideshow of folder %s...' % (interval, Utils.default(item_id, path))) target_time = time.time() + int(interval) * 60 while not self.cancel_operation() and target_time > time.time( ) and KodiUtils.get_cond_visibility('Slideshow.IsActive'): if self._system_monitor.waitForAbort(10): break self._print_slideshow_info() if not self.cancel_operation() and KodiUtils.get_cond_visibility( 'Slideshow.IsActive'): try: self._slideshow(driveid, item_driveid, item_id, path, change_token) except Exception as e: Logger.error( 'Slideshow failed to auto refresh. Will be restarted when possible. Error: ' ) Logger.error(ExceptionUtils.full_stacktrace(e)) self._refresh_slideshow(driveid, item_driveid, item_id, path, None, wait_for_slideshow) else: Logger.notice( 'Slideshow is not running anymore or abort requested.')
def stop(self): if self._server: self._server.shutdown() self._server.server_close() Logger.notice('Service stopped')