Beispiel #1
0
  def add_from_snippet(cls, items, collection=None, publisher=None, enforce_category=False, approve=False, dont_fetch_publisher=False):
    from publisher import Publisher
    from publisher import PublisherMedia
    from collection import CollectionMedia
    from topic import Topic
    from topic import TopicMedia
    from topic import TopicCollectionMedia

    medias = []
    for item in items:
      id = item['id']
      media = Media.get_by_key_name(MediaHost.YOUTUBE + id)
      if not media:
        duration = re.search('PT((\d*)H)?((\d*)M)?((\d*)S)?', item['contentDetails']['duration']).groups()
        duration = float(3600*float(duration[1] or 0) + 60*float(duration[3] or 0) + float(duration[5] or 0))
        media = cls(key_name=(MediaHost.YOUTUBE + id),
                    type=MediaType.VIDEO,
                    host_id=id,
                    name=db.Text(item['snippet']['title']),
                    published=iso8601.parse_date(item['snippet']['publishedAt']).replace(tzinfo=None),
                    duration=duration,
                    description=db.Text(item['snippet']['description'].replace("\n", r" ") if item['snippet']['description'] else ''),
                    host_views=int(item['statistics']['viewCount']) if item['statistics']['viewCount'] else 0)
        media.put()
        logging.info('FETCHED: ' + media.name)

      collection_media = None
      if collection and \
          (not enforce_category or (item['snippet'].get('categoryId') in collection.categories)):
        collection_media = CollectionMedia.add(collection, media, publisher=publisher,
                                               approved=(True if approve else None))

      if not dont_fetch_publisher and (not publisher or publisher.channel_id != item['snippet']['channelId']):
        publisher = Publisher.get_by_channel_id(item['snippet']['channelId'], item['snippet']['channelTitle'])
      if publisher and item['snippet']['channelId'] == publisher.channel_id:
        pm = media.publisherMedias.get()
        if pm and pm.publisher.channel_id != item['snippet']['channelId']:
          # Check if there was an incorrect publisher assigned before
          pm.delete()
        pm = PublisherMedia.add(publisher=publisher, media=media)
        logging.info(pm.publisher.name)

      # if item.get('topicDetails'):
      #   for topic_id in item['topicDetails']['topicIds']:
      #     topic = Topic.add(topic_id)
      #     TopicMedia.add(topic, media)
      #     if collection_media:
      #       TopicCollectionMedia.add(topic, collection_media)

      medias.append(media)
    return medias
Beispiel #2
0
  def add_from_entry(cls, entries, fetch_publisher=False, approve=False, collection=None):
    from publisher import Publisher
    from publisher import PublisherMedia
    from collection import CollectionMedia

    medias = []
    for entry in [e for e in entries if e.media.player and not e.noembed]:
      content_url = urlparse.urlparse(entry.media.player.url)
      id = urlparse.parse_qs(content_url.query)['v'][0]
      media = Media.get_by_key_name(MediaHost.YOUTUBE + id)
      publisher = None
      if not media:
        name = entry.media.title.text.decode('utf-8')
        desc = entry.media.description.text
        desc = desc.decode('utf-8').replace("\n", r" ") if desc else None
        category = entry.media.category[0].text
        category = category.decode('utf-8') if category else None
        media = cls(key_name=(MediaHost.YOUTUBE + id),
                    type=MediaType.VIDEO,
                    host_id=id,
                    name=db.Text(name),
                    published=iso8601.parse_date(entry.published.text).replace(tzinfo=None),
                    duration=float(entry.media.duration.seconds),
                    description=desc,
                    host_views=int(entry.statistics.view_count) if entry.statistics else 0,
                    category=category)

        publisher_name = entry.author[0].name.text.lower()
        publisher = Publisher.add(MediaHost.YOUTUBE, publisher_name, fetch_details=fetch_publisher)
        PublisherMedia.add(publisher, media)
        media.put()
      medias.append(media)

      publisher = publisher or media.publisherMedias.get().publisher if media.publisherMedias.get() else None
      collection_media = None
      if collection:
        collection_media = CollectionMedia.add(collection, media, publisher=publisher, approved=(True if approve else None))
    return medias