voxmedia.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import urllib.parse
  2. from .common import InfoExtractor
  3. from .once import OnceIE
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. try_get,
  8. unified_timestamp,
  9. )
  10. class VoxMediaVolumeIE(OnceIE):
  11. _VALID_URL = r'https?://volume\.vox-cdn\.com/embed/(?P<id>[0-9a-f]{9})'
  12. def _real_extract(self, url):
  13. video_id = self._match_id(url)
  14. webpage = self._download_webpage(url, video_id)
  15. setup = self._parse_json(self._search_regex(
  16. r'setup\s*=\s*({.+});', webpage, 'setup'), video_id)
  17. player_setup = setup.get('player_setup') or setup
  18. video_data = player_setup.get('video') or {}
  19. formatted_metadata = video_data.get('formatted_metadata') or {}
  20. info = {
  21. 'id': video_id,
  22. 'title': player_setup.get('title') or video_data.get('title_short'),
  23. 'description': video_data.get('description_long') or video_data.get('description_short'),
  24. 'thumbnail': formatted_metadata.get('thumbnail') or video_data.get('brightcove_thumbnail'),
  25. 'timestamp': unified_timestamp(formatted_metadata.get('video_publish_date')),
  26. }
  27. asset = try_get(setup, lambda x: x['embed_assets']['chorus'], dict) or {}
  28. formats = []
  29. hls_url = asset.get('hls_url')
  30. if hls_url:
  31. formats.extend(self._extract_m3u8_formats(
  32. hls_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  33. mp4_url = asset.get('mp4_url')
  34. if mp4_url:
  35. tbr = self._search_regex(r'-(\d+)k\.', mp4_url, 'bitrate', default=None)
  36. format_id = 'http'
  37. if tbr:
  38. format_id += '-' + tbr
  39. formats.append({
  40. 'format_id': format_id,
  41. 'url': mp4_url,
  42. 'tbr': int_or_none(tbr),
  43. })
  44. if formats:
  45. info['formats'] = formats
  46. info['duration'] = int_or_none(asset.get('duration'))
  47. return info
  48. for provider_video_type in ('youtube', 'brightcove'):
  49. provider_video_id = video_data.get(f'{provider_video_type}_id')
  50. if not provider_video_id:
  51. continue
  52. if provider_video_type == 'brightcove':
  53. info['formats'] = self._extract_once_formats(provider_video_id)
  54. else:
  55. info.update({
  56. '_type': 'url_transparent',
  57. 'url': provider_video_id if provider_video_type == 'youtube' else f'{provider_video_type}:{provider_video_id}',
  58. 'ie_key': provider_video_type.capitalize(),
  59. })
  60. return info
  61. raise ExtractorError('Unable to find provider video id')
  62. class VoxMediaIE(InfoExtractor):
  63. _VALID_URL = r'https?://(?:www\.)?(?:(?:theverge|vox|sbnation|eater|polygon|curbed|racked|funnyordie)\.com|recode\.net)/(?:[^/]+/)*(?P<id>[^/?]+)'
  64. _EMBED_REGEX = [r'<iframe[^>]+?src="(?P<url>https?://(?:www\.)?funnyordie\.com/embed/[^"]+)"']
  65. _TESTS = [{
  66. # Volume embed, Youtube
  67. 'url': 'http://www.theverge.com/2014/6/27/5849272/material-world-how-google-discovered-what-software-is-made-of',
  68. 'info_dict': {
  69. 'id': 'j4mLW6x17VM',
  70. 'ext': 'mp4',
  71. 'title': 'Material world: how Google discovered what software is made of',
  72. 'description': 'md5:dfc17e7715e3b542d66e33a109861382',
  73. 'upload_date': '20190710',
  74. 'uploader_id': 'TheVerge',
  75. 'uploader': 'The Verge',
  76. },
  77. 'add_ie': ['Youtube'],
  78. }, {
  79. # Volume embed, Youtube
  80. 'url': 'http://www.theverge.com/2014/10/21/7025853/google-nexus-6-hands-on-photos-video-android-phablet',
  81. 'md5': 'fd19aa0cf3a0eea515d4fd5c8c0e9d68',
  82. 'info_dict': {
  83. 'id': 'Gy8Md3Eky38',
  84. 'ext': 'mp4',
  85. 'title': 'The Nexus 6: hands-on with Google\'s phablet',
  86. 'description': 'md5:d9f0216e5fb932dd2033d6db37ac3f1d',
  87. 'uploader_id': 'TheVerge',
  88. 'upload_date': '20141021',
  89. 'uploader': 'The Verge',
  90. 'timestamp': 1413907200,
  91. },
  92. 'add_ie': ['Youtube'],
  93. 'skip': 'similar to the previous test',
  94. }, {
  95. # Volume embed, Youtube
  96. 'url': 'http://www.vox.com/2016/3/31/11336640/mississippi-lgbt-religious-freedom-bill',
  97. 'info_dict': {
  98. 'id': '22986359b',
  99. 'ext': 'mp4',
  100. 'title': "Mississippi's laws are so bad that its anti-LGBTQ law isn't needed to allow discrimination",
  101. 'description': 'md5:fc1317922057de31cd74bce91eb1c66c',
  102. 'upload_date': '20150915',
  103. 'timestamp': 1442332800,
  104. 'duration': 285,
  105. },
  106. 'add_ie': ['Youtube'],
  107. 'skip': 'similar to the previous test',
  108. }, {
  109. # youtube embed
  110. 'url': 'http://www.vox.com/2016/3/24/11291692/robot-dance',
  111. 'md5': '83b3080489fb103941e549352d3e0977',
  112. 'info_dict': {
  113. 'id': 'FcNHTJU1ufM',
  114. 'ext': 'mp4',
  115. 'title': 'How "the robot" became the greatest novelty dance of all time',
  116. 'description': 'md5:b081c0d588b8b2085870cda55e6da176',
  117. 'upload_date': '20160324',
  118. 'uploader_id': 'voxdotcom',
  119. 'uploader': 'Vox',
  120. },
  121. 'add_ie': ['Youtube'],
  122. 'skip': 'Page no longer contain videos',
  123. }, {
  124. # SBN.VideoLinkset.entryGroup multiple ooyala embeds
  125. 'url': 'http://www.sbnation.com/college-football-recruiting/2015/2/3/7970291/national-signing-day-rationalizations-itll-be-ok-itll-be-ok',
  126. 'info_dict': {
  127. 'id': 'national-signing-day-rationalizations-itll-be-ok-itll-be-ok',
  128. 'title': '25 lies you will tell yourself on National Signing Day',
  129. 'description': 'It\'s the most self-delusional time of the year, and everyone\'s gonna tell the same lies together!',
  130. },
  131. 'playlist': [{
  132. 'md5': '721fededf2ab74ae4176c8c8cbfe092e',
  133. 'info_dict': {
  134. 'id': 'p3cThlMjE61VDi_SD9JlIteSNPWVDBB9',
  135. 'ext': 'mp4',
  136. 'title': 'Buddy Hield vs Steph Curry (and the world)',
  137. 'description': 'Let’s dissect only the most important Final Four storylines.',
  138. },
  139. }, {
  140. 'md5': 'bf0c5cc115636af028be1bab79217ea9',
  141. 'info_dict': {
  142. 'id': 'BmbmVjMjE6esPHxdALGubTrouQ0jYLHj',
  143. 'ext': 'mp4',
  144. 'title': 'Chasing Cinderella 2016: Syracuse basketball',
  145. 'description': 'md5:e02d56b026d51aa32c010676765a690d',
  146. },
  147. }],
  148. 'skip': 'Page no longer contain videos',
  149. }, {
  150. # volume embed, Brightcove Once
  151. 'url': 'https://www.recode.net/2014/6/17/11628066/post-post-pc-ceo-the-full-code-conference-video-of-microsofts-satya',
  152. 'md5': '2dbc77b8b0bff1894c2fce16eded637d',
  153. 'info_dict': {
  154. 'id': '1231c973d',
  155. 'ext': 'mp4',
  156. 'title': 'Post-Post-PC CEO: The Full Code Conference Video of Microsoft\'s Satya Nadella',
  157. 'description': 'The longtime veteran was chosen earlier this year as the software giant\'s third leader in its history.',
  158. 'timestamp': 1402938000,
  159. 'upload_date': '20140616',
  160. 'duration': 4114,
  161. },
  162. 'add_ie': ['VoxMediaVolume'],
  163. }]
  164. def _real_extract(self, url):
  165. display_id = self._match_id(url)
  166. webpage = urllib.parse.unquote(self._download_webpage(url, display_id))
  167. def create_entry(provider_video_id, provider_video_type, title=None, description=None):
  168. video_url = {
  169. 'youtube': '%s',
  170. 'volume': 'http://volume.vox-cdn.com/embed/%s',
  171. }[provider_video_type] % provider_video_id
  172. return {
  173. '_type': 'url_transparent',
  174. 'url': video_url,
  175. 'title': title or self._og_search_title(webpage),
  176. 'description': description or self._og_search_description(webpage),
  177. }
  178. entries = []
  179. entries_data = self._search_regex([
  180. r'Chorus\.VideoContext\.addVideo\((\[{.+}\])\);',
  181. r'var\s+entry\s*=\s*({.+});',
  182. r'SBN\.VideoLinkset\.entryGroup\(\s*(\[.+\])',
  183. ], webpage, 'video data', default=None)
  184. if entries_data:
  185. entries_data = self._parse_json(entries_data, display_id)
  186. if isinstance(entries_data, dict):
  187. entries_data = [entries_data]
  188. for video_data in entries_data:
  189. provider_video_id = video_data.get('provider_video_id')
  190. provider_video_type = video_data.get('provider_video_type')
  191. if provider_video_id and provider_video_type:
  192. entries.append(create_entry(
  193. provider_video_id, provider_video_type,
  194. video_data.get('title'), video_data.get('description')))
  195. volume_uuid = self._search_regex(
  196. r'data-volume-uuid="([^"]+)"', webpage, 'volume uuid', default=None)
  197. if volume_uuid:
  198. entries.append(create_entry(volume_uuid, 'volume'))
  199. if len(entries) == 1:
  200. return entries[0]
  201. else:
  202. return self.playlist_result(entries, display_id, self._og_search_title(webpage), self._og_search_description(webpage))