Merge remote-tracking branch 'rzhxeo/crunchyroll'
[youtube-dl] / youtube_dl / extractor / internetvideoarchive.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     compat_urlparse,
6     compat_urllib_parse,
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': 153,
22         },
23     }
24
25     @staticmethod
26     def _build_url(query):
27         return 'http://video.internetvideoarchive.net/flash/players/flashconfiguration.aspx?' + query
28
29     @staticmethod
30     def _clean_query(query):
31         NEEDED_ARGS = ['publishedid', 'customerid']
32         query_dic = compat_urlparse.parse_qs(query)
33         cleaned_dic = dict((k,v[0]) for (k,v) in query_dic.items() if k in NEEDED_ARGS)
34         # Other player ids return m3u8 urls
35         cleaned_dic['playerid'] = '247'
36         cleaned_dic['videokbrate'] = '100000'
37         return compat_urllib_parse.urlencode(cleaned_dic)
38
39     def _real_extract(self, url):
40         query = compat_urlparse.urlparse(url).query
41         query_dic = compat_urlparse.parse_qs(query)
42         video_id = query_dic['publishedid'][0]
43         url = self._build_url(query)
44
45         flashconfiguration = self._download_xml(url, video_id,
46             u'Downloading flash configuration')
47         file_url = flashconfiguration.find('file').text
48         file_url = file_url.replace('/playlist.aspx', '/mrssplaylist.aspx')
49         # Replace some of the parameters in the query to get the best quality
50         # and http links (no m3u8 manifests)
51         file_url = re.sub(r'(?<=\?)(.+)$',
52             lambda m: self._clean_query(m.group()),
53             file_url)
54         info = self._download_xml(file_url, video_id,
55             u'Downloading video info')
56         item = info.find('channel/item')
57
58         def _bp(p):
59             return xpath_with_ns(p,
60                 {'media': 'http://search.yahoo.com/mrss/',
61                 'jwplayer': 'http://developer.longtailvideo.com/trac/wiki/FlashFormats'})
62         formats = []
63         for content in item.findall(_bp('media:group/media:content')):
64             attr = content.attrib
65             f_url = attr['url']
66             formats.append({
67                 'url': f_url,
68                 'ext': determine_ext(f_url),
69                 'width': int(attr['width']),
70                 'bitrate': int(attr['bitrate']),
71             })
72         formats = sorted(formats, key=lambda f: f['bitrate'])
73
74         return {
75             'id': video_id,
76             'title': item.find('title').text,
77             'formats': formats,
78             'thumbnail': item.find(_bp('media:thumbnail')).attrib['url'],
79             'description': item.find('description').text,
80             'duration': int(attr['duration']),
81         }