Merge remote-tracking branch 'Rudloff/websurg'
[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     compat_urllib_parse,
8     xpath_with_ns,
9     determine_ext,
10 )
11
12
13 class InternetVideoArchiveIE(InfoExtractor):
14     _VALID_URL = r'https?://video\.internetvideoarchive\.net/flash/players/.*?\?.*?publishedid.*?'
15
16     _TEST = {
17         u'url': u'http://video.internetvideoarchive.net/flash/players/flashconfiguration.aspx?customerid=69249&publishedid=452693&playerid=247',
18         u'file': u'452693.mp4',
19         u'info_dict': {
20             u'title': u'SKYFALL',
21             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.',
22             u'duration': 156,
23         },
24     }
25
26     @staticmethod
27     def _build_url(query):
28         return 'http://video.internetvideoarchive.net/flash/players/flashconfiguration.aspx?' + query
29
30     @staticmethod
31     def _clean_query(query):
32         NEEDED_ARGS = ['publishedid', 'customerid']
33         query_dic = compat_urlparse.parse_qs(query)
34         cleaned_dic = dict((k,v[0]) for (k,v) in query_dic.items() if k in NEEDED_ARGS)
35         # Other player ids return m3u8 urls
36         cleaned_dic['playerid'] = '247'
37         cleaned_dic['videokbrate'] = '100000'
38         return compat_urllib_parse.urlencode(cleaned_dic)
39
40     def _real_extract(self, url):
41         query = compat_urlparse.urlparse(url).query
42         query_dic = compat_urlparse.parse_qs(query)
43         video_id = query_dic['publishedid'][0]
44         url = self._build_url(query)
45
46         flashconfiguration_xml = self._download_webpage(url, video_id,
47             u'Downloading flash configuration')
48         flashconfiguration = xml.etree.ElementTree.fromstring(flashconfiguration_xml.encode('utf-8'))
49         file_url = flashconfiguration.find('file').text
50         file_url = file_url.replace('/playlist.aspx', '/mrssplaylist.aspx')
51         # Replace some of the parameters in the query to get the best quality
52         # and http links (no m3u8 manifests)
53         file_url = re.sub(r'(?<=\?)(.+)$',
54             lambda m: self._clean_query(m.group()),
55             file_url)
56         info_xml = self._download_webpage(file_url, video_id,
57             u'Downloading video info')
58         info = xml.etree.ElementTree.fromstring(info_xml.encode('utf-8'))
59         item = info.find('channel/item')
60
61         def _bp(p):
62             return xpath_with_ns(p,
63                 {'media': 'http://search.yahoo.com/mrss/',
64                 'jwplayer': 'http://developer.longtailvideo.com/trac/wiki/FlashFormats'})
65         formats = []
66         for content in item.findall(_bp('media:group/media:content')):
67             attr = content.attrib
68             f_url = attr['url']
69             formats.append({
70                 'url': f_url,
71                 'ext': determine_ext(f_url),
72                 'width': int(attr['width']),
73                 'bitrate': int(attr['bitrate']),
74             })
75         formats = sorted(formats, key=lambda f: f['bitrate'])
76
77         info = {
78             'id': video_id,
79             'title': item.find('title').text,
80             'formats': formats,
81             'thumbnail': item.find(_bp('media:thumbnail')).attrib['url'],
82             'description': item.find('description').text,
83             'duration': int(attr['duration']),
84         }
85         # TODO: Remove when #980 has been merged
86         info.update(formats[-1])
87         return info