cpac.py 5.6 KB

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