Add an extractor for internetvideoarchive.com videos
[youtube-dl] / youtube_dl / extractor / internetvideoarchive.py
1 import re
2 import xml.etree.ElementTree
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urlparse,
7     xpath_with_ns,
8     determine_ext,
9 )
10
11
12 class InternetVideoArchiveIE(InfoExtractor):
13     _VALID_URL = r'https?://video\.internetvideoarchive\.net/flash/players/.*?\?.*?publishedid.*?'
14
15     _TEST = {
16         u'url': u'http://video.internetvideoarchive.net/flash/players/flashconfiguration.aspx?customerid=69249&publishedid=452693&playerid=247',
17         u'file': u'452693.mp4',
18         u'info_dict': {
19             u'title': u'SKYFALL',
20             u'description': u'In SKYFALL, Bond\'s loyalty to M is tested as her past comes back to haunt her. As MI6 comes under attack, 007 must track down and destroy the threat, no matter how personal the cost.',
21             u'duration': 156,
22         },
23     }
24
25     @staticmethod
26     def _build_url(query):
27         return 'http://video.internetvideoarchive.net/flash/players/flashconfiguration.aspx?' + query
28
29     def _real_extract(self, url):
30         query = compat_urlparse.urlparse(url).query
31         query_dic = compat_urlparse.parse_qs(query)
32         video_id = query_dic['publishedid'][0]
33         url = self._build_url(query)
34
35         flashconfiguration_xml = self._download_webpage(url, video_id,
36             u'Downloading flash configuration')
37         flashconfiguration = xml.etree.ElementTree.fromstring(flashconfiguration_xml.encode('utf-8'))
38         file_url = flashconfiguration.find('file').text
39         file_url = file_url.replace('/playlist.aspx', '/mrssplaylist.aspx')
40         info_xml = self._download_webpage(file_url, video_id,
41             u'Downloading video info')
42         info = xml.etree.ElementTree.fromstring(info_xml.encode('utf-8'))
43         item = info.find('channel/item')
44
45         def _bp(p):
46             return xpath_with_ns(p,
47                 {'media': 'http://search.yahoo.com/mrss/',
48                 'jwplayer': 'http://developer.longtailvideo.com/trac/wiki/FlashFormats'})
49         formats = []
50         for content in item.findall(_bp('media:group/media:content')):
51             attr = content.attrib
52             f_url = attr['url']
53             formats.append({
54                 'url': f_url,
55                 'ext': determine_ext(f_url),
56                 'width': int(attr['width']),
57                 'bitrate': int(attr['bitrate']),
58             })
59         formats = sorted(formats, key=lambda f: f['bitrate'])
60
61         info = {
62             'id': video_id,
63             'title': item.find('title').text,
64             'formats': formats,
65             'thumbnail': item.find(_bp('media:thumbnail')).attrib['url'],
66             'description': item.find('description').text,
67             'duration': int(attr['duration']),
68         }
69         # TODO: Remove when #980 has been merged
70         info.update(formats[-1])
71         return info