steam.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. unescapeHTML,
  7. )
  8. class SteamIE(InfoExtractor):
  9. _VALID_URL = r"""(?x)
  10. https?://store\.steampowered\.com/
  11. (agecheck/)?
  12. (?P<urltype>video|app)/ #If the page is only for videos or for a game
  13. (?P<gameID>\d+)/?
  14. (?P<videoID>\d*)(?P<extra>\??) # For urltype == video we sometimes get the videoID
  15. |
  16. https?://(?:www\.)?steamcommunity\.com/sharedfiles/filedetails/\?id=(?P<fileID>[0-9]+)
  17. """
  18. _VIDEO_PAGE_TEMPLATE = 'http://store.steampowered.com/video/%s/'
  19. _AGECHECK_TEMPLATE = 'http://store.steampowered.com/agecheck/video/%s/?snr=1_agecheck_agecheck__age-gate&ageDay=1&ageMonth=January&ageYear=1970'
  20. _TESTS = [{
  21. "url": "http://store.steampowered.com/video/105600/",
  22. "playlist": [
  23. {
  24. "md5": "f870007cee7065d7c76b88f0a45ecc07",
  25. "info_dict": {
  26. 'id': '81300',
  27. 'ext': 'flv',
  28. "title": "Terraria 1.1 Trailer",
  29. 'playlist_index': 1,
  30. }
  31. },
  32. {
  33. "md5": "61aaf31a5c5c3041afb58fb83cbb5751",
  34. "info_dict": {
  35. 'id': '80859',
  36. 'ext': 'flv',
  37. "title": "Terraria Trailer",
  38. 'playlist_index': 2,
  39. }
  40. }
  41. ],
  42. 'params': {
  43. 'playlistend': 2,
  44. }
  45. }, {
  46. 'url': 'http://steamcommunity.com/sharedfiles/filedetails/?id=242472205',
  47. 'info_dict': {
  48. 'id': 'WB5DvDOOvAY',
  49. 'ext': 'mp4',
  50. 'upload_date': '20140329',
  51. 'title': 'FRONTIERS - Final Greenlight Trailer',
  52. 'description': 'md5:dc96a773669d0ca1b36c13c1f30250d9',
  53. 'uploader': 'AAD Productions',
  54. 'uploader_id': 'AtomicAgeDogGames',
  55. }
  56. }]
  57. def _real_extract(self, url):
  58. m = re.match(self._VALID_URL, url)
  59. fileID = m.group('fileID')
  60. if fileID:
  61. videourl = url
  62. playlist_id = fileID
  63. else:
  64. gameID = m.group('gameID')
  65. playlist_id = gameID
  66. videourl = self._VIDEO_PAGE_TEMPLATE % playlist_id
  67. webpage = self._download_webpage(videourl, playlist_id)
  68. if re.search('<h2>Please enter your birth date to continue:</h2>', webpage) is not None:
  69. videourl = self._AGECHECK_TEMPLATE % playlist_id
  70. self.report_age_confirmation()
  71. webpage = self._download_webpage(videourl, playlist_id)
  72. if fileID:
  73. playlist_title = self._html_search_regex(
  74. r'<div class="workshopItemTitle">(.+)</div>', webpage, 'title')
  75. mweb = re.finditer(r'''(?x)
  76. 'movie_(?P<videoID>[0-9]+)':\s*\{\s*
  77. YOUTUBE_VIDEO_ID:\s*"(?P<youtube_id>[^"]+)",
  78. ''', webpage)
  79. videos = [{
  80. '_type': 'url',
  81. 'url': vid.group('youtube_id'),
  82. 'ie_key': 'Youtube',
  83. } for vid in mweb]
  84. else:
  85. playlist_title = self._html_search_regex(
  86. r'<h2 class="pageheader">(.*?)</h2>', webpage, 'game title')
  87. mweb = re.finditer(r'''(?x)
  88. 'movie_(?P<videoID>[0-9]+)':\s*\{\s*
  89. FILENAME:\s*"(?P<videoURL>[\w:/\.\?=]+)"
  90. (,\s*MOVIE_NAME:\s*\"(?P<videoName>[\w:/\.\?=\+-]+)\")?\s*\},
  91. ''', webpage)
  92. titles = re.finditer(
  93. r'<span class="title">(?P<videoName>.+?)</span>', webpage)
  94. thumbs = re.finditer(
  95. r'<img class="movie_thumb" src="(?P<thumbnail>.+?)">', webpage)
  96. videos = []
  97. for vid, vtitle, thumb in zip(mweb, titles, thumbs):
  98. video_id = vid.group('videoID')
  99. title = vtitle.group('videoName')
  100. video_url = vid.group('videoURL')
  101. video_thumb = thumb.group('thumbnail')
  102. if not video_url:
  103. raise ExtractorError('Cannot find video url for %s' % video_id)
  104. videos.append({
  105. 'id': video_id,
  106. 'url': video_url,
  107. 'ext': 'flv',
  108. 'title': unescapeHTML(title),
  109. 'thumbnail': video_thumb
  110. })
  111. if not videos:
  112. raise ExtractorError('Could not find any videos')
  113. return self.playlist_result(videos, playlist_id, playlist_title)