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.

238 lines
8.5 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from .once import OnceIE
  5. from ..compat import compat_str
  6. from ..utils import (
  7. determine_ext,
  8. int_or_none,
  9. unified_timestamp,
  10. )
  11. class ESPNIE(OnceIE):
  12. _VALID_URL = r'''(?x)
  13. https?://
  14. (?:
  15. (?:
  16. (?:
  17. (?:(?:\w+\.)+)?espn\.go|
  18. (?:www\.)?espn
  19. )\.com/
  20. (?:
  21. (?:
  22. video/(?:clip|iframe/twitter)|
  23. watch/player
  24. )
  25. (?:
  26. .*?\?.*?\bid=|
  27. /_/id/
  28. )
  29. )
  30. )|
  31. (?:www\.)espnfc\.(?:com|us)/(?:video/)?[^/]+/\d+/video/
  32. )
  33. (?P<id>\d+)
  34. '''
  35. _TESTS = [{
  36. 'url': 'http://espn.go.com/video/clip?id=10365079',
  37. 'info_dict': {
  38. 'id': '10365079',
  39. 'ext': 'mp4',
  40. 'title': '30 for 30 Shorts: Judging Jewell',
  41. 'description': 'md5:39370c2e016cb4ecf498ffe75bef7f0f',
  42. 'timestamp': 1390936111,
  43. 'upload_date': '20140128',
  44. },
  45. 'params': {
  46. 'skip_download': True,
  47. },
  48. }, {
  49. 'url': 'https://broadband.espn.go.com/video/clip?id=18910086',
  50. 'info_dict': {
  51. 'id': '18910086',
  52. 'ext': 'mp4',
  53. 'title': 'Kyrie spins around defender for two',
  54. 'description': 'md5:2b0f5bae9616d26fba8808350f0d2b9b',
  55. 'timestamp': 1489539155,
  56. 'upload_date': '20170315',
  57. },
  58. 'params': {
  59. 'skip_download': True,
  60. },
  61. 'expected_warnings': ['Unable to download f4m manifest'],
  62. }, {
  63. 'url': 'http://nonredline.sports.espn.go.com/video/clip?id=19744672',
  64. 'only_matching': True,
  65. }, {
  66. 'url': 'https://cdn.espn.go.com/video/clip/_/id/19771774',
  67. 'only_matching': True,
  68. }, {
  69. 'url': 'http://www.espn.com/watch/player?id=19141491',
  70. 'only_matching': True,
  71. }, {
  72. 'url': 'http://www.espn.com/watch/player?bucketId=257&id=19505875',
  73. 'only_matching': True,
  74. }, {
  75. 'url': 'http://www.espn.com/watch/player/_/id/19141491',
  76. 'only_matching': True,
  77. }, {
  78. 'url': 'http://www.espn.com/video/clip?id=10365079',
  79. 'only_matching': True,
  80. }, {
  81. 'url': 'http://www.espn.com/video/clip/_/id/17989860',
  82. 'only_matching': True,
  83. }, {
  84. 'url': 'https://espn.go.com/video/iframe/twitter/?cms=espn&id=10365079',
  85. 'only_matching': True,
  86. }, {
  87. 'url': 'http://www.espnfc.us/video/espn-fc-tv/86/video/3319154/nashville-unveiled-as-the-newest-club-in-mls',
  88. 'only_matching': True,
  89. }, {
  90. 'url': 'http://www.espnfc.com/english-premier-league/23/video/3324163/premier-league-in-90-seconds-golden-tweets',
  91. 'only_matching': True,
  92. }]
  93. def _real_extract(self, url):
  94. video_id = self._match_id(url)
  95. clip = self._download_json(
  96. 'http://api-app.espn.com/v1/video/clips/%s' % video_id,
  97. video_id)['videos'][0]
  98. title = clip['headline']
  99. format_urls = set()
  100. formats = []
  101. def traverse_source(source, base_source_id=None):
  102. for source_id, source in source.items():
  103. if source_id == 'alert':
  104. continue
  105. elif isinstance(source, compat_str):
  106. extract_source(source, base_source_id)
  107. elif isinstance(source, dict):
  108. traverse_source(
  109. source,
  110. '%s-%s' % (base_source_id, source_id)
  111. if base_source_id else source_id)
  112. def extract_source(source_url, source_id=None):
  113. if source_url in format_urls:
  114. return
  115. format_urls.add(source_url)
  116. ext = determine_ext(source_url)
  117. if OnceIE.suitable(source_url):
  118. formats.extend(self._extract_once_formats(source_url))
  119. elif ext == 'smil':
  120. formats.extend(self._extract_smil_formats(
  121. source_url, video_id, fatal=False))
  122. elif ext == 'f4m':
  123. formats.extend(self._extract_f4m_formats(
  124. source_url, video_id, f4m_id=source_id, fatal=False))
  125. elif ext == 'm3u8':
  126. formats.extend(self._extract_m3u8_formats(
  127. source_url, video_id, 'mp4', entry_protocol='m3u8_native',
  128. m3u8_id=source_id, fatal=False))
  129. else:
  130. f = {
  131. 'url': source_url,
  132. 'format_id': source_id,
  133. }
  134. mobj = re.search(r'(\d+)p(\d+)_(\d+)k\.', source_url)
  135. if mobj:
  136. f.update({
  137. 'height': int(mobj.group(1)),
  138. 'fps': int(mobj.group(2)),
  139. 'tbr': int(mobj.group(3)),
  140. })
  141. if source_id == 'mezzanine':
  142. f['preference'] = 1
  143. formats.append(f)
  144. links = clip.get('links', {})
  145. traverse_source(links.get('source', {}))
  146. traverse_source(links.get('mobile', {}))
  147. self._sort_formats(formats)
  148. description = clip.get('caption') or clip.get('description')
  149. thumbnail = clip.get('thumbnail')
  150. duration = int_or_none(clip.get('duration'))
  151. timestamp = unified_timestamp(clip.get('originalPublishDate'))
  152. return {
  153. 'id': video_id,
  154. 'title': title,
  155. 'description': description,
  156. 'thumbnail': thumbnail,
  157. 'timestamp': timestamp,
  158. 'duration': duration,
  159. 'formats': formats,
  160. }
  161. class ESPNArticleIE(InfoExtractor):
  162. _VALID_URL = r'https?://(?:espn\.go|(?:www\.)?espn)\.com/(?:[^/]+/)*(?P<id>[^/]+)'
  163. _TESTS = [{
  164. 'url': 'http://espn.go.com/nba/recap?gameId=400793786',
  165. 'only_matching': True,
  166. }, {
  167. 'url': 'http://espn.go.com/blog/golden-state-warriors/post/_/id/593/how-warriors-rapidly-regained-a-winning-edge',
  168. 'only_matching': True,
  169. }, {
  170. 'url': 'http://espn.go.com/sports/endurance/story/_/id/12893522/dzhokhar-tsarnaev-sentenced-role-boston-marathon-bombings',
  171. 'only_matching': True,
  172. }, {
  173. 'url': 'http://espn.go.com/nba/playoffs/2015/story/_/id/12887571/john-wall-washington-wizards-no-swelling-left-hand-wrist-game-5-return',
  174. 'only_matching': True,
  175. }]
  176. @classmethod
  177. def suitable(cls, url):
  178. return False if ESPNIE.suitable(url) else super(ESPNArticleIE, cls).suitable(url)
  179. def _real_extract(self, url):
  180. video_id = self._match_id(url)
  181. webpage = self._download_webpage(url, video_id)
  182. video_id = self._search_regex(
  183. r'class=(["\']).*?video-play-button.*?\1[^>]+data-id=["\'](?P<id>\d+)',
  184. webpage, 'video id', group='id')
  185. return self.url_result(
  186. 'http://espn.go.com/video/clip?id=%s' % video_id, ESPNIE.ie_key())
  187. class FiveThirtyEightIE(InfoExtractor):
  188. _VALID_URL = r'https?://(?:www\.)?fivethirtyeight\.com/features/(?P<id>[^/?#]+)'
  189. _TEST = {
  190. 'url': 'http://fivethirtyeight.com/features/how-the-6-8-raiders-can-still-make-the-playoffs/',
  191. 'info_dict': {
  192. 'id': '21846851',
  193. 'ext': 'mp4',
  194. 'title': 'FiveThirtyEight: The Raiders can still make the playoffs',
  195. 'description': 'Neil Paine breaks down the simplest scenario that will put the Raiders into the playoffs at 8-8.',
  196. 'timestamp': 1513960621,
  197. 'upload_date': '20171222',
  198. },
  199. 'params': {
  200. 'skip_download': True,
  201. },
  202. 'expected_warnings': ['Unable to download f4m manifest'],
  203. }
  204. def _real_extract(self, url):
  205. video_id = self._match_id(url)
  206. webpage = self._download_webpage(url, video_id)
  207. video_id = self._search_regex(
  208. r'data-video-id=["\'](?P<id>\d+)',
  209. webpage, 'video id', group='id')
  210. return self.url_result(
  211. 'http://espn.go.com/video/clip?id=%s' % video_id, ESPNIE.ie_key())