33e59e82c47f10d37c0004de073757568d25347b
[youtube-dl] / youtube_dl / extractor / gametrailers.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     compat_urllib_parse,
6
7     ExtractorError,
8 )
9
10 class GametrailersIE(InfoExtractor):
11     _VALID_URL = r'http://www.gametrailers.com/(?P<type>videos|reviews|full-episodes)/(?P<id>.*?)/(?P<title>.*)'
12
13     def _real_extract(self, url):
14         mobj = re.match(self._VALID_URL, url)
15         if mobj is None:
16             raise ExtractorError(u'Invalid URL: %s' % url)
17         video_id = mobj.group('id')
18         video_type = mobj.group('type')
19         webpage = self._download_webpage(url, video_id)
20         if video_type == 'full-episodes':
21             mgid_re = r'data-video="(?P<mgid>mgid:.*?)"'
22         else:
23             mgid_re = r'data-contentId=\'(?P<mgid>mgid:.*?)\''
24         mgid = self._search_regex(mgid_re, webpage, u'mgid')
25         data = compat_urllib_parse.urlencode({'uri': mgid, 'acceptMethods': 'fms'})
26
27         info_page = self._download_webpage('http://www.gametrailers.com/feeds/mrss?' + data,
28                                            video_id, u'Downloading video info')
29         links_webpage = self._download_webpage('http://www.gametrailers.com/feeds/mediagen/?' + data,
30                                                video_id, u'Downloading video urls info')
31
32         self.report_extraction(video_id)
33         info_re = r'''<title><!\[CDATA\[(?P<title>.*?)\]\]></title>.*
34                       <description><!\[CDATA\[(?P<description>.*?)\]\]></description>.*
35                       <image>.*
36                         <url>(?P<thumb>.*?)</url>.*
37                       </image>'''
38
39         m_info = re.search(info_re, info_page, re.VERBOSE|re.DOTALL)
40         if m_info is None:
41             raise ExtractorError(u'Unable to extract video info')
42         video_title = m_info.group('title')
43         video_description = m_info.group('description')
44         video_thumb = m_info.group('thumb')
45
46         m_urls = list(re.finditer(r'<src>(?P<url>.*)</src>', links_webpage))
47         if m_urls is None or len(m_urls) == 0:
48             raise ExtractorError(u'Unable to extract video url')
49         # They are sorted from worst to best quality
50         video_url = m_urls[-1].group('url')
51
52         return {'url':         video_url,
53                 'id':          video_id,
54                 'title':       video_title,
55                 # Videos are actually flv not mp4
56                 'ext':         'flv',
57                 'thumbnail':   video_thumb,
58                 'description': video_description,
59                 }