steam.py 6.4 KB

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