You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. float_or_none,
  8. int_or_none,
  9. parse_duration,
  10. )
  11. class CDAIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:(?:www\.)?cda\.pl/video|ebd\.cda\.pl/[0-9]+x[0-9]+)/(?P<id>[0-9a-z]+)'
  13. _BASE_URL = 'http://www.cda.pl/'
  14. _TESTS = [{
  15. 'url': 'http://www.cda.pl/video/5749950c',
  16. 'md5': '6f844bf51b15f31fae165365707ae970',
  17. 'info_dict': {
  18. 'id': '5749950c',
  19. 'ext': 'mp4',
  20. 'height': 720,
  21. 'title': 'Oto dlaczego przed zakrętem należy zwolnić.',
  22. 'description': 'md5:269ccd135d550da90d1662651fcb9772',
  23. 'thumbnail': r're:^https?://.*\.jpg$',
  24. 'average_rating': float,
  25. 'duration': 39
  26. }
  27. }, {
  28. 'url': 'http://www.cda.pl/video/57413289',
  29. 'md5': 'a88828770a8310fc00be6c95faf7f4d5',
  30. 'info_dict': {
  31. 'id': '57413289',
  32. 'ext': 'mp4',
  33. 'title': 'Lądowanie na lotnisku na Maderze',
  34. 'description': 'md5:60d76b71186dcce4e0ba6d4bbdb13e1a',
  35. 'thumbnail': r're:^https?://.*\.jpg$',
  36. 'uploader': 'crash404',
  37. 'view_count': int,
  38. 'average_rating': float,
  39. 'duration': 137
  40. }
  41. }, {
  42. 'url': 'http://ebd.cda.pl/0x0/5749950c',
  43. 'only_matching': True,
  44. }]
  45. def _real_extract(self, url):
  46. video_id = self._match_id(url)
  47. self._set_cookie('cda.pl', 'cda.player', 'html5')
  48. webpage = self._download_webpage(
  49. self._BASE_URL + '/video/' + video_id, video_id)
  50. if 'Ten film jest dostępny dla użytkowników premium' in webpage:
  51. raise ExtractorError('This video is only available for premium users.', expected=True)
  52. formats = []
  53. uploader = self._search_regex(r'''(?x)
  54. <(span|meta)[^>]+itemprop=(["\'])author\2[^>]*>
  55. (?:<\1[^>]*>[^<]*</\1>|(?!</\1>)(?:.|\n))*?
  56. <(span|meta)[^>]+itemprop=(["\'])name\4[^>]*>(?P<uploader>[^<]+)</\3>
  57. ''', webpage, 'uploader', default=None, group='uploader')
  58. view_count = self._search_regex(
  59. r'Odsłony:(?:\s|&nbsp;)*([0-9]+)', webpage,
  60. 'view_count', default=None)
  61. average_rating = self._search_regex(
  62. r'<(?:span|meta)[^>]+itemprop=(["\'])ratingValue\1[^>]*>(?P<rating_value>[0-9.]+)',
  63. webpage, 'rating', fatal=False, group='rating_value')
  64. info_dict = {
  65. 'id': video_id,
  66. 'title': self._og_search_title(webpage),
  67. 'description': self._og_search_description(webpage),
  68. 'uploader': uploader,
  69. 'view_count': int_or_none(view_count),
  70. 'average_rating': float_or_none(average_rating),
  71. 'thumbnail': self._og_search_thumbnail(webpage),
  72. 'formats': formats,
  73. 'duration': None,
  74. }
  75. def extract_format(page, version):
  76. json_str = self._search_regex(
  77. r'player_data=(\\?["\'])(?P<player_data>.+?)\1', page,
  78. '%s player_json' % version, fatal=False, group='player_data')
  79. if not json_str:
  80. return
  81. player_data = self._parse_json(
  82. json_str, '%s player_data' % version, fatal=False)
  83. if not player_data:
  84. return
  85. video = player_data.get('video')
  86. if not video or 'file' not in video:
  87. self.report_warning('Unable to extract %s version information' % version)
  88. return
  89. f = {
  90. 'url': video['file'],
  91. }
  92. m = re.search(
  93. r'<a[^>]+data-quality="(?P<format_id>[^"]+)"[^>]+href="[^"]+"[^>]+class="[^"]*quality-btn-active[^"]*">(?P<height>[0-9]+)p',
  94. page)
  95. if m:
  96. f.update({
  97. 'format_id': m.group('format_id'),
  98. 'height': int(m.group('height')),
  99. })
  100. info_dict['formats'].append(f)
  101. if not info_dict['duration']:
  102. info_dict['duration'] = parse_duration(video.get('duration'))
  103. extract_format(webpage, 'default')
  104. for href, resolution in re.findall(
  105. r'<a[^>]+data-quality="[^"]+"[^>]+href="([^"]+)"[^>]+class="quality-btn"[^>]*>([0-9]+p)',
  106. webpage):
  107. webpage = self._download_webpage(
  108. self._BASE_URL + href, video_id,
  109. 'Downloading %s version information' % resolution, fatal=False)
  110. if not webpage:
  111. # Manually report warning because empty page is returned when
  112. # invalid version is requested.
  113. self.report_warning('Unable to download %s version information' % resolution)
  114. continue
  115. extract_format(webpage, resolution)
  116. self._sort_formats(formats)
  117. return info_dict