Merge remote-tracking branch 'dstftw/channel9'
[youtube-dl] / youtube_dl / extractor / metacritic.py
1 import re
2 import operator
3
4 from .common import InfoExtractor
5 from ..utils import (
6     fix_xml_all_ampersand,
7 )
8
9
10 class MetacriticIE(InfoExtractor):
11     _VALID_URL = r'https?://www\.metacritic\.com/.+?/trailers/(?P<id>\d+)'
12
13     _TEST = {
14         u'url': u'http://www.metacritic.com/game/playstation-4/infamous-second-son/trailers/3698222',
15         u'file': u'3698222.mp4',
16         u'info_dict': {
17             u'title': u'inFamous: Second Son - inSide Sucker Punch: Smoke & Mirrors',
18             u'description': u'Take a peak behind-the-scenes to see how Sucker Punch brings smoke into the universe of inFAMOUS Second Son on the PS4.',
19             u'duration': 221,
20         },
21     }
22
23     def _real_extract(self, url):
24         mobj = re.match(self._VALID_URL, url)
25         video_id = mobj.group('id')
26         webpage = self._download_webpage(url, video_id)
27         # The xml is not well formatted, there are raw '&'
28         info = self._download_xml('http://www.metacritic.com/video_data?video=' + video_id,
29             video_id, u'Downloading info xml', transform_source=fix_xml_all_ampersand)
30
31         clip = next(c for c in info.findall('playList/clip') if c.find('id').text == video_id)
32         formats = []
33         for videoFile in clip.findall('httpURI/videoFile'):
34             rate_str = videoFile.find('rate').text
35             video_url = videoFile.find('filePath').text
36             formats.append({
37                 'url': video_url,
38                 'ext': 'mp4',
39                 'format_id': rate_str,
40                 'rate': int(rate_str),
41             })
42         formats.sort(key=operator.itemgetter('rate'))
43
44         description = self._html_search_regex(r'<b>Description:</b>(.*?)</p>',
45             webpage, u'description', flags=re.DOTALL)
46
47         return {
48             'id': video_id,
49             'title': clip.find('title').text,
50             'formats': formats,
51             'description': description,
52             'duration': int(clip.find('duration').text),
53         }