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.

182 lines
6.9 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. multipart_encode,
  11. parse_duration,
  12. random_birthday,
  13. urljoin,
  14. )
  15. class CDAIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:(?:www\.)?cda\.pl/video|ebd\.cda\.pl/[0-9]+x[0-9]+)/(?P<id>[0-9a-z]+)'
  17. _BASE_URL = 'http://www.cda.pl/'
  18. _TESTS = [{
  19. 'url': 'http://www.cda.pl/video/5749950c',
  20. 'md5': '6f844bf51b15f31fae165365707ae970',
  21. 'info_dict': {
  22. 'id': '5749950c',
  23. 'ext': 'mp4',
  24. 'height': 720,
  25. 'title': 'Oto dlaczego przed zakrętem należy zwolnić.',
  26. 'description': 'md5:269ccd135d550da90d1662651fcb9772',
  27. 'thumbnail': r're:^https?://.*\.jpg$',
  28. 'average_rating': float,
  29. 'duration': 39,
  30. 'age_limit': 0,
  31. }
  32. }, {
  33. 'url': 'http://www.cda.pl/video/57413289',
  34. 'md5': 'a88828770a8310fc00be6c95faf7f4d5',
  35. 'info_dict': {
  36. 'id': '57413289',
  37. 'ext': 'mp4',
  38. 'title': 'Lądowanie na lotnisku na Maderze',
  39. 'description': 'md5:60d76b71186dcce4e0ba6d4bbdb13e1a',
  40. 'thumbnail': r're:^https?://.*\.jpg$',
  41. 'uploader': 'crash404',
  42. 'view_count': int,
  43. 'average_rating': float,
  44. 'duration': 137,
  45. 'age_limit': 0,
  46. }
  47. }, {
  48. # Age-restricted
  49. 'url': 'http://www.cda.pl/video/1273454c4',
  50. 'info_dict': {
  51. 'id': '1273454c4',
  52. 'ext': 'mp4',
  53. 'title': 'Bronson (2008) napisy HD 1080p',
  54. 'description': 'md5:1b6cb18508daf2dc4e0fa4db77fec24c',
  55. 'height': 1080,
  56. 'uploader': 'boniek61',
  57. 'thumbnail': r're:^https?://.*\.jpg$',
  58. 'duration': 5554,
  59. 'age_limit': 18,
  60. 'view_count': int,
  61. 'average_rating': float,
  62. },
  63. }, {
  64. 'url': 'http://ebd.cda.pl/0x0/5749950c',
  65. 'only_matching': True,
  66. }]
  67. def _download_age_confirm_page(self, url, video_id, *args, **kwargs):
  68. form_data = random_birthday('rok', 'miesiac', 'dzien')
  69. form_data.update({'return': url, 'module': 'video', 'module_id': video_id})
  70. data, content_type = multipart_encode(form_data)
  71. return self._download_webpage(
  72. urljoin(url, '/a/validatebirth'), video_id, *args,
  73. data=data, headers={
  74. 'Referer': url,
  75. 'Content-Type': content_type,
  76. }, **kwargs)
  77. def _real_extract(self, url):
  78. video_id = self._match_id(url)
  79. self._set_cookie('cda.pl', 'cda.player', 'html5')
  80. webpage = self._download_webpage(
  81. self._BASE_URL + '/video/' + video_id, video_id)
  82. if 'Ten film jest dostępny dla użytkowników premium' in webpage:
  83. raise ExtractorError('This video is only available for premium users.', expected=True)
  84. need_confirm_age = False
  85. if self._html_search_regex(r'(<form[^>]+action="/a/validatebirth")',
  86. webpage, 'birthday validate form', default=None):
  87. webpage = self._download_age_confirm_page(
  88. url, video_id, note='Confirming age')
  89. need_confirm_age = True
  90. formats = []
  91. uploader = self._search_regex(r'''(?x)
  92. <(span|meta)[^>]+itemprop=(["\'])author\2[^>]*>
  93. (?:<\1[^>]*>[^<]*</\1>|(?!</\1>)(?:.|\n))*?
  94. <(span|meta)[^>]+itemprop=(["\'])name\4[^>]*>(?P<uploader>[^<]+)</\3>
  95. ''', webpage, 'uploader', default=None, group='uploader')
  96. view_count = self._search_regex(
  97. r'Odsłony:(?:\s|&nbsp;)*([0-9]+)', webpage,
  98. 'view_count', default=None)
  99. average_rating = self._search_regex(
  100. r'<(?:span|meta)[^>]+itemprop=(["\'])ratingValue\1[^>]*>(?P<rating_value>[0-9.]+)',
  101. webpage, 'rating', fatal=False, group='rating_value')
  102. info_dict = {
  103. 'id': video_id,
  104. 'title': self._og_search_title(webpage),
  105. 'description': self._og_search_description(webpage),
  106. 'uploader': uploader,
  107. 'view_count': int_or_none(view_count),
  108. 'average_rating': float_or_none(average_rating),
  109. 'thumbnail': self._og_search_thumbnail(webpage),
  110. 'formats': formats,
  111. 'duration': None,
  112. 'age_limit': 18 if need_confirm_age else 0,
  113. }
  114. def extract_format(page, version):
  115. json_str = self._html_search_regex(
  116. r'player_data=(\\?["\'])(?P<player_data>.+?)\1', page,
  117. '%s player_json' % version, fatal=False, group='player_data')
  118. if not json_str:
  119. return
  120. player_data = self._parse_json(
  121. json_str, '%s player_data' % version, fatal=False)
  122. if not player_data:
  123. return
  124. video = player_data.get('video')
  125. if not video or 'file' not in video:
  126. self.report_warning('Unable to extract %s version information' % version)
  127. return
  128. if video['file'].startswith('uggc'):
  129. video['file'] = codecs.decode(video['file'], 'rot_13')
  130. if video['file'].endswith('adc.mp4'):
  131. video['file'] = video['file'].replace('adc.mp4', '.mp4')
  132. f = {
  133. 'url': video['file'],
  134. }
  135. m = re.search(
  136. r'<a[^>]+data-quality="(?P<format_id>[^"]+)"[^>]+href="[^"]+"[^>]+class="[^"]*quality-btn-active[^"]*">(?P<height>[0-9]+)p',
  137. page)
  138. if m:
  139. f.update({
  140. 'format_id': m.group('format_id'),
  141. 'height': int(m.group('height')),
  142. })
  143. info_dict['formats'].append(f)
  144. if not info_dict['duration']:
  145. info_dict['duration'] = parse_duration(video.get('duration'))
  146. extract_format(webpage, 'default')
  147. for href, resolution in re.findall(
  148. r'<a[^>]+data-quality="[^"]+"[^>]+href="([^"]+)"[^>]+class="quality-btn"[^>]*>([0-9]+p)',
  149. webpage):
  150. if need_confirm_age:
  151. handler = self._download_age_confirm_page
  152. else:
  153. handler = self._download_webpage
  154. webpage = handler(
  155. self._BASE_URL + href, video_id,
  156. 'Downloading %s version information' % resolution, fatal=False)
  157. if not webpage:
  158. # Manually report warning because empty page is returned when
  159. # invalid version is requested.
  160. self.report_warning('Unable to download %s version information' % resolution)
  161. continue
  162. extract_format(webpage, resolution)
  163. self._sort_formats(formats)
  164. return info_dict