logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

cpac.py (5904B)


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