def _grab_file_url(url, callback, errback, default_mime_type): path = download_utils.get_file_url_path(url) try: f = file(path) except EnvironmentError: eventloop.add_idle(errback, 'grab file url errback', args=(FileURLNotFoundError(path), )) else: try: data = f.read() except IOError: eventloop.add_idle(errback, 'grab file url errback', args=(FileURLReadError(path), )) else: info = { "body": data, "updated-url": url, "redirected-url": url, "content-type": default_mime_type, } eventloop.add_idle(callback, 'grab file url callback', args=(info, ))
def get_downloader_for_item(item): existing = get_existing_downloader(item) if existing: return existing url = item.get_url() existing = get_existing_downloader_by_url(url) if existing: return existing channel_name = unicode_to_filename(item.get_channel_title(True)) if not channel_name: channel_name = None if url.startswith(u'file://'): path = get_file_url_path(url) try: get_torrent_info_hash(path) except ValueError: raise ValueError("Don't know how to handle %s" % url) except (OSError, IOError): return None else: return RemoteDownloader(url, item, u'application/x-bittorrent', channel_name=channel_name) elif is_magnet_uri(url): return RemoteDownloader(url, item, u'application/x-magnet') else: return RemoteDownloader(url, item, channel_name=channel_name)
def get_metainfo(self): if self.metainfo is None: if self.url.startswith('file://'): path = get_file_url_path(self.url) try: metainfoFile = open(path, 'rb') except IOError: self.handle_error( _("Torrent file deleted"), _("The torrent file for this item was deleted " "outside of %(appname)s.", {"appname": app.config.get(prefs.SHORT_APP_NAME)} )) return try: metainfo = metainfoFile.read() finally: metainfoFile.close() self.handle_metainfo(metainfo) else: self.description_client = httpclient.grab_url(self.url, self.on_metainfo_download, self.on_metainfo_download_error, content_check_callback=self.check_description) else: self.got_metainfo()
def get_filename(self): self.dbItem.confirm_db_thread() if self.url and self.url.startswith(u"file://"): return get_file_url_path(self.url) elif self.url and self.url.startswith(u"/"): return unicode_to_filename(self.url) else: return self.filename
def _grab_file_url(url, callback, errback, default_mime_type): path = download_utils.get_file_url_path(url) try: f = file(path) except EnvironmentError: eventloop.add_idle(errback, 'grab file url errback', args=(FileURLNotFoundError(path),)) else: try: data = f.read() except IOError: eventloop.add_idle(errback, 'grab file url errback', args=(FileURLReadError(path),)) else: info = {"body": data, "updated-url":url, "redirected-url":url, "content-type": default_mime_type, } eventloop.add_idle(callback, 'grab file url callback', args=(info,))
def parse_command_line_args(args): """ This goes through a list of files which could be arguments passed in on the command line or a list of files from other source. """ if not _started_up: _command_line_args.extend(args) return for i in xrange(len(args)): if args[i].startswith('file://'): args[i] = args[i][len('file://'):] reset_command_line_view() added_videos = False added_downloads = False for arg in args: if arg.startswith('file://'): arg = download_utils.get_file_url_path(arg) elif arg.startswith('miro:'): add_subscription_url('miro:', 'application/x-miro', arg) elif arg.startswith('democracy:'): add_subscription_url('democracy:', 'application/x-democracy', arg) elif (arg.startswith('http:') or arg.startswith('https:') or arg.startswith('feed:') or arg.startswith('feeds:') or is_magnet_uri(arg)): singleclick.add_download(filename_to_unicode(arg)) elif os.path.exists(arg): ext = os.path.splitext(arg)[1].lower() if ext in ('.torrent', '.tor'): try: torrent_infohash = get_torrent_info_hash(arg) except ValueError: title = _("Invalid Torrent") msg = _( "The torrent file %(filename)s appears to be corrupt " "and cannot be opened.", {"filename": os.path.basename(arg)} ) dialogs.MessageBoxDialog(title, msg).run() continue except (IOError, OSError): title = _("File Error") msg = _( "The torrent file %(filename)s could not be opened. " "Please ensure it exists and you have permission to " "access this file.", {"filename": os.path.basename(arg)} ) dialogs.MessageBoxDialog(title, msg).run() continue add_torrent(arg, torrent_infohash) added_downloads = True elif ext in ('.rss', '.rdf', '.atom', '.ato'): feed.add_feed_from_file(arg) elif ext in ('.miro', '.democracy', '.dem', '.opml'): opml.Importer().import_subscriptions(arg, show_summary=False) else: add_video(arg) added_videos = True else: logging.warning("parse_command_line_args: %s doesn't exist", arg) # if the user has Miro set up to play all videos externally, then # we don't want to play videos added by the command line. # # this fixes bug 12362 where if the user has his/her system set up # to use Miro to play videos and Miro goes to play a video # externally, then it causes an infinite loop and dies. if added_videos and app.config.get(prefs.PLAY_IN_MIRO): item_infos = [itemsource.DatabaseItemSource._item_info_for(i) for i in _command_line_videos] messages.PlayMovie(item_infos).send_to_frontend() if added_downloads: # FIXME - switch to downloads tab? pass
def parse_command_line_args(args): """ This goes through a list of files which could be arguments passed in on the command line or a list of files from other source. """ if not _started_up: _command_line_args.extend(args) return for i in xrange(len(args)): if args[i].startswith('file://'): args[i] = args[i][len('file://'):] reset_command_line_view() added_videos = False added_downloads = False for arg in args: if arg.startswith('file://'): arg = download_utils.get_file_url_path(arg) elif arg.startswith('miro:'): add_subscription_url('miro:', 'application/x-miro', arg) elif arg.startswith('democracy:'): add_subscription_url('democracy:', 'application/x-democracy', arg) elif (arg.startswith('http:') or arg.startswith('https:') or arg.startswith('feed:') or arg.startswith('feeds:') or is_magnet_uri(arg)): singleclick.add_download(filename_to_unicode(arg)) elif os.path.exists(arg): ext = os.path.splitext(arg)[1].lower() if ext in ('.torrent', '.tor'): try: torrent_infohash = get_torrent_info_hash(arg) except ValueError: title = _("Invalid Torrent") msg = _( "The torrent file %(filename)s appears to be corrupt " "and cannot be opened.", {"filename": os.path.basename(arg)}) dialogs.MessageBoxDialog(title, msg).run() continue except (IOError, OSError): title = _("File Error") msg = _( "The torrent file %(filename)s could not be opened. " "Please ensure it exists and you have permission to " "access this file.", {"filename": os.path.basename(arg)}) dialogs.MessageBoxDialog(title, msg).run() continue add_torrent(arg, torrent_infohash) added_downloads = True elif ext in ('.rss', '.rdf', '.atom', '.ato'): feed.add_feed_from_file(arg) elif ext in ('.miro', '.democracy', '.dem', '.opml'): opml.Importer().import_subscriptions(arg, show_summary=False) else: add_video(arg) added_videos = True else: logging.warning("parse_command_line_args: %s doesn't exist", arg) # if the user has Miro set up to play all videos externally, then # we don't want to play videos added by the command line. # # this fixes bug 12362 where if the user has his/her system set up # to use Miro to play videos and Miro goes to play a video # externally, then it causes an infinite loop and dies. if added_videos and app.config.get(prefs.PLAY_IN_MIRO): item_ids = [i.id for i in _command_line_videos] item_infos = app.db.fetch_item_infos(item_ids) messages.PlayMovies(item_infos).send_to_frontend() if added_downloads: # FIXME - switch to downloads tab? pass