You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.1 KiB

  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import compat_str
  4. from ..utils import (
  5. determine_ext,
  6. int_or_none,
  7. unified_timestamp,
  8. )
  9. class ESPNIE(InfoExtractor):
  10. _VALID_URL = r'''(?x)
  11. https?://
  12. (?:
  13. (?:(?:\w+\.)+)?espn\.go|
  14. (?:www\.)?espn
  15. )\.com/
  16. (?:
  17. (?:
  18. video/clip|
  19. watch/player
  20. )
  21. (?:
  22. \?.*?\bid=|
  23. /_/id/
  24. )
  25. )
  26. (?P<id>\d+)
  27. '''
  28. _TESTS = [{
  29. 'url': 'http://espn.go.com/video/clip?id=10365079',
  30. 'info_dict': {
  31. 'id': '10365079',
  32. 'ext': 'mp4',
  33. 'title': '30 for 30 Shorts: Judging Jewell',
  34. 'description': 'md5:39370c2e016cb4ecf498ffe75bef7f0f',
  35. 'timestamp': 1390936111,
  36. 'upload_date': '20140128',
  37. },
  38. 'params': {
  39. 'skip_download': True,
  40. },
  41. }, {
  42. 'url': 'https://broadband.espn.go.com/video/clip?id=18910086',
  43. 'info_dict': {
  44. 'id': '18910086',
  45. 'ext': 'mp4',
  46. 'title': 'Kyrie spins around defender for two',
  47. 'description': 'md5:2b0f5bae9616d26fba8808350f0d2b9b',
  48. 'timestamp': 1489539155,
  49. 'upload_date': '20170315',
  50. },
  51. 'params': {
  52. 'skip_download': True,
  53. },
  54. 'expected_warnings': ['Unable to download f4m manifest'],
  55. }, {
  56. 'url': 'http://nonredline.sports.espn.go.com/video/clip?id=19744672',
  57. 'only_matching': True,
  58. }, {
  59. 'url': 'https://cdn.espn.go.com/video/clip/_/id/19771774',
  60. 'only_matching': True,
  61. }, {
  62. 'url': 'http://www.espn.com/watch/player?id=19141491',
  63. 'only_matching': True,
  64. }, {
  65. 'url': 'http://www.espn.com/watch/player?bucketId=257&id=19505875',
  66. 'only_matching': True,
  67. }, {
  68. 'url': 'http://www.espn.com/watch/player/_/id/19141491',
  69. 'only_matching': True,
  70. }, {
  71. 'url': 'http://www.espn.com/video/clip?id=10365079',
  72. 'only_matching': True,
  73. }, {
  74. 'url': 'http://www.espn.com/video/clip/_/id/17989860',
  75. 'only_matching': True,
  76. }]
  77. def _real_extract(self, url):
  78. video_id = self._match_id(url)
  79. clip = self._download_json(
  80. 'http://api-app.espn.com/v1/video/clips/%s' % video_id,
  81. video_id)['videos'][0]
  82. title = clip['headline']
  83. format_urls = set()
  84. formats = []
  85. def traverse_source(source, base_source_id=None):
  86. for source_id, source in source.items():
  87. if isinstance(source, compat_str):
  88. extract_source(source, base_source_id)
  89. elif isinstance(source, dict):
  90. traverse_source(
  91. source,
  92. '%s-%s' % (base_source_id, source_id)
  93. if base_source_id else source_id)
  94. def extract_source(source_url, source_id=None):
  95. if source_url in format_urls:
  96. return
  97. format_urls.add(source_url)
  98. ext = determine_ext(source_url)
  99. if ext == 'smil':
  100. formats.extend(self._extract_smil_formats(
  101. source_url, video_id, fatal=False))
  102. elif ext == 'f4m':
  103. formats.extend(self._extract_f4m_formats(
  104. source_url, video_id, f4m_id=source_id, fatal=False))
  105. elif ext == 'm3u8':
  106. formats.extend(self._extract_m3u8_formats(
  107. source_url, video_id, 'mp4', entry_protocol='m3u8_native',
  108. m3u8_id=source_id, fatal=False))
  109. else:
  110. formats.append({
  111. 'url': source_url,
  112. 'format_id': source_id,
  113. })
  114. traverse_source(clip['links']['source'])
  115. self._sort_formats(formats)
  116. description = clip.get('caption') or clip.get('description')
  117. thumbnail = clip.get('thumbnail')
  118. duration = int_or_none(clip.get('duration'))
  119. timestamp = unified_timestamp(clip.get('originalPublishDate'))
  120. return {
  121. 'id': video_id,
  122. 'title': title,
  123. 'description': description,
  124. 'thumbnail': thumbnail,
  125. 'timestamp': timestamp,
  126. 'duration': duration,
  127. 'formats': formats,
  128. }
  129. class ESPNArticleIE(InfoExtractor):
  130. _VALID_URL = r'https?://(?:espn\.go|(?:www\.)?espn)\.com/(?:[^/]+/)*(?P<id>[^/]+)'
  131. _TESTS = [{
  132. 'url': 'https://espn.go.com/video/iframe/twitter/?cms=espn&id=10365079',
  133. 'only_matching': True,
  134. }, {
  135. 'url': 'http://espn.go.com/nba/recap?gameId=400793786',
  136. 'only_matching': True,
  137. }, {
  138. 'url': 'http://espn.go.com/blog/golden-state-warriors/post/_/id/593/how-warriors-rapidly-regained-a-winning-edge',
  139. 'only_matching': True,
  140. }, {
  141. 'url': 'http://espn.go.com/sports/endurance/story/_/id/12893522/dzhokhar-tsarnaev-sentenced-role-boston-marathon-bombings',
  142. 'only_matching': True,
  143. }, {
  144. 'url': 'http://espn.go.com/nba/playoffs/2015/story/_/id/12887571/john-wall-washington-wizards-no-swelling-left-hand-wrist-game-5-return',
  145. 'only_matching': True,
  146. }]
  147. @classmethod
  148. def suitable(cls, url):
  149. return False if ESPNIE.suitable(url) else super(ESPNArticleIE, cls).suitable(url)
  150. def _real_extract(self, url):
  151. video_id = self._match_id(url)
  152. webpage = self._download_webpage(url, video_id)
  153. video_id = self._search_regex(
  154. r'class=(["\']).*?video-play-button.*?\1[^>]+data-id=["\'](?P<id>\d+)',
  155. webpage, 'video id', group='id')
  156. return self.url_result(
  157. 'http://espn.go.com/video/clip?id=%s' % video_id, ESPNIE.ie_key())