[steam] Simplify
[youtube-dl] / youtube_dl / extractor / steam.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8     unescapeHTML,
9 )
10
11
12 class SteamIE(InfoExtractor):
13     _VALID_URL = r"""(?x)http://store\.steampowered\.com/
14                 (agecheck/)?
15                 (?P<urltype>video|app)/ #If the page is only for videos or for a game
16                 (?P<gameID>\d+)/?
17                 (?P<videoID>\d*)(?P<extra>\??) #For urltype == video we sometimes get the videoID
18                 """
19     _VIDEO_PAGE_TEMPLATE = 'http://store.steampowered.com/video/%s/'
20     _AGECHECK_TEMPLATE = 'http://store.steampowered.com/agecheck/video/%s/?snr=1_agecheck_agecheck__age-gate&ageDay=1&ageMonth=January&ageYear=1970'
21     _TEST = {
22         "url": "http://store.steampowered.com/video/105600/",
23         "playlist": [
24             {
25                 "md5": "f870007cee7065d7c76b88f0a45ecc07",
26                 "info_dict": {
27                     'id': '81300',
28                     'ext': 'flv',
29                     "title": "Terraria 1.1 Trailer",
30                     'playlist_index': 1,
31                 }
32             },
33             {
34                 "md5": "61aaf31a5c5c3041afb58fb83cbb5751",
35                 "info_dict": {
36                     'id': '80859',
37                     'ext': 'flv',
38                     "title": "Terraria Trailer",
39                     'playlist_index': 2,
40                 }
41             }
42         ],
43         'params': {
44             'playlistend': 2,
45         }
46     }
47
48     def _real_extract(self, url):
49         m = re.match(self._VALID_URL, url, re.VERBOSE)
50         gameID = m.group('gameID')
51
52         videourl = self._VIDEO_PAGE_TEMPLATE % gameID
53         webpage = self._download_webpage(videourl, gameID)
54
55         if re.search('<h2>Please enter your birth date to continue:</h2>', webpage) is not None:
56             videourl = self._AGECHECK_TEMPLATE % gameID
57             self.report_age_confirmation()
58             webpage = self._download_webpage(videourl, gameID)
59
60         self.report_extraction(gameID)
61         game_title = self._html_search_regex(r'<h2 class="pageheader">(.*?)</h2>',
62                                              webpage, 'game title')
63
64         mweb = re.finditer(
65             r"'movie_(?P<videoID>\d+)': \{\s*FILENAME: \"(?P<videoURL>[\w:/\.\?=]+)\"(,\s*MOVIE_NAME: \"(?P<videoName>[\w:/\.\?=\+-]+)\")?\s*\},",
66             webpage)
67         titles = re.finditer(
68             r'<span class="title">(?P<videoName>.+?)</span>', webpage)
69         thumbs = re.finditer(
70             r'<img class="movie_thumb" src="(?P<thumbnail>.+?)">', webpage)
71         videos = []
72         for vid, vtitle, thumb in zip(mweb, titles, thumbs):
73             video_id = vid.group('videoID')
74             title = vtitle.group('videoName')
75             video_url = vid.group('videoURL')
76             video_thumb = thumb.group('thumbnail')
77             if not video_url:
78                 raise ExtractorError('Cannot find video url for %s' % video_id)
79             videos.append({
80                 'id': video_id,
81                 'url': video_url,
82                 'ext': 'flv',
83                 'title': unescapeHTML(title),
84                 'thumbnail': video_thumb
85             })
86         return self.playlist_result(videos, gameID, game_title)