olympics.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from .common import InfoExtractor
  2. from ..utils import int_or_none, try_get
  3. class OlympicsReplayIE(InfoExtractor):
  4. _VALID_URL = r'https?://(?:www\.)?olympics\.com(?:/tokyo-2020)?/[a-z]{2}/(?:replay|video)/(?P<id>[^/#&?]+)'
  5. _TESTS = [{
  6. 'url': 'https://olympics.com/fr/video/men-s-109kg-group-a-weightlifting-tokyo-2020-replays',
  7. 'info_dict': {
  8. 'id': 'f6a0753c-8e6f-4b7d-a435-027054a4f8e9',
  9. 'ext': 'mp4',
  10. 'title': '+109kg (H) Groupe A - Haltérophilie | Replay de Tokyo 2020',
  11. 'upload_date': '20210801',
  12. 'timestamp': 1627783200,
  13. 'description': 'md5:c66af4a5bc7429dbcc43d15845ff03b3',
  14. 'uploader': 'International Olympic Committee',
  15. },
  16. 'params': {
  17. 'skip_download': True,
  18. },
  19. }, {
  20. 'url': 'https://olympics.com/tokyo-2020/en/replay/bd242924-4b22-49a5-a846-f1d4c809250d/mens-bronze-medal-match-hun-esp',
  21. 'only_matching': True,
  22. }]
  23. def _real_extract(self, url):
  24. video_id = self._match_id(url)
  25. webpage = self._download_webpage(url, video_id)
  26. title = self._html_search_meta(('title', 'og:title', 'twitter:title'), webpage)
  27. uuid = self._html_search_meta('episode_uid', webpage)
  28. m3u8_url = self._html_search_meta('video_url', webpage)
  29. json_ld = self._search_json_ld(webpage, uuid)
  30. thumbnails_list = json_ld.get('image')
  31. if not thumbnails_list:
  32. thumbnails_list = self._html_search_regex(
  33. r'["\']image["\']:\s*["\']([^"\']+)["\']', webpage, 'images', default='')
  34. thumbnails_list = thumbnails_list.replace('[', '').replace(']', '').split(',')
  35. thumbnails_list = [thumbnail.strip() for thumbnail in thumbnails_list]
  36. thumbnails = []
  37. for thumbnail in thumbnails_list:
  38. width_a, height_a, width = self._search_regex(
  39. r'/images/image/private/t_(?P<width_a>\d+)-(?P<height_a>\d+)_(?P<width>\d+)/primary/[\W\w\d]+',
  40. thumbnail, 'thumb', group=(1, 2, 3), default=(None, None, None))
  41. width_a, height_a, width = int_or_none(width_a), int_or_none(height_a), int_or_none(width)
  42. thumbnails.append({
  43. 'url': thumbnail,
  44. 'width': width,
  45. 'height': int_or_none(try_get(width, lambda x: x * height_a / width_a)),
  46. })
  47. m3u8_url = self._download_json(
  48. f'https://olympics.com/tokenGenerator?url={m3u8_url}', uuid, note='Downloading m3u8 url')
  49. formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, uuid, 'mp4', m3u8_id='hls')
  50. return {
  51. 'id': uuid,
  52. 'title': title,
  53. 'thumbnails': thumbnails,
  54. 'formats': formats,
  55. 'subtitles': subtitles,
  56. **json_ld,
  57. }