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 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}
Ejemplo n.º 3
0
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