logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

olympics.py (6671B)


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