]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/steam.py
e15c22f2a73247bd5bd424bbe331348f9aa2ac42
[yt-dlp.git] / yt_dlp / extractor / steam.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 extract_attributes,
6 ExtractorError,
7 get_element_by_class,
8 )
9
10
11 class SteamIE(InfoExtractor):
12 _VALID_URL = r"""(?x)
13 https?://(?:store\.steampowered|steamcommunity)\.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 https?://(?:www\.)?steamcommunity\.com/sharedfiles/filedetails/\?id=(?P<fileID>[0-9]+)
20 """
21 _VIDEO_PAGE_TEMPLATE = 'http://store.steampowered.com/video/%s/'
22 _AGECHECK_TEMPLATE = 'http://store.steampowered.com/agecheck/video/%s/?snr=1_agecheck_agecheck__age-gate&ageDay=1&ageMonth=January&ageYear=1970'
23 _TESTS = [{
24 'url': 'http://store.steampowered.com/video/105600/',
25 'playlist': [
26 {
27 'md5': '695242613303ffa2a4c44c9374ddc067',
28 'info_dict': {
29 'id': '256785003',
30 'ext': 'mp4',
31 'title': 'Terraria video 256785003',
32 'thumbnail': r're:^https://cdn\.[^\.]+\.steamstatic\.com',
33 'n_entries': 2,
34 }
35 },
36 {
37 'md5': '6a294ee0c4b1f47f5bb76a65e31e3592',
38 'info_dict': {
39 'id': '2040428',
40 'ext': 'mp4',
41 'title': 'Terraria video 2040428',
42 'playlist_index': 2,
43 'thumbnail': r're:^https://cdn\.[^\.]+\.steamstatic\.com',
44 'n_entries': 2,
45 }
46 }
47 ],
48 'info_dict': {
49 'id': '105600',
50 'title': 'Terraria',
51 },
52 'params': {
53 'playlistend': 2,
54 }
55 }, {
56 'url': 'https://store.steampowered.com/app/271590/Grand_Theft_Auto_V/',
57 'info_dict': {
58 'id': '256757115',
59 'title': 'Grand Theft Auto V video 256757115',
60 'ext': 'mp4',
61 'thumbnail': r're:^https://cdn\.[^\.]+\.steamstatic\.com',
62 'n_entries': 20,
63 },
64 }]
65
66 def _real_extract(self, url):
67 m = self._match_valid_url(url)
68 fileID = m.group('fileID')
69 if fileID:
70 video_url = url
71 playlist_id = fileID
72 else:
73 gameID = m.group('gameID')
74 playlist_id = gameID
75 video_url = self._VIDEO_PAGE_TEMPLATE % playlist_id
76
77 self._set_cookie('steampowered.com', 'wants_mature_content', '1')
78 self._set_cookie('steampowered.com', 'birthtime', '944006401')
79 self._set_cookie('steampowered.com', 'lastagecheckage', '1-0-2000')
80
81 webpage = self._download_webpage(video_url, playlist_id)
82
83 if re.search('<div[^>]+>Please enter your birth date to continue:</div>', webpage) is not None:
84 video_url = self._AGECHECK_TEMPLATE % playlist_id
85 self.report_age_confirmation()
86 webpage = self._download_webpage(video_url, playlist_id)
87
88 videos = re.findall(r'(<div[^>]+id=[\'"]highlight_movie_(\d+)[\'"][^>]+>)', webpage)
89 entries = []
90 playlist_title = get_element_by_class('apphub_AppName', webpage)
91 for movie, movie_id in videos:
92 if not movie:
93 continue
94 movie = extract_attributes(movie)
95 if not movie_id:
96 continue
97 entry = {
98 'id': movie_id,
99 'title': f'{playlist_title} video {movie_id}',
100 }
101 formats = []
102 if movie:
103 entry['thumbnail'] = movie.get('data-poster')
104 for quality in ('', '-hd'):
105 for ext in ('webm', 'mp4'):
106 video_url = movie.get('data-%s%s-source' % (ext, quality))
107 if video_url:
108 formats.append({
109 'format_id': ext + quality,
110 'url': video_url,
111 })
112 self._sort_formats(formats)
113 entry['formats'] = formats
114 entries.append(entry)
115 embedded_videos = re.findall(r'(<iframe[^>]+>)', webpage)
116 for evideos in embedded_videos:
117 evideos = extract_attributes(evideos).get('src')
118 video_id = self._search_regex(r'youtube\.com/embed/([0-9A-Za-z_-]{11})', evideos, 'youtube_video_id', default=None)
119 if video_id:
120 entries.append({
121 '_type': 'url_transparent',
122 'id': video_id,
123 'url': video_id,
124 'ie_key': 'Youtube',
125 })
126 if not entries:
127 raise ExtractorError('Could not find any videos')
128
129 return self.playlist_result(entries, playlist_id, playlist_title)
130
131
132 class SteamCommunityBroadcastIE(InfoExtractor):
133 _VALID_URL = r'https?://steamcommunity\.(?:com)/broadcast/watch/(?P<id>\d+)'
134 _TESTS = [{
135 'url': 'https://steamcommunity.com/broadcast/watch/76561199073851486',
136 'info_dict': {
137 'id': '76561199073851486',
138 'title': r're:Steam Community :: pepperm!nt :: Broadcast 2022-06-26 \d{2}:\d{2}',
139 'ext': 'mp4',
140 'uploader_id': 1113585758,
141 'uploader': 'pepperm!nt',
142 'live_status': 'is_live',
143 },
144 'skip': 'Stream has ended',
145 }]
146
147 def _real_extract(self, url):
148 video_id = self._match_id(url)
149 webpage = self._download_webpage(url, video_id)
150 json_data = self._download_json(
151 'https://steamcommunity.com/broadcast/getbroadcastmpd/',
152 video_id, query={'steamid': f'{video_id}'})
153
154 formats, subs = self._extract_m3u8_formats_and_subtitles(json_data['hls_url'], video_id)
155
156 ''' # We cannot download live dash atm
157 mpd_formats, mpd_subs = self._extract_mpd_formats_and_subtitles(json_data['url'], video_id)
158 formats.extend(mpd_formats)
159 self._merge_subtitles(mpd_subs, target=subs)
160 '''
161
162 uploader_json = self._download_json(
163 'https://steamcommunity.com/actions/ajaxresolveusers',
164 video_id, query={'steamids': video_id})[0]
165
166 self._sort_formats(formats)
167 return {
168 'id': video_id,
169 'title': self._html_extract_title(webpage) or self._og_search_title(webpage),
170 'formats': formats,
171 'live_status': 'is_live',
172 'view_count': json_data.get('num_view'),
173 'uploader': uploader_json.get('persona_name'),
174 'uploader_id': uploader_json.get('accountid'),
175 'subtitles': subs,
176 }