logo

youtube-dl

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

cda.py (8325B)


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