Пример #1
0
def _get_metadata(url, username, password):
    """Retrieve the metadata for the given url with username and password.

      >>> _get_metadata('http://one.revver.com/watch/1', '', '')
      Traceback (most recent call last):
      Fault: <Fault 3: 'Authentication failed'>

    """

    video_id = get_video_id(url)

    api_url = 'https://api.revver.com/xml/1.0?login=%s&passwd=%s' % (username,
                                                                     password)

    api = Server(api_url)
    results = api.video.find({'ids': [video_id]},
                             ['id', 'title', 'author',
                              'description', 'thumbnailUrl',
                              'keywords', 'duration'])
    result = results[0]

    metadata = VideoMetadata()
    metadata.title = unicode(result['title'], 'utf-8')
    metadata.author = unicode(result['author'], 'utf-8')
    metadata.description = unicode(result['description'], 'utf-8')
    metadata.thumbnail_url = result['thumbnailUrl']
    metadata.tags = [unicode(x, 'utf-8') for x in result['keywords']]
    metadata.duration = float(result['duration'])

    return metadata
Пример #2
0
def _youtube_metadata_lookup(xml):
    """Parse the given xml and get appropriate metadata.

      >>> xml = '''<?xml version="1.0" ?>
      ... <ut_response status="ok">
      ...   <video_details>
      ...     <author>youtubeuser</author>
      ...     <title>Random Title</title>
      ...     <rating_avg>3.25</rating_avg>
      ...     <rating_count>10</rating_count>
      ...     <tags>california trip redwoods</tags>
      ...     <description>Random description.</description>
      ...     <update_time>1129803584</update_time>
      ...     <view_count>7</view_count>
      ...     <upload_time>1127760809</upload_time>
      ...     <length_seconds>8</length_seconds>
      ...     <recording_date>None</recording_date>
      ...     <recording_location/>
      ...     <recording_country/>
      ...     <thumbnail_url>http://blah.com/default.jpg</thumbnail_url>
      ...   </video_details>
      ... </ut_response>'''

      >>> data = _youtube_metadata_lookup(xml)
      >>> data.author
      u'youtubeuser'
      >>> data.title
      u'Random Title'
      >>> data.description
      u'Random description.'
      >>> data.tags
      [u'california', u'trip', u'redwoods']
      >>> data.thumbnail_url
      u'http://blah.com/default.jpg'
      >>> data.duration
      8.0

    """

    try:
        doc = minidom.parseString(xml)
    except expat.ExpatError:
        logger.exception('Error while trying to parse RSS XML - "%s"' % xml)
        return None

    metadata = VideoMetadata()
    metadata.title = xpath_text(doc, u'ut_response/video_details/title')
    metadata.author = xpath_text(doc, u'ut_response/video_details/author')
    metadata.description = xpath_text(\
        doc, u'ut_response/video_details/description')
    metadata.thumbnail_url = xpath_text(\
        doc, u'ut_response/video_details/thumbnail_url')
    metadata.tags = xpath_text(\
        doc, u'ut_response/video_details/tags').split(' ')

    duration = xpath_text( \
        doc, u'ut_response/video_details/length_seconds')
    if duration is not None and duration.strip() != '':
        try:
            metadata.duration = float(duration)
        except:
            # probably wasn't an int, ignoring
            pass

    return metadata