canalplus.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. # ExtractorError,
  4. # HEADRequest,
  5. int_or_none,
  6. qualities,
  7. unified_strdate,
  8. )
  9. class CanalplusIE(InfoExtractor):
  10. IE_DESC = 'mycanal.fr and piwiplus.fr'
  11. _VALID_URL = r'https?://(?:www\.)?(?P<site>mycanal|piwiplus)\.fr/(?:[^/]+/)*(?P<display_id>[^?/]+)(?:\.html\?.*\bvid=|/p/)(?P<id>\d+)'
  12. _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
  13. _SITE_ID_MAP = {
  14. 'mycanal': 'cplus',
  15. 'piwiplus': 'teletoon',
  16. }
  17. # Only works for direct mp4 URLs
  18. _GEO_COUNTRIES = ['FR']
  19. _TESTS = [{
  20. 'url': 'https://www.mycanal.fr/d17-emissions/lolywood/p/1397061',
  21. 'info_dict': {
  22. 'id': '1397061',
  23. 'display_id': 'lolywood',
  24. 'ext': 'mp4',
  25. 'title': 'Euro 2016 : Je préfère te prévenir - Lolywood - Episode 34',
  26. 'description': 'md5:7d97039d455cb29cdba0d652a0efaa5e',
  27. 'upload_date': '20160602',
  28. },
  29. }, {
  30. # geo restricted, bypassed
  31. 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
  32. 'info_dict': {
  33. 'id': '1108190',
  34. 'display_id': 'pid1405-le-labyrinthe-boing-super-ranger',
  35. 'ext': 'mp4',
  36. 'title': 'BOING SUPER RANGER - Ep : Le labyrinthe',
  37. 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
  38. 'upload_date': '20140724',
  39. },
  40. 'expected_warnings': ['HTTP Error 403: Forbidden'],
  41. }]
  42. def _real_extract(self, url):
  43. site, display_id, video_id = self._match_valid_url(url).groups()
  44. site_id = self._SITE_ID_MAP[site]
  45. info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
  46. video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
  47. if isinstance(video_data, list):
  48. video_data = next(video for video in video_data if video.get('ID') == video_id)
  49. media = video_data['MEDIA']
  50. infos = video_data['INFOS']
  51. preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
  52. # _, fmt_url = next(iter(media['VIDEOS'].items()))
  53. # if '/geo' in fmt_url.lower():
  54. # response = self._request_webpage(
  55. # HEADRequest(fmt_url), video_id,
  56. # 'Checking if the video is georestricted')
  57. # if '/blocage' in response.url:
  58. # raise ExtractorError(
  59. # 'The video is not available in your country',
  60. # expected=True)
  61. formats = []
  62. for format_id, format_url in media['VIDEOS'].items():
  63. if not format_url:
  64. continue
  65. if format_id == 'HLS':
  66. formats.extend(self._extract_m3u8_formats(
  67. format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
  68. elif format_id == 'HDS':
  69. formats.extend(self._extract_f4m_formats(
  70. format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
  71. else:
  72. formats.append({
  73. # the secret extracted from ya function in http://player.canalplus.fr/common/js/canalPlayer.js
  74. 'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
  75. 'format_id': format_id,
  76. 'quality': preference(format_id),
  77. })
  78. thumbnails = [{
  79. 'id': image_id,
  80. 'url': image_url,
  81. } for image_id, image_url in media.get('images', {}).items()]
  82. titrage = infos['TITRAGE']
  83. return {
  84. 'id': video_id,
  85. 'display_id': display_id,
  86. 'title': '{} - {}'.format(titrage['TITRE'], titrage['SOUS_TITRE']),
  87. 'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
  88. 'thumbnails': thumbnails,
  89. 'description': infos.get('DESCRIPTION'),
  90. 'duration': int_or_none(infos.get('DURATION')),
  91. 'view_count': int_or_none(infos.get('NB_VUES')),
  92. 'like_count': int_or_none(infos.get('NB_LIKES')),
  93. 'comment_count': int_or_none(infos.get('NB_COMMENTS')),
  94. 'formats': formats,
  95. }