def do_GET(self):
        Logger.debug(self.path)
        data = self.path.split('/')
        code = 307
        headers = {}
        content = Utils.get_file_buffer()
        if len(data) > 4 and data[1] == self.server.service.name:
            try:
                driveid = data[2]
                provider = self.server.data()
                account_manager = AccountManager(
                    self.server.service.profile_path)
                provider.configure(account_manager, driveid)
                item = provider.get_item(item_driveid=data[3],
                                         item_id=data[4],
                                         include_download_info=True)
                headers['location'] = item['download_info']['url']
            except Exception as e:
                httpex = ExceptionUtils.extract_exception(e, HTTPError)
                if httpex:
                    code = httpex.code
                else:
                    code = 500

                ErrorReport.handle_exception(e)
                content.write(ExceptionUtils.full_stacktrace(e))
        else:
            code = 404
        self.write_response(code, content=content, headers=headers)
    def run_export(self, export):
        exporting = Utils.get_safe_value(export, 'exporting', False)
        Logger.debug('Run export requested. Exporting = %s' % (exporting, ))
        if not exporting:
            export['exporting'] = True
            export['origin'] = 'schedule'
            self.export_manager.save_export(export)
            try:
                self._export_progress_dialog_bg.create(
                    self._addon_name + ' ' +
                    self._common_addon.getLocalizedString(32024),
                    self._common_addon.getLocalizedString(32025))
                export_folder = export['destination_folder']
                if not KodiUtils.file_exists(export_folder):
                    Logger.debug('creating folder: %s' % (export_folder, ))
                    if not KodiUtils.mkdirs(export_folder):
                        Logger.debug('unable to create folder %s' %
                                     (export_folder, ))
                if KodiUtils.file_exists(export_folder):
                    driveid = export['driveid']
                    self.provider.configure(self._account_manager, driveid)
                    exportid = export['id']
                    items_info = {}
                    ExportManager.add_item_info(items_info, 'root-folder',
                                                None, export_folder, None,
                                                'folder')
                    self.export_manager.save_items_info(exportid, items_info)
                    item = self.provider.get_item(export['item_driveid'],
                                                  exportid)
                    item.update({
                        'parent': 'root-folder',
                        'origin': 'schedule'
                    })
                    self.export_manager.save_pending_changes(
                        exportid, deque([item]))
                    self.export_manager.save_retry_changes(exportid, deque([]))
                    self.process_pending_changes(
                        exportid,
                        on_before_change=self._show_progress_before_change)
                    if Utils.get_safe_value(export, 'update_library', False):
                        if Utils.get_safe_value(export, 'content_type',
                                                '') == 'audio':
                            KodiUtils.update_library('music')
                        else:
                            KodiUtils.update_library('video')

            except Exception as e:
                ErrorReport.handle_exception(e)
                KodiUtils.show_notification(
                    self._common_addon.getLocalizedString(32027) + ' ' +
                    Utils.unicode(e))
            finally:
                export['exporting'] = False
                del export['origin']
                self.export_manager.save_export(export)
                self._export_progress_dialog_bg.close()
        else:
            KodiUtils.show_notification(
                self._common_addon.getLocalizedString(32059) + ' ' +
                self._common_addon.getLocalizedString(32038))
Esempio n. 3
0
 def create_text_file(file_path, content):
     try:
         with KodiUtils.file(file_path, 'w') as f:
             f.write(Utils.str(content))
     except Exception as e:
         ErrorReport.handle_exception(e)
         return False
     return True
 def create_text_file(file_path, content):
     f = None
     try:
         f = KodiUtils.file(file_path, 'w')
         f.write(Utils.str(content))
     except Exception as e:
         ErrorReport.handle_exception(e)
         return False
     finally:
         if f:
             f.close()
     return True
    def download(item, download_path, provider, on_update_download=None):
        url = item['download_info']['url']
        headers = None
        if provider.download_requires_auth:
            headers = {"Authorization":"Bearer %s"%provider.get_access_tokens()['access_token']}
        try:
            req = Request(url, None, headers, download_path = download_path, on_update_download = on_update_download)
            req.request()
        except Exception as e:
            ErrorReport.handle_exception(e)
            return False
        return req.success

    
                
Esempio n. 6
0
 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 get_subtitles(self):
     try:
         from clouddrive.common.remote.request import Request
         from clouddrive.common.service.download import DownloadServiceUtil
         response = Request(self.getPlayingFile() + '?subtitles',
                            None).request_json()
         if response and 'driveid' in response and 'subtitles' in response:
             driveid = response['driveid']
             subtitles = response['subtitles']
             for subtitle in subtitles:
                 url = DownloadServiceUtil.build_download_url(
                     driveid,
                     Utils.default(
                         Utils.get_safe_value(subtitle, 'drive_id'),
                         driveid), subtitle['id'],
                     urllib.quote(Utils.str(subtitle['name'])))
                 Logger.debug('subtitle: %s' % url)
                 self.setSubtitles(url)
     except Exception as e:
         Logger.error(e)
         from clouddrive.common.remote.errorreport import ErrorReport
         ErrorReport.handle_exception(e)
    def do_GET(self):
        Logger.debug(self.path + ': Requested')
        if self._system_monitor.abortRequested():
            Logger.debug(self.path + ': abort requested')
            return
        data = self.path.split('/')
        size = len(data)
        cached_page = self._page_cache.get(self.path)
        if cached_page:
            if cached_page['pending']:
                Logger.debug(self.path + ': Already requested. Waiting for original request...')
                max_waiting_time = time.time() + 30
                while not self._system_monitor.abortRequested() and max_waiting_time > time.time() and cached_page['pending']:
                    if self._system_monitor.waitForAbort(1):
                        break
                    cached_page = self._page_cache.get(self.path)

            if not self._system_monitor.abortRequested():
                if cached_page['pending']:
                    self.write_response(504)
                    Logger.debug(self.path + ': 504 - Gateway timeout')
                    self._page_cache.remove(self.path)
                else:
                    if 'content' in cached_page and cached_page['content']:
                        content = Utils.get_file_buffer()
                        content.write(cached_page['content'])
                        cached_page['content'] = content
                    self.write_response(cached_page['response_code'], content=Utils.get_safe_value(cached_page, 'content'), headers=Utils.get_safe_value(cached_page, 'headers', {}))
                    Logger.debug(self.path + ': %d - Served from cache' % cached_page['response_code'])
        else:
            cached_page = {'pending': True}
            self._page_cache.set(self.path, cached_page)
            if size > 1 and data[1] == self.server.service.name:
                try:
                    if size == 2:
                        cached_page['response_code'] = 303
                        cached_page['headers'] = {'location': self.path + '/'}
                    elif size > 2 and data[2]:
                        cached_page = self.handle_resource_request(data)
                    else:
                        cached_page = self.show_addon_list()
                except Exception as e:
                    httpex = ExceptionUtils.extract_exception(e, HTTPError)
                    if httpex:
                        cached_page['response_code'] = httpex.code
                    else:
                        cached_page['response_code'] = 500
                    
                    ErrorReport.handle_exception(e)
                    content = Utils.get_file_buffer()
                    content.write(ExceptionUtils.full_stacktrace(e))
                    
                    cached_page['content'] = content
            else:
                cached_page['response_code'] = 404
            cached_page['pending'] = False
            content_value = None
            if 'content' in cached_page:
                content_value = cached_page['content'].getvalue()
            self.write_response(cached_page['response_code'], content=Utils.get_safe_value(cached_page, 'content'), headers=Utils.get_safe_value(cached_page, 'headers', {}))
            cached_page['content'] = content_value
            if Utils.get_safe_value(cached_page, 'response_code', 0) >= 500:
                self._page_cache.remove(self.path)
            else:
                self._page_cache.set(self.path, cached_page)
            Logger.debug(self.path + ': Response code ' + Utils.str(cached_page['response_code']))
Esempio n. 9
0
 def process_watch(self):
     exports = self.export_manager.get_exports()
     update_library = {}
     changes_by_drive = {}
     for exportid in exports:
         export = exports[exportid]
         watch = Utils.get_safe_value(export, 'watch', False)
         exporting = Utils.get_safe_value(export, 'exporting', False)
         retry_changes = self.export_manager.get_retry_changes(exportid)
         if (watch or len(retry_changes) > 0) and not exporting:
             items_info = self.export_manager.get_items_info(exportid)
             if items_info:
                 export['exporting'] = True
                 export['origin'] = 'watch'
                 self.export_manager.save_export(export)
                 try:
                     driveid = export['driveid']
                     if driveid in changes_by_drive:
                         changes = changes_by_drive[driveid]
                     else:
                         self.provider.configure(self._account_manager,
                                                 export['driveid'])
                         changes = self.provider.changes()
                         changes_by_drive[driveid] = []
                         changes_by_drive[driveid].extend(changes)
                     pending_changes = self.export_manager.get_pending_changes(
                         exportid)
                     pending_changes.extend(retry_changes)
                     pending_changes.extend(changes)
                     if len(changes) > 0 or len(retry_changes) > 0:
                         self.export_manager.save_pending_changes(
                             exportid, pending_changes)
                     if len(retry_changes) > 0:
                         self.export_manager.save_retry_changes(
                             exportid, deque([]))
                     show_export_progress = KodiUtils.get_addon_setting(
                         'hide_export_progress') != 'true'
                     if pending_changes and show_export_progress:
                         self._export_progress_dialog_bg.update(
                             0, self._addon_name + ' ' +
                             self._common_addon.getLocalizedString(32088),
                             self._common_addon.getLocalizedString(32025))
                     if show_export_progress:
                         progress_listener = self._show_progress_after_change
                     else:
                         progress_listener = None
                     changes_done = self.process_pending_changes(
                         exportid, on_after_change=progress_listener)
                     if changes_done:
                         if Utils.get_safe_value(export, 'update_library',
                                                 False):
                             update_library[Utils.get_safe_value(
                                 export, 'content_type', 'None')] = True
                     for change in changes_done:
                         if change in changes_by_drive[driveid]:
                             changes_by_drive[driveid].remove(change)
                 except Exception as e:
                     ErrorReport.handle_exception(e)
                     KodiUtils.show_notification(
                         self._common_addon.getLocalizedString(32027) +
                         ' ' + Utils.unicode(e))
                 finally:
                     export['exporting'] = False
                     del export['origin']
                     self.export_manager.save_export(export)
             else:
                 self.run_export(export)
     self._export_progress_dialog_bg.close()
     if update_library:
         if Utils.get_safe_value(update_library, 'video', False):
             KodiUtils.update_library('video')
         if Utils.get_safe_value(update_library, 'audio', False):
             KodiUtils.update_library('music')