cpac.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. int_or_none,
  4. str_or_none,
  5. try_get,
  6. unified_timestamp,
  7. update_url_query,
  8. urljoin,
  9. )
  10. class CPACIE(InfoExtractor):
  11. IE_NAME = 'cpac'
  12. _VALID_URL = r'https?://(?:www\.)?cpac\.ca/(?P<fr>l-)?episode\?id=(?P<id>[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12})'
  13. _TEST = {
  14. # 'url': 'http://www.cpac.ca/en/programs/primetime-politics/episodes/65490909',
  15. 'url': 'https://www.cpac.ca/episode?id=fc7edcae-4660-47e1-ba61-5b7f29a9db0f',
  16. 'md5': 'e46ad699caafd7aa6024279f2614e8fa',
  17. 'info_dict': {
  18. 'id': 'fc7edcae-4660-47e1-ba61-5b7f29a9db0f',
  19. 'ext': 'mp4',
  20. 'upload_date': '20220215',
  21. 'title': 'News Conference to Celebrate National Kindness Week – February 15, 2022',
  22. 'description': 'md5:466a206abd21f3a6f776cdef290c23fb',
  23. 'timestamp': 1644901200,
  24. },
  25. 'params': {
  26. 'format': 'bestvideo',
  27. 'hls_prefer_native': True,
  28. },
  29. }
  30. def _real_extract(self, url):
  31. video_id = self._match_id(url)
  32. url_lang = 'fr' if '/l-episode?' in url else 'en'
  33. content = self._download_json(
  34. 'https://www.cpac.ca/api/1/services/contentModel.json?url=/site/website/episode/index.xml&crafterSite=cpacca&id=' + video_id,
  35. video_id)
  36. video_url = try_get(content, lambda x: x['page']['details']['videoUrl'], str)
  37. formats = []
  38. if video_url:
  39. content = content['page']
  40. title = str_or_none(content['details'][f'title_{url_lang}_t'])
  41. formats = self._extract_m3u8_formats(video_url, video_id, m3u8_id='hls', ext='mp4')
  42. for fmt in formats:
  43. # prefer language to match URL
  44. fmt_lang = fmt.get('language')
  45. if fmt_lang == url_lang:
  46. fmt['language_preference'] = 10
  47. elif not fmt_lang:
  48. fmt['language_preference'] = -1
  49. else:
  50. fmt['language_preference'] = -10
  51. category = str_or_none(content['details'][f'category_{url_lang}_t'])
  52. def is_live(v_type):
  53. return (v_type == 'live') if v_type is not None else None
  54. return {
  55. 'id': video_id,
  56. 'formats': formats,
  57. 'title': title,
  58. 'description': str_or_none(content['details'].get(f'description_{url_lang}_t')),
  59. 'timestamp': unified_timestamp(content['details'].get('liveDateTime')),
  60. 'categories': [category] if category else None,
  61. 'thumbnail': urljoin(url, str_or_none(content['details'].get(f'image_{url_lang}_s'))),
  62. 'is_live': is_live(content['details'].get('type')),
  63. }
  64. class CPACPlaylistIE(InfoExtractor):
  65. IE_NAME = 'cpac:playlist'
  66. _VALID_URL = r'(?i)https?://(?:www\.)?cpac\.ca/(?:program|search|(?P<fr>emission|rechercher))\?(?:[^&]+&)*?(?P<id>(?:id=\d+|programId=\d+|key=[^&]+))'
  67. _TESTS = [{
  68. 'url': 'https://www.cpac.ca/program?id=6',
  69. 'info_dict': {
  70. 'id': 'id=6',
  71. 'title': 'Headline Politics',
  72. 'description': 'Watch CPAC’s signature long-form coverage of the day’s pressing political events as they unfold.',
  73. },
  74. 'playlist_count': 10,
  75. }, {
  76. 'url': 'https://www.cpac.ca/search?key=hudson&type=all&order=desc',
  77. 'info_dict': {
  78. 'id': 'key=hudson',
  79. 'title': 'hudson',
  80. },
  81. 'playlist_count': 22,
  82. }, {
  83. 'url': 'https://www.cpac.ca/search?programId=50',
  84. 'info_dict': {
  85. 'id': 'programId=50',
  86. 'title': '50',
  87. },
  88. 'playlist_count': 9,
  89. }, {
  90. 'url': 'https://www.cpac.ca/emission?id=6',
  91. 'only_matching': True,
  92. }, {
  93. 'url': 'https://www.cpac.ca/rechercher?key=hudson&type=all&order=desc',
  94. 'only_matching': True,
  95. }]
  96. def _real_extract(self, url):
  97. video_id = self._match_id(url)
  98. url_lang = 'fr' if any(x in url for x in ('/emission?', '/rechercher?')) else 'en'
  99. pl_type, list_type = ('program', 'itemList') if any(x in url for x in ('/program?', '/emission?')) else ('search', 'searchResult')
  100. api_url = (
  101. f'https://www.cpac.ca/api/1/services/contentModel.json?url=/site/website/{pl_type}/index.xml&crafterSite=cpacca&{video_id}')
  102. content = self._download_json(api_url, video_id)
  103. entries = []
  104. total_pages = int_or_none(try_get(content, lambda x: x['page'][list_type]['totalPages']), default=1)
  105. for page in range(1, total_pages + 1):
  106. if page > 1:
  107. api_url = update_url_query(api_url, {'page': page})
  108. content = self._download_json(
  109. api_url, video_id,
  110. note=f'Downloading continuation - {page}',
  111. fatal=False)
  112. for item in try_get(content, lambda x: x['page'][list_type]['item'], list) or []:
  113. episode_url = urljoin(url, try_get(item, lambda x: x[f'url_{url_lang}_s']))
  114. if episode_url:
  115. entries.append(episode_url)
  116. return self.playlist_result(
  117. (self.url_result(entry) for entry in entries),
  118. playlist_id=video_id,
  119. playlist_title=try_get(content, lambda x: x['page']['program'][f'title_{url_lang}_t']) or video_id.split('=')[-1],
  120. playlist_description=try_get(content, lambda x: x['page']['program'][f'description_{url_lang}_t']),
  121. )