logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

mlb.py (9355B)


  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. int_or_none,
  7. parse_duration,
  8. parse_iso8601,
  9. try_get,
  10. )
  11. class MLBBaseIE(InfoExtractor):
  12. def _real_extract(self, url):
  13. display_id = self._match_id(url)
  14. video = self._download_video_data(display_id)
  15. video_id = video['id']
  16. title = video['title']
  17. feed = self._get_feed(video)
  18. formats = []
  19. for playback in (feed.get('playbacks') or []):
  20. playback_url = playback.get('url')
  21. if not playback_url:
  22. continue
  23. name = playback.get('name')
  24. ext = determine_ext(playback_url)
  25. if ext == 'm3u8':
  26. formats.extend(self._extract_m3u8_formats(
  27. playback_url, video_id, 'mp4',
  28. 'm3u8_native', m3u8_id=name, fatal=False))
  29. else:
  30. f = {
  31. 'format_id': name,
  32. 'url': playback_url,
  33. }
  34. mobj = re.search(r'_(\d+)K_(\d+)X(\d+)', name)
  35. if mobj:
  36. f.update({
  37. 'height': int(mobj.group(3)),
  38. 'tbr': int(mobj.group(1)),
  39. 'width': int(mobj.group(2)),
  40. })
  41. mobj = re.search(r'_(\d+)x(\d+)_(\d+)_(\d+)K\.mp4', playback_url)
  42. if mobj:
  43. f.update({
  44. 'fps': int(mobj.group(3)),
  45. 'height': int(mobj.group(2)),
  46. 'tbr': int(mobj.group(4)),
  47. 'width': int(mobj.group(1)),
  48. })
  49. formats.append(f)
  50. self._sort_formats(formats)
  51. thumbnails = []
  52. for cut in (try_get(feed, lambda x: x['image']['cuts'], list) or []):
  53. src = cut.get('src')
  54. if not src:
  55. continue
  56. thumbnails.append({
  57. 'height': int_or_none(cut.get('height')),
  58. 'url': src,
  59. 'width': int_or_none(cut.get('width')),
  60. })
  61. language = (video.get('language') or 'EN').lower()
  62. return {
  63. 'id': video_id,
  64. 'title': title,
  65. 'formats': formats,
  66. 'description': video.get('description'),
  67. 'duration': parse_duration(feed.get('duration')),
  68. 'thumbnails': thumbnails,
  69. 'timestamp': parse_iso8601(video.get(self._TIMESTAMP_KEY)),
  70. 'subtitles': self._extract_mlb_subtitles(feed, language),
  71. }
  72. class MLBIE(MLBBaseIE):
  73. _VALID_URL = r'''(?x)
  74. https?://
  75. (?:[\da-z_-]+\.)*mlb\.com/
  76. (?:
  77. (?:
  78. (?:[^/]+/)*video/[^/]+/c-|
  79. (?:
  80. shared/video/embed/(?:embed|m-internal-embed)\.html|
  81. (?:[^/]+/)+(?:play|index)\.jsp|
  82. )\?.*?\bcontent_id=
  83. )
  84. (?P<id>\d+)
  85. )
  86. '''
  87. _TESTS = [
  88. {
  89. 'url': 'https://www.mlb.com/mariners/video/ackleys-spectacular-catch/c-34698933',
  90. 'md5': '632358dacfceec06bad823b83d21df2d',
  91. 'info_dict': {
  92. 'id': '34698933',
  93. 'ext': 'mp4',
  94. 'title': "Ackley's spectacular catch",
  95. 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
  96. 'duration': 66,
  97. 'timestamp': 1405995000,
  98. 'upload_date': '20140722',
  99. 'thumbnail': r're:^https?://.*\.jpg$',
  100. },
  101. },
  102. {
  103. 'url': 'https://www.mlb.com/video/stanton-prepares-for-derby/c-34496663',
  104. 'md5': 'bf2619bf9cacc0a564fc35e6aeb9219f',
  105. 'info_dict': {
  106. 'id': '34496663',
  107. 'ext': 'mp4',
  108. 'title': 'Stanton prepares for Derby',
  109. 'description': 'md5:d00ce1e5fd9c9069e9c13ab4faedfa57',
  110. 'duration': 46,
  111. 'timestamp': 1405120200,
  112. 'upload_date': '20140711',
  113. 'thumbnail': r're:^https?://.*\.jpg$',
  114. },
  115. },
  116. {
  117. 'url': 'https://www.mlb.com/video/cespedes-repeats-as-derby-champ/c-34578115',
  118. 'md5': '99bb9176531adc600b90880fb8be9328',
  119. 'info_dict': {
  120. 'id': '34578115',
  121. 'ext': 'mp4',
  122. 'title': 'Cespedes repeats as Derby champ',
  123. 'description': 'md5:08df253ce265d4cf6fb09f581fafad07',
  124. 'duration': 488,
  125. 'timestamp': 1405414336,
  126. 'upload_date': '20140715',
  127. 'thumbnail': r're:^https?://.*\.jpg$',
  128. },
  129. },
  130. {
  131. 'url': 'https://www.mlb.com/video/bautista-on-home-run-derby/c-34577915',
  132. 'md5': 'da8b57a12b060e7663ee1eebd6f330ec',
  133. 'info_dict': {
  134. 'id': '34577915',
  135. 'ext': 'mp4',
  136. 'title': 'Bautista on Home Run Derby',
  137. 'description': 'md5:b80b34031143d0986dddc64a8839f0fb',
  138. 'duration': 52,
  139. 'timestamp': 1405405122,
  140. 'upload_date': '20140715',
  141. 'thumbnail': r're:^https?://.*\.jpg$',
  142. },
  143. },
  144. {
  145. 'url': 'https://www.mlb.com/video/hargrove-homers-off-caldwell/c-1352023483?tid=67793694',
  146. 'only_matching': True,
  147. },
  148. {
  149. 'url': 'http://m.mlb.com/shared/video/embed/embed.html?content_id=35692085&topic_id=6479266&width=400&height=224&property=mlb',
  150. 'only_matching': True,
  151. },
  152. {
  153. 'url': 'http://mlb.mlb.com/shared/video/embed/embed.html?content_id=36599553',
  154. 'only_matching': True,
  155. },
  156. {
  157. 'url': 'http://mlb.mlb.com/es/video/play.jsp?content_id=36599553',
  158. 'only_matching': True,
  159. },
  160. {
  161. 'url': 'https://www.mlb.com/cardinals/video/piscottys-great-sliding-catch/c-51175783',
  162. 'only_matching': True,
  163. },
  164. {
  165. # From http://m.mlb.com/news/article/118550098/blue-jays-kevin-pillar-goes-spidey-up-the-wall-to-rob-tim-beckham-of-a-homer
  166. 'url': 'http://mlb.mlb.com/shared/video/embed/m-internal-embed.html?content_id=75609783&property=mlb&autoplay=true&hashmode=false&siteSection=mlb/multimedia/article_118550098/article_embed&club=mlb',
  167. 'only_matching': True,
  168. },
  169. ]
  170. _TIMESTAMP_KEY = 'date'
  171. @staticmethod
  172. def _get_feed(video):
  173. return video
  174. @staticmethod
  175. def _extract_mlb_subtitles(feed, language):
  176. subtitles = {}
  177. for keyword in (feed.get('keywordsAll') or []):
  178. keyword_type = keyword.get('type')
  179. if keyword_type and keyword_type.startswith('closed_captions_location_'):
  180. cc_location = keyword.get('value')
  181. if cc_location:
  182. subtitles.setdefault(language, []).append({
  183. 'url': cc_location,
  184. })
  185. return subtitles
  186. def _download_video_data(self, display_id):
  187. return self._download_json(
  188. 'http://content.mlb.com/mlb/item/id/v1/%s/details/web-v1.json' % display_id,
  189. display_id)
  190. class MLBVideoIE(MLBBaseIE):
  191. _VALID_URL = r'https?://(?:www\.)?mlb\.com/(?:[^/]+/)*video/(?P<id>[^/?&#]+)'
  192. _TEST = {
  193. 'url': 'https://www.mlb.com/mariners/video/ackley-s-spectacular-catch-c34698933',
  194. 'md5': '632358dacfceec06bad823b83d21df2d',
  195. 'info_dict': {
  196. 'id': 'c04a8863-f569-42e6-9f87-992393657614',
  197. 'ext': 'mp4',
  198. 'title': "Ackley's spectacular catch",
  199. 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
  200. 'duration': 66,
  201. 'timestamp': 1405995000,
  202. 'upload_date': '20140722',
  203. 'thumbnail': r're:^https?://.+',
  204. },
  205. }
  206. _TIMESTAMP_KEY = 'timestamp'
  207. @classmethod
  208. def suitable(cls, url):
  209. return False if MLBIE.suitable(url) else super(MLBVideoIE, cls).suitable(url)
  210. @staticmethod
  211. def _get_feed(video):
  212. return video['feeds'][0]
  213. @staticmethod
  214. def _extract_mlb_subtitles(feed, language):
  215. subtitles = {}
  216. for cc_location in (feed.get('closedCaptions') or []):
  217. subtitles.setdefault(language, []).append({
  218. 'url': cc_location,
  219. })
  220. def _download_video_data(self, display_id):
  221. # https://www.mlb.com/data-service/en/videos/[SLUG]
  222. return self._download_json(
  223. 'https://fastball-gateway.mlb.com/graphql',
  224. display_id, query={
  225. 'query': '''{
  226. mediaPlayback(ids: "%s") {
  227. description
  228. feeds(types: CMS) {
  229. closedCaptions
  230. duration
  231. image {
  232. cuts {
  233. width
  234. height
  235. src
  236. }
  237. }
  238. playbacks {
  239. name
  240. url
  241. }
  242. }
  243. id
  244. timestamp
  245. title
  246. }
  247. }''' % display_id,
  248. })['data']['mediaPlayback'][0]