]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/steam.py
[youtube] Make uploader extraction non fatal (#16444)
[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     extract_attributes,
8     ExtractorError,
9     get_element_by_class,
10     js_to_json,
11 )
12
13
14 class SteamIE(InfoExtractor):
15     _VALID_URL = r"""(?x)
16         https?://store\.steampowered\.com/
17             (agecheck/)?
18             (?P<urltype>video|app)/ #If the page is only for videos or for a game
19             (?P<gameID>\d+)/?
20             (?P<videoID>\d*)(?P<extra>\??) # For urltype == video we sometimes get the videoID
21         |
22         https?://(?:www\.)?steamcommunity\.com/sharedfiles/filedetails/\?id=(?P<fileID>[0-9]+)
23     """
24     _VIDEO_PAGE_TEMPLATE = 'http://store.steampowered.com/video/%s/'
25     _AGECHECK_TEMPLATE = 'http://store.steampowered.com/agecheck/video/%s/?snr=1_agecheck_agecheck__age-gate&ageDay=1&ageMonth=January&ageYear=1970'
26     _TESTS = [{
27         'url': 'http://store.steampowered.com/video/105600/',
28         'playlist': [
29             {
30                 'md5': '6a294ee0c4b1f47f5bb76a65e31e3592',
31                 'info_dict': {
32                     'id': '2040428',
33                     'ext': 'mp4',
34                     'title': 'Terraria 1.3 Trailer',
35                     'playlist_index': 1,
36                 }
37             },
38             {
39                 'md5': '911672b20064ca3263fa89650ba5a7aa',
40                 'info_dict': {
41                     'id': '2029566',
42                     'ext': 'mp4',
43                     'title': 'Terraria 1.2 Trailer',
44                     'playlist_index': 2,
45                 }
46             }
47         ],
48         'info_dict': {
49             'id': '105600',
50             'title': 'Terraria',
51         },
52         'params': {
53             'playlistend': 2,
54         }
55     }, {
56         'url': 'http://steamcommunity.com/sharedfiles/filedetails/?id=242472205',
57         'info_dict': {
58             'id': 'X8kpJBlzD2E',
59             'ext': 'mp4',
60             'upload_date': '20140617',
61             'title': 'FRONTIERS - Trapping',
62             'description': 'md5:bf6f7f773def614054089e5769c12a6e',
63             'uploader': 'AAD Productions',
64             'uploader_id': 'AtomicAgeDogGames',
65         }
66     }]
67
68     def _real_extract(self, url):
69         m = re.match(self._VALID_URL, url)
70         fileID = m.group('fileID')
71         if fileID:
72             videourl = url
73             playlist_id = fileID
74         else:
75             gameID = m.group('gameID')
76             playlist_id = gameID
77             videourl = self._VIDEO_PAGE_TEMPLATE % playlist_id
78
79         self._set_cookie('steampowered.com', 'mature_content', '1')
80
81         webpage = self._download_webpage(videourl, playlist_id)
82
83         if re.search('<h2>Please enter your birth date to continue:</h2>', webpage) is not None:
84             videourl = self._AGECHECK_TEMPLATE % playlist_id
85             self.report_age_confirmation()
86             webpage = self._download_webpage(videourl, playlist_id)
87
88         flash_vars = self._parse_json(self._search_regex(
89             r'(?s)rgMovieFlashvars\s*=\s*({.+?});', webpage,
90             'flash vars'), playlist_id, js_to_json)
91
92         playlist_title = None
93         entries = []
94         if fileID:
95             playlist_title = get_element_by_class('workshopItemTitle', webpage)
96             for movie in flash_vars.values():
97                 if not movie:
98                     continue
99                 youtube_id = movie.get('YOUTUBE_VIDEO_ID')
100                 if not youtube_id:
101                     continue
102                 entries.append({
103                     '_type': 'url',
104                     'url': youtube_id,
105                     'ie_key': 'Youtube',
106                 })
107         else:
108             playlist_title = get_element_by_class('apphub_AppName', webpage)
109             for movie_id, movie in flash_vars.items():
110                 if not movie:
111                     continue
112                 video_id = self._search_regex(r'movie_(\d+)', movie_id, 'video id', fatal=False)
113                 title = movie.get('MOVIE_NAME')
114                 if not title or not video_id:
115                     continue
116                 entry = {
117                     'id': video_id,
118                     'title': title.replace('+', ' '),
119                 }
120                 formats = []
121                 flv_url = movie.get('FILENAME')
122                 if flv_url:
123                     formats.append({
124                         'format_id': 'flv',
125                         'url': flv_url,
126                     })
127                 highlight_element = self._search_regex(
128                     r'(<div[^>]+id="highlight_movie_%s"[^>]+>)' % video_id,
129                     webpage, 'highlight element', fatal=False)
130                 if highlight_element:
131                     highlight_attribs = extract_attributes(highlight_element)
132                     if highlight_attribs:
133                         entry['thumbnail'] = highlight_attribs.get('data-poster')
134                         for quality in ('', '-hd'):
135                             for ext in ('webm', 'mp4'):
136                                 video_url = highlight_attribs.get('data-%s%s-source' % (ext, quality))
137                                 if video_url:
138                                     formats.append({
139                                         'format_id': ext + quality,
140                                         'url': video_url,
141                                     })
142                 if not formats:
143                     continue
144                 entry['formats'] = formats
145                 entries.append(entry)
146         if not entries:
147             raise ExtractorError('Could not find any videos')
148
149         return self.playlist_result(entries, playlist_id, playlist_title)