def validate_options(self, options, parser): self.channel_names = parse_channel_names(','.join(options.channel)) if len(self.channel_names) == 0: parser.error(_('Please specify at least one valid channel')) from mediacore.model.auth import DBSession, User u = DBSession.query(User).filter_by(user_name=unicode(options.user_name)) if u.count() == 0: parser.error(_('Unknown user "%(user_name)s"') % {'user_name': options.user_name}) self.user = u.one()
def extend_parser(self, parser): parser.add_argument('--publish', dest='publish', action='store_true', default=False, help=_('immediately publish imported videos')) parser.add_argument('--tags', dest='tags', type=unicode, help=_('associate new videos with these tags (comma separated list)')) parser.add_argument('--categories', dest='categories', type=unicode, help=_('associate new videos with these categories (comma separated list)')) parser.add_argument('--user', dest='user_name', default=u'admin', type=unicode, help=_('MediaCore user name for newly created videos (default: "admin")')) parser.add_argument('channel', nargs='+', help=_('YouTube channel name (e.g. "LinuxMagazine")'))
def create_parser(self, extend_parser_callback, config_file=None): if not os.path.isfile(config_file or ''): config_file = None parser = ArgumentParser( # ArgumentParser tries to use string methods but we might have a lazy description=unicode(self.description), add_help=True ) config_help = _('MediaCore config file') if config_file: config_help = _('MediaCore config file (default: "%(file)s")') % {'file': config_file} parser.add_argument('--config', dest='config', default=config_file, help=config_help) self.extend_parser(parser) return parser
def create_parser(self, extend_parser_callback, config_file=None): if not os.path.isfile(config_file or ''): config_file = None parser = ArgumentParser( # ArgumentParser tries to use string methods but we might have a lazy description=unicode(self.description), add_help=True) config_help = _('MediaCore config file') if config_file: config_help = _('MediaCore config file (default: "%(file)s")') % { 'file': config_file } parser.add_argument('--config', dest='config', default=config_file, help=config_help) self.extend_parser(parser) return parser
def import_videos_from_channel(self, channel_name): try: log.debug('importing videos from YouTube channel %r' % channel_name) self._import_channel(channel_name) except RequestError, request_error: exc_data = request_error.args[0] if exc_data['status'] != 403: raise msg_id = _(u'You have exceeded the traffic quota allowed by YouTube. \n' + \ u'While some of the videos have been saved, not all of them were \n' + \ u'imported correctly. Please wait a few minutes and run the \n' + \ u'import again to continue.') raise YouTubeQuotaExceeded(msg_id)
def init(self, locale_map=None): locale = default_locale() or 'en' translator = Translator(locale, locale_map or {}) pylons.translator._push_object(translator) config_file = find_config(default=None) parser = self.create_parser(config_file) options = parser.parse_args() if not options.config: parser.error(_('Please specify a config file using "--config=<filename>"')) init_mediacore(options.config) self.validate_options(options, parser) self.options = options return self
def init(self, locale_map=None): locale = default_locale() or 'en' translator = Translator(locale, locale_map or {}) pylons.translator._push_object(translator) config_file = find_config(default=None) parser = self.create_parser(config_file) options = parser.parse_args() if not options.config: parser.error( _('Please specify a config file using "--config=<filename>"')) init_mediacore(options.config) self.validate_options(options, parser) self.options = options return self
class ImportVideosForm(ListForm): template = 'admin/box-form.html' id = 'settings-form' css_class = 'form' submit_text = None fields = [ ListFieldSet( 'youtube', suppress_label=True, legend='', css_classes=['details_fieldset'], children=[ TextArea( 'channel_names', attrs=dict(rows=3, cols=20), label_text=_('Channel Name(s)'), help_text= _('One or more channel names (separated by commas) to import. Please enter only the channel/user name, not the full URL. Please be aware that it may take several minutes for the import to complete. When all videos have been imported, you will be returned to the Media page to manage your new videos.' ), validator=NotEmpty), CheckBox( 'auto_publish', label_text=_('Publish Videos'), help_text= _('When this is selected, videos are published automatically when they are imported. Otherwise the videos will be added, but will be waiting for review before being published.' )), CategoryCheckBoxList('categories', label_text=_('Categories'), options=lambda: DBSession.query( Category.id, Category.name).all()), TextArea('tags', label_text=_('Tags'), attrs=dict(rows=3, cols=15), help_text=_(u'e.g.: puppies, great dane, adorable')), SubmitButton('save', default=_('Import'), css_classes=['btn', 'btn-save', 'blue', 'f-rgt']), ]) ]
def import_channels(self): from mediacore.model import DBSession # MediaCore uses '.flush()' but due to different DBSession setup this # will not trigger a commit for command line scripts. Treating 'flush()' # as 'commit()' is not 100% right but works well enough here... DBSession.flush = DBSession.commit importer = YouTubeImporter(self.user, self.options.publish, self.options.tags, self.options.categories) print _('Importing...') states = self.load_state(importer) for name in self.channel_names: state = states.get(name, ChannelImportState(importer)) if state.is_complete(): continue state.register() progressbar = CLIProgressReporter(importer, label=' '+name+' ') progressbar.register() try: try: importer.import_videos_from_channel(name) progressbar.done() except YouTubeQuotaExceeded, e: print e.args[0] break finally: progressbar.unregister() state.unregister() states[name] = state if self.is_import_complete(states): print _('Import complete') self.delete_state() return self.save_state(states) print _('Import paused.')
class CommandLineImport(CommandLineTask): description = _('Import YouTube videos into MediaCore') def __init__(self): super(CommandLineImport, self).__init__() self.user = None self.channel_names = () # override from super class def init(self): i18n_dir = os.path.join(os.path.dirname(__file__), '..', 'i18n') super(CommandLineImport, self).init(locale_map={'youtube_import': i18n_dir}) return self # override from super class def extend_parser(self, parser): parser.add_argument('--publish', dest='publish', action='store_true', default=False, help=_('immediately publish imported videos')) parser.add_argument('--tags', dest='tags', type=unicode, help=_('associate new videos with these tags (comma separated list)')) parser.add_argument('--categories', dest='categories', type=unicode, help=_('associate new videos with these categories (comma separated list)')) parser.add_argument('--user', dest='user_name', default=u'admin', type=unicode, help=_('MediaCore user name for newly created videos (default: "admin")')) parser.add_argument('channel', nargs='+', help=_('YouTube channel name (e.g. "LinuxMagazine")')) # override from super class def validate_options(self, options, parser): self.channel_names = parse_channel_names(','.join(options.channel)) if len(self.channel_names) == 0: parser.error(_('Please specify at least one valid channel')) from mediacore.model.auth import DBSession, User u = DBSession.query(User).filter_by(user_name=unicode(options.user_name)) if u.count() == 0: parser.error(_('Unknown user "%(user_name)s"') % {'user_name': options.user_name}) self.user = u.one() def is_import_complete(self, states): for state in states.values(): if state.was_interrupted(): return False return True def state_filename(self): return os.path.join(os.getcwd(), 'youtube-import.state') def delete_state(self): filename = self.state_filename() if os.path.isfile(filename): os.unlink(filename) def load_state(self, importer): filename = self.state_filename() if not os.path.isfile(filename): return {} file_content = file(filename, 'rb').read().decode('utf-8') try: json_states = json.loads(file_content) except ValueError: self.delete_state() return {} states = {} for key, state_data in json_states.items(): states[key] = ChannelImportState.from_json(importer, state_data) return states def save_state(self, states): filename = self.state_filename() json_states = {} for key, state in states.items(): json_states[key] = state.to_json() file(filename, 'wb').write(json.dumps(json_states)) def import_channels(self): from mediacore.model import DBSession # MediaCore uses '.flush()' but due to different DBSession setup this # will not trigger a commit for command line scripts. Treating 'flush()' # as 'commit()' is not 100% right but works well enough here... DBSession.flush = DBSession.commit importer = YouTubeImporter(self.user, self.options.publish, self.options.tags, self.options.categories) print _('Importing...') states = self.load_state(importer) for name in self.channel_names: state = states.get(name, ChannelImportState(importer)) if state.is_complete(): continue state.register() progressbar = CLIProgressReporter(importer, label=' '+name+' ') progressbar.register() try: try: importer.import_videos_from_channel(name) progressbar.done() except YouTubeQuotaExceeded, e: print e.args[0] break finally: progressbar.unregister() state.unregister() states[name] = state if self.is_import_complete(states): print _('Import complete') self.delete_state() return self.save_state(states) print _('Import paused.')