olympics.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from .common import InfoExtractor
  2. from ..networking.exceptions import HTTPError
  3. from ..utils import (
  4. ExtractorError,
  5. int_or_none,
  6. parse_iso8601,
  7. try_get,
  8. url_or_none,
  9. )
  10. from ..utils.traversal import traverse_obj
  11. class OlympicsReplayIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?olympics\.com/[a-z]{2}/(?:paris-2024/)?(?:replay|videos?|original-series/episode)/(?P<id>[\w-]+)'
  13. _TESTS = [{
  14. 'url': 'https://olympics.com/fr/video/men-s-109kg-group-a-weightlifting-tokyo-2020-replays',
  15. 'info_dict': {
  16. 'id': 'f6a0753c-8e6f-4b7d-a435-027054a4f8e9',
  17. 'ext': 'mp4',
  18. 'title': '+109kg (H) Groupe A - Haltérophilie | Replay de Tokyo 2020',
  19. 'upload_date': '20210801',
  20. 'timestamp': 1627797600,
  21. 'description': 'md5:c66af4a5bc7429dbcc43d15845ff03b3',
  22. 'thumbnail': 'https://img.olympics.com/images/image/private/t_1-1_1280/primary/nua4o7zwyaznoaejpbk2',
  23. 'duration': 7017.0,
  24. },
  25. 'params': {
  26. 'skip_download': True,
  27. },
  28. }, {
  29. 'url': 'https://olympics.com/en/original-series/episode/b-boys-and-b-girls-take-the-spotlight-breaking-life-road-to-paris-2024',
  30. 'info_dict': {
  31. 'id': '32633650-c5ee-4280-8b94-fb6defb6a9b5',
  32. 'ext': 'mp4',
  33. 'title': 'B-girl Nicka - Breaking Life, Road to Paris 2024 | Episode 1',
  34. 'upload_date': '20240517',
  35. 'timestamp': 1715948200,
  36. 'description': 'md5:f63d728a41270ec628f6ac33ce471bb1',
  37. 'thumbnail': 'https://img.olympics.com/images/image/private/t_1-1_1280/primary/a3j96l7j6so3vyfijby1',
  38. 'duration': 1321.0,
  39. },
  40. }, {
  41. 'url': 'https://olympics.com/en/paris-2024/videos/men-s-preliminaries-gbr-esp-ned-rsa-hockey-olympic-games-paris-2024',
  42. 'info_dict': {
  43. 'id': '3d96db23-8eee-4b7c-8ef5-488a0361026c',
  44. 'ext': 'mp4',
  45. 'title': 'Men\'s Preliminaries GBR-ESP & NED-RSA | Hockey | Olympic Games Paris 2024',
  46. 'upload_date': '20240727',
  47. 'timestamp': 1722066600,
  48. },
  49. 'skip': 'Geo-restricted to RU, BR, BT, NP, TM, BD, TL',
  50. }, {
  51. 'url': 'https://olympics.com/en/paris-2024/videos/dnp-suni-lee-i-have-goals-and-i-have-expectations-for-myself-but-i-also-am-trying-to-give-myself-grace',
  52. 'info_dict': {
  53. 'id': 'a42f37ab-8a74-41d0-a7d9-af27b7b02a90',
  54. 'ext': 'mp4',
  55. 'title': 'md5:c7cfbc9918636a98e66400a812e4d407',
  56. 'upload_date': '20240729',
  57. 'timestamp': 1722288600,
  58. },
  59. }]
  60. _GEO_BYPASS = False
  61. def _extract_from_nextjs_data(self, webpage, video_id):
  62. data = traverse_obj(self._search_nextjs_data(webpage, video_id, default={}), (
  63. 'props', 'pageProps', 'page', 'items',
  64. lambda _, v: v['name'] == 'videoPlaylist', 'data', 'currentVideo', {dict}, any))
  65. if not data:
  66. return None
  67. geo_countries = traverse_obj(data, ('countries', ..., {str}))
  68. if traverse_obj(data, ('geoRestrictedVideo', {bool})):
  69. self.raise_geo_restricted(countries=geo_countries)
  70. is_live = traverse_obj(data, ('streamingStatus', {str})) == 'LIVE'
  71. m3u8_url = traverse_obj(data, ('videoUrl', {url_or_none})) or data['streamUrl']
  72. tokenized_url = m3u8_url if is_live else self._tokenize_url(m3u8_url, video_id)
  73. try:
  74. formats, subtitles = self._extract_m3u8_formats_and_subtitles(
  75. tokenized_url, video_id, 'mp4', m3u8_id='hls')
  76. except ExtractorError as e:
  77. if isinstance(e.cause, HTTPError) and 'georestricted' in e.cause.msg:
  78. self.raise_geo_restricted(countries=geo_countries)
  79. raise
  80. return {
  81. 'formats': formats,
  82. 'subtitles': subtitles,
  83. 'is_live': is_live,
  84. **traverse_obj(data, {
  85. 'id': ('videoID', {str}),
  86. 'title': ('title', {str}),
  87. 'timestamp': ('contentDate', {parse_iso8601}),
  88. }),
  89. }
  90. def _tokenize_url(self, url, video_id):
  91. return self._download_json(
  92. 'https://olympics.com/tokenGenerator', video_id,
  93. 'Downloading tokenized m3u8 url', query={'url': url})
  94. def _real_extract(self, url):
  95. video_id = self._match_id(url)
  96. webpage = self._download_webpage(url, video_id)
  97. if info := self._extract_from_nextjs_data(webpage, video_id):
  98. return info
  99. title = self._html_search_meta(('title', 'og:title', 'twitter:title'), webpage)
  100. video_uuid = self._html_search_meta('episode_uid', webpage)
  101. m3u8_url = self._html_search_meta('video_url', webpage)
  102. json_ld = self._search_json_ld(webpage, video_uuid)
  103. thumbnails_list = json_ld.get('image')
  104. if not thumbnails_list:
  105. thumbnails_list = self._html_search_regex(
  106. r'["\']image["\']:\s*["\']([^"\']+)["\']', webpage, 'images', default='')
  107. thumbnails_list = thumbnails_list.replace('[', '').replace(']', '').split(',')
  108. thumbnails_list = [thumbnail.strip() for thumbnail in thumbnails_list]
  109. thumbnails = []
  110. for thumbnail in thumbnails_list:
  111. width_a, height_a, width = self._search_regex(
  112. r'/images/image/private/t_(?P<width_a>\d+)-(?P<height_a>\d+)_(?P<width>\d+)/primary/[\W\w\d]+',
  113. thumbnail, 'thumb', group=(1, 2, 3), default=(None, None, None))
  114. width_a, height_a, width = int_or_none(width_a), int_or_none(height_a), int_or_none(width)
  115. thumbnails.append({
  116. 'url': thumbnail,
  117. 'width': width,
  118. 'height': int_or_none(try_get(width, lambda x: x * height_a / width_a)),
  119. })
  120. formats, subtitles = self._extract_m3u8_formats_and_subtitles(
  121. self._tokenize_url(m3u8_url, video_uuid), video_uuid, 'mp4', m3u8_id='hls')
  122. return {
  123. 'id': video_uuid,
  124. 'title': title,
  125. 'thumbnails': thumbnails,
  126. 'formats': formats,
  127. 'subtitles': subtitles,
  128. **json_ld,
  129. }