def save_media_obj(self, name, email, title, description, tags, uploaded_file, url): # create our media object as a status-less placeholder initially media_obj = Media() media_obj.author = Author(name, email) media_obj.title = title media_obj.slug = get_available_slug(Media, title) media_obj.description = description if request.settings['wording_display_administrative_notes']: media_obj.notes = request.settings['wording_administrative_notes'] media_obj.set_tags(tags) # Give the Media object an ID. DBSession.add(media_obj) DBSession.flush() # Create a MediaFile object, add it to the media_obj, and store the file permanently. media_file = add_new_media_file(media_obj, file=uploaded_file, url=url) # The thumbs may have been created already by add_new_media_file if not has_thumbs(media_obj): create_default_thumbs_for(media_obj) media_obj.update_status() DBSession.flush() return media_obj
def _import_video(self, entry): player_url = self._player_url_from_entry(entry) if not player_url: log.debug('Video Feed Error: No player URL? %s' % entry) return None if self._has_media_file_for(player_url): return None media = fetch_row(Media, u'new') media.author = Author(self.user.display_name, self.user.email_address) media.reviewed = True media.title = unicode(entry.media.title.text, "utf-8") if entry.media.description.text: encoded_description = unicode(entry.media.description.text, "utf-8") media.description = clean_xhtml(encoded_description) media.slug = get_available_slug(Media, media.title, media) if self.tags: media.set_tags(unicode(self.tags)) if self.categories: media.set_categories(self.categories) try: media_file = add_new_media_file(media, url=player_url) except StorageError, e: log.debug('Video Feed Error: Error storing video: %s at %s' \ % (e.message, player_url)) return None
def podcast_from_feed(d, tags=False, save_files=False): # Assume not explicit explicit = False if 'itunes_explicit' in d['feed']: explicit = bool(d['feed']['itunes_explicit']) image = None if 'image' in d['feed']: image = d['feed']['image']['href'] title = u'' if 'title' in d['feed']: title = d['feed']['title'] description = u'' if 'summary' in d['feed']: description = d['feed']['summary'] subtitle = u'' if 'subtitle' in d['feed']: subtitle = d['feed']['subtitle'] slug = slugify(title) author_name = u"PLACEHOLDER NAME" author_email = u"*****@*****.**" podcast = Podcast() podcast.slug = get_available_slug(Podcast, slug, podcast) podcast.title = title podcast.subtitle = subtitle podcast.author = Author(author_name, author_email) podcast.description = description podcast.explicit = explicit DBSession.add(podcast) DBSession.flush() # Create thumbs from image, or default thumbs created_images = False if image: temp_imagefile = tempfile.TemporaryFile() imagefile = urllib2.urlopen(image) temp_imagefile.write(imagefile.read()) temp_imagefile.seek(0) filename = urlparse.urlparse(image)[2] create_thumbs_for(podcast, temp_imagefile, filename) created_images = True if not created_images: create_default_thumbs_for(podcast) # Now add all of the entries for entry in d['entries']: media = media_from_entry(entry, tags, save_files) media.podcast = podcast return podcast
def save(self, id, slug, title, author_name, author_email, description, notes, podcast, tags, categories, delete=None, **kwargs): """Save changes or create a new :class:`~mediacore.model.media.Media` instance. Form handler the :meth:`edit` action and the :class:`~mediacore.forms.admin.media.MediaForm`. Redirects back to :meth:`edit` after successful editing and :meth:`index` after successful deletion. """ media = fetch_row(Media, id) if delete: self._delete_media(media) DBSession.commit() redirect(action='index', id=None) if not slug: slug = title elif slug.startswith('_stub_'): slug = slug[len('_stub_'):] if slug != media.slug: media.slug = get_available_slug(Media, slug, media) media.title = title media.author = Author(author_name, author_email) media.description = description media.notes = notes media.podcast_id = podcast media.set_tags(tags) media.set_categories(categories) media.update_status() DBSession.add(media) DBSession.flush() if id == 'new' and not has_thumbs(media): create_default_thumbs_for(media) if request.is_xhr: status_form_xhtml = unicode(update_status_form.display( action=url_for(action='update_status', id=media.id), media=media)) return dict( media_id = media.id, values = {'slug': slug}, link = url_for(action='edit', id=media.id), status_form = status_form_xhtml, ) else: redirect(action='edit', id=media.id)
def save(self, id, slug, title, subtitle, author_name, author_email, description, details, feed, delete=None, **kwargs): """Save changes or create a new :class:`~mediacore.model.podcasts.Podcast` instance. Form handler the :meth:`edit` action and the :class:`~mediacore.forms.admin.podcasts.PodcastForm`. Redirects back to :meth:`edit` after successful editing and :meth:`index` after successful deletion. """ podcast = fetch_row(Podcast, id) if delete: file_paths = thumb_paths(podcast).values() DBSession.delete(podcast) DBSession.commit() helpers.delete_files(file_paths, Podcast._thumb_dir) redirect(action='index', id=None) if not slug: slug = title if slug != podcast.slug: podcast.slug = get_available_slug(Podcast, slug, podcast) podcast.title = title podcast.subtitle = subtitle podcast.author = Author(author_name, author_email) podcast.description = description podcast.copyright = details['copyright'] podcast.category = details['category'] podcast.itunes_url = feed['itunes_url'] podcast.feedburner_url = feed['feedburner_url'] podcast.explicit = { 'yes': True, 'clean': False }.get(details['explicit'], None) if id == 'new': DBSession.add(podcast) DBSession.flush() create_default_thumbs_for(podcast) redirect(action='edit', id=podcast.id)
def createMediaItem(self, title, author_email=None, author_name=None, slug=None, tags=None, podcast_id=None, category_ids=None, meta=None, **kwargs): mediaItem = Media() log.info("createMediaItem({title})".format(title=title)) if not slug: slug = title elif slug.startswith('_stub_'): slug = slug[len('_stub_'):] if slug != mediaItem.slug: mediaItem.slug = get_available_slug(Media, slug, mediaItem) if podcast_id: podcast_id = int(podcast_id) else: podcast_id = 0 if not meta: meta = {} else: try: meta = json.loads(meta) except Exception as e: return { "success": False, "message": "Invalid JSON object given for `meta`" } mediaItem.title = title mediaItem.author = Author(author_name or "No Author", author_email or "No Email") mediaItem.podcast_id = podcast_id or None mediaItem.set_tags(tags) mediaItem.set_categories(category_ids) mediaItem.update_status() mediaItem.meta = meta DBSession.add(mediaItem) DBSession.flush() return {"success": True, "id": mediaItem.id}
def _new_publishable_media(self, slug, name): from datetime import datetime from mediacore.model import Author, Media media = Media() media.slug = slug media.title = name media.subtitle = None media.description = u"""<p>Description</p>""" media.description_plain = u"""Description""" media.author = Author(u'fake name', u'*****@*****.**') media.publish_on = datetime.now() media.publishable = True media.reviewed = True media.encoded = False media.type = None return media
def save_thumb(self, id, thumb, **kwargs): """Save a thumbnail uploaded with :class:`~mediacore.forms.admin.ThumbForm`. :param id: Media ID. If ``"new"`` a new Media stub is created. :type id: ``int`` or ``"new"`` :param file: The uploaded file :type file: :class:`cgi.FieldStorage` or ``None`` :rtype: JSON dict :returns: success bool message Error message, if unsuccessful id The :attr:`~mediacore.model.media.Media.id` which is important if a new media has just been created. """ if id == 'new': media = Media() user = req.perm.user media.author = Author(user.display_name, user.email_address) media.title = os.path.basename(thumb.filename) media.slug = get_available_slug(Media, '_stub_' + media.title) DBSession.add(media) DBSession.flush() else: media = fetch_row(Media, id) try: # Create JPEG thumbs create_thumbs_for(media, thumb.file, thumb.filename) success = True message = None except IOError, e: success = False if id == 'new': DBSession.delete(media) if e.errno == 13: message = _('Permission denied, cannot write file') elif e.message == 'cannot identify image file': message = _('Unsupported image type: %s') \ % os.path.splitext(thumb.filename)[1].lstrip('.') elif e.message == 'cannot read interlaced PNG files': message = _('Interlaced PNGs are not supported.') else: raise
def test_can_create_example_media(self): media = Media.example() assert_not_none(media.id) assert_equals(u'Foo Media', media.title) assert_equals(u'foo-media', media.slug) assert_equals(Author(u'Joe', u'*****@*****.**'), media.author) assert_length(0, media.files) assert_none(media.type) assert_none(media.podcast_id) assert_false(media.publishable) assert_false(media.reviewed) assert_false(media.encoded) assert_none(media.publish_on) assert_none(media.publish_until) assert_false(media.is_published)
def add_file(self, id, file=None, url=None, **kwargs): """Save action for the :class:`~mediacore.forms.admin.media.AddFileForm`. Creates a new :class:`~mediacore.model.media.MediaFile` from the uploaded file or the local or remote URL. :param id: Media ID. If ``"new"`` a new Media stub is created. :type id: :class:`int` or ``"new"`` :param file: The uploaded file :type file: :class:`cgi.FieldStorage` or ``None`` :param url: A URL to a recognizable audio or video file :type url: :class:`unicode` or ``None`` :rtype: JSON dict :returns: success bool message Error message, if unsuccessful media_id The :attr:`~mediacore.model.media.Media.id` which is important if new media has just been created. file_id The :attr:`~mediacore.model.media.MediaFile.id` for the newly created file. edit_form The rendered XHTML :class:`~mediacore.forms.admin.media.EditFileForm` for this file. status_form The rendered XHTML :class:`~mediacore.forms.admin.media.UpdateStatusForm` """ if id == 'new': media = Media() user = request.environ['repoze.who.identity']['user'] media.author = Author(user.display_name, user.email_address) # Create a temp stub until we can set it to something meaningful timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') media.title = u'Temporary stub %s' % timestamp media.slug = get_available_slug(Media, '_stub_' + timestamp) media.reviewed = True DBSession.add(media) DBSession.flush() else: media = fetch_row(Media, id) media_file = add_new_media_file(media, file, url) if media.slug.startswith('_stub_'): media.title = media_file.display_name media.slug = get_available_slug(Media, '_stub_' + media.title) # The thumbs may have been created already by add_new_media_file if id == 'new' and not has_thumbs(media): create_default_thumbs_for(media) media.update_status() # Render some widgets so the XHTML can be injected into the page edit_form_xhtml = unicode(edit_file_form.display( action=url_for(action='edit_file', id=media.id), file=media_file)) status_form_xhtml = unicode(update_status_form.display( action=url_for(action='update_status', id=media.id), media=media)) data = dict( success = True, media_id = media.id, file_id = media_file.id, file_type = media_file.type, edit_form = edit_form_xhtml, status_form = status_form_xhtml, title = media.title, slug = media.slug, description = media.description, link = url_for(action='edit', id=media.id), duration = helpers.duration_from_seconds(media.duration), ) return data
def add_default_data(): log.info('Adding default data') settings = [ (u'email_media_uploaded', None), (u'email_comment_posted', None), (u'email_support_requests', None), (u'email_send_from', u'noreply@localhost'), (u'wording_user_uploads', N_(u"Upload your media using the form below. We'll review it and get back to you." )), (u'wording_administrative_notes', None), (u'wording_display_administrative_notes', u''), (u'popularity_decay_exponent', u'4'), (u'popularity_decay_lifetime', u'36'), (u'rich_text_editor', u'tinymce'), (u'google_analytics_uacct', u''), (u'featured_category', u'1'), (u'max_upload_size', u'314572800'), (u'ftp_storage', u'false'), (u'ftp_server', u'ftp.someserver.com'), (u'ftp_user', u'username'), (u'ftp_password', u'password'), (u'ftp_upload_directory', u'media'), (u'ftp_download_url', u'http://www.someserver.com/web/accessible/media/'), (u'ftp_upload_integrity_retries', u'10'), (u'akismet_key', u''), (u'akismet_url', u''), (u'req_comment_approval', u''), (u'use_embed_thumbnails', u'true'), (u'api_secret_key_required', u'true'), (u'api_secret_key', random_string(20)), (u'api_media_max_results', u'50'), (u'api_tree_max_depth', u'10'), (u'general_site_name', u'MediaCore'), (u'general_site_title_display_order', u'prepend'), (u'sitemaps_display', u'True'), (u'rss_display', u'True'), (u'vulgarity_filtered_words', u''), (u'primary_language', u'en'), (u'advertising_banner_html', u''), (u'advertising_sidebar_html', u''), (u'comments_engine', u'mediacore'), (u'facebook_appid', u''), ] settings.extend(appearance_settings) for key, value in settings: s = Setting() s.key = key s.value = value DBSession.add(s) admin_user = User() admin_user.user_name = u'admin' admin_user.display_name = u'Admin' admin_user.email_address = u'*****@*****.**' admin_user.password = u'admin' DBSession.add(admin_user) admin_group = Group() admin_group.group_name = u'admins' admin_group.display_name = u'Admins' admin_group.users.append(admin_user) DBSession.add(admin_group) editor_group = Group() editor_group.group_name = u'editors' editor_group.display_name = u'Editors' DBSession.add(editor_group) admin_perm = Permission() admin_perm.permission_name = u'admin' admin_perm.description = u'Grants access to the admin panel' admin_perm.groups.append(admin_group) DBSession.add(admin_perm) edit_perm = Permission() edit_perm.permission_name = u'edit' edit_perm.description = u'Grants access to edit site content' edit_perm.groups.append(admin_group) edit_perm.groups.append(editor_group) DBSession.add(edit_perm) category = Category() category.name = u'Featured' category.slug = u'featured' DBSession.add(category) category2 = Category() category2.name = u'Instructional' category2.slug = u'instructional' DBSession.add(category2) podcast = Podcast() podcast.slug = u'hello-world' podcast.title = u'Hello World' podcast.subtitle = u'My very first podcast!' podcast.description = u"""<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>""" podcast.category = u'Technology' podcast.author = Author(admin_user.display_name, admin_user.email_address) podcast.explicit = None podcast.copyright = u'Copyright 2009 Xyz' podcast.itunes_url = None podcast.feedburner_url = None DBSession.add(podcast) comment = Comment() comment.subject = u'Re: New Media' comment.author = AuthorWithIP(name=u'John Doe', ip=2130706433) comment.body = u'<p>Hello to you too!</p>' DBSession.add(comment) media = Media() media.type = None media.slug = u'new-media' media.reviewed = True media.encoded = False media.publishable = False media.title = u'New Media' media.subtitle = None media.description = u"""<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>""" media.description_plain = u"""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.""" media.author = Author(admin_user.display_name, admin_user.email_address) media.categories.append(category) media.comments.append(comment) DBSession.add(media) #XXX The list of default players is actually defined in model.players # and should at some point be moved here to avoid inconsistency # between the default storage engines and default players. remote_url_storage = RemoteURLStorage() default_engines = [ LocalFileStorage(), remote_url_storage, YoutubeStorage(), VimeoStorage(), BlipTVStorage(), DailyMotionStorage(), GoogleVideoStorage(), ] for engine in default_engines: DBSession.add(engine) import datetime instructional_media = [ ( u'workflow-in-mediacore', u'Workflow in MediaCore', u'<p>This sceencast explains the publish status feature in MediaCore.</p><p>Initially all videos uploaded through the front-end or admin panel are placed under "awaiting review" status. Once the administrator hits the "review complete" button, they can upload media. Videos can be added in any format, however, they can only be published if they are in a web-ready format such as FLV, M4V, MP3, or MP4. Alternatively, if they are published through Youtube or Vimeo the encoding step is skipped</p><p>Once uploaded and encoded the administrator can then publish the video.</p>', u'This sceencast explains the publish status feature in MediaCore.\nInitially all videos uploaded through the front-end or admin panel are placed under \"awaiting review\" status. Once the administrator hits the \"review complete\" button, they can upload media. Videos can be added in any format, however, they can only be published if they are in a web-ready format such as FLV, M4V, MP3, or MP4. Alternatively, if they are published through Youtube or Vimeo the encoding step is skipped\nOnce uploaded and encoded the administrator can then publish the video.', datetime.datetime(2010, 5, 13, 2, 29, 40), 218, u'http://getmediacore.com/files/tutorial-workflow-in-mediacore.mp4', u'video', u'mp4', ), ( u'creating-a-podcast-in-mediacore', u'Creating a Podcast in MediaCore', u'<p>This describes the process an administrator goes through in creating a podcast in MediaCore. An administrator can enter information that will automatically generate the iTunes/RSS feed information. Any episodes published to a podcast will automatically publish to iTunes/RSS.</p>', u'This describes the process an administrator goes through in creating a podcast in MediaCore. An administrator can enter information that will automatically generate the iTunes/RSS feed information. Any episodes published to a podcast will automatically publish to iTunes/RSS.', datetime.datetime(2010, 5, 13, 2, 33, 44), 100, u'http://getmediacore.com/files/tutorial-create-podcast-in-mediacore.mp4', u'video', u'mp4', ), ( u'adding-a-video-in-mediacore', u'Adding a Video in MediaCore', u'<p>This screencast shows how video or audio can be added in MediaCore.</p><p>MediaCore supports a wide range of formats including (but not limited to): YouTube, Vimeo, Google Video, Amazon S3, Bits on the Run, BrightCove, Kaltura, and either your own server or someone else\'s.</p><p>Videos can be uploaded in any format, but can only be published in web-ready formats such as FLV, MP3, M4V, MP4 etc.</p>', u'This screencast shows how video or audio can be added in MediaCore.\nMediaCore supports a wide range of formats including (but not limited to): YouTube, Vimeo, Google Video, Amazon S3, Bits on the Run, BrightCove, Kaltura, and either your own server or someone else\'s.\nVideos can be uploaded in any format, but can only be published in web-ready formats such as FLV, MP3, M4V, MP4 etc.', datetime.datetime(2010, 5, 13, 02, 37, 36), 169, u'http://getmediacore.com/files/tutorial-add-video-in-mediacore.mp4', u'video', u'mp4', ), ] name = u'MediaCore Team' email = u'*****@*****.**' for slug, title, desc, desc_plain, publish_on, duration, url, type_, container in instructional_media: media = Media() media.author = Author(name, email) media.description = desc media.description_plain = desc_plain media.duration = duration media.publish_on = publish_on media.slug = slug media.title = title media.type = type_ media_file = MediaFile() media_file.container = container media_file.created_on = publish_on media_file.display_name = os.path.basename(url) media_file.duration = duration media_file.type = type_ media_file.storage = remote_url_storage media_file.unique_id = url DBSession.add(media) DBSession.add(media_file) media.files.append(media_file) media.categories.append(category2) media.encoded = True media.reviewed = True media.publishable = True
def media_from_entry(e, tags=False, save_files=False): # Get tags as a list of unicode objects. tags = [t['term'] for t in e['tags']] # Assume not explicit. explicit = 0 if 'itunes_explicit' in e: explicit = e['itunes_explicit'] # Find the duration, if it exists duration = u'' if 'itunes_duration' in e: try: duration = e['itunes_duration'] duration = duration_to_seconds(duration) except ValueError: duration = None # Find the first <img> tag in the summary, if there is one image = None m = img_regex.match(e['summary']) if m is not None: image = m.group(1)[1:-1] title = e['title'] slug = slugify(title) author_name = u"PLACEHOLDER NAME" author_email = u"*****@*****.**" if 'author_detail' in e: if 'name' in e['author_detail']: author_name = e['author_detail']['name'] if 'email' in e['author_detail']: author_email = e['author_detail']['email'] year, month, day, hour, minute, second = e['updated_parsed'][:6] updated = datetime(year, month, day, hour, minute, second) media = Media() media.slug = get_available_slug(Media, slug, media) media.title = e['title'] media.author = Author(author_name, author_email) media.description = e['summary'] media.notes = u'' if tags: media.set_tags(tags) else: media.set_categories(tags) media.publish_on = updated media.created_on = updated media.publishable = True media.reviewed = True media.duration = duration DBSession.add(media) DBSession.flush() # Create thumbs from image, or default thumbs created_images = False if image: temp_imagefile = tempfile.TemporaryFile() imagefile = urllib2.urlopen(image) temp_imagefile.write(imagefile.read()) temp_imagefile.seek(0) filename = urlparse.urlparse(image)[2] create_thumbs_for(media, temp_imagefile, filename) created_images = True if not created_images: create_default_thumbs_for(media) print "Loaded episode:", media # now add all of the files. for enc in e['enclosures']: mf = media_file_from_enclosure(enc, media, save_files) print "Loaded media file:", mf media.update_status() return media