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.

136 lines
5.2 KiB

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