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.

169 lines
6.3 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. ExtractorError,
  8. float_or_none,
  9. get_element_by_class,
  10. int_or_none,
  11. js_to_json,
  12. parse_iso8601,
  13. remove_start,
  14. strip_or_none,
  15. url_basename,
  16. )
  17. class OnetBaseIE(InfoExtractor):
  18. def _search_mvp_id(self, webpage):
  19. return self._search_regex(
  20. r'id=(["\'])mvp:(?P<id>.+?)\1', webpage, 'mvp id', group='id')
  21. def _extract_from_id(self, video_id, webpage):
  22. response = self._download_json(
  23. 'http://qi.ckm.onetapi.pl/', video_id,
  24. query={
  25. 'body[id]': video_id,
  26. 'body[jsonrpc]': '2.0',
  27. 'body[method]': 'get_asset_detail',
  28. 'body[params][ID_Publikacji]': video_id,
  29. 'body[params][Service]': 'www.onet.pl',
  30. 'content-type': 'application/jsonp',
  31. 'x-onet-app': 'player.front.onetapi.pl',
  32. })
  33. error = response.get('error')
  34. if error:
  35. raise ExtractorError(
  36. '%s said: %s' % (self.IE_NAME, error['message']), expected=True)
  37. video = response['result'].get('0')
  38. formats = []
  39. for _, formats_dict in video['formats'].items():
  40. if not isinstance(formats_dict, dict):
  41. continue
  42. for format_id, format_list in formats_dict.items():
  43. if not isinstance(format_list, list):
  44. continue
  45. for f in format_list:
  46. video_url = f.get('url')
  47. if not video_url:
  48. continue
  49. ext = determine_ext(video_url)
  50. if format_id == 'ism':
  51. # TODO: Support Microsoft Smooth Streaming
  52. continue
  53. elif ext == 'mpd':
  54. formats.extend(self._extract_mpd_formats(
  55. video_url, video_id, mpd_id='dash', fatal=False))
  56. else:
  57. formats.append({
  58. 'url': video_url,
  59. 'format_id': format_id,
  60. 'height': int_or_none(f.get('vertical_resolution')),
  61. 'width': int_or_none(f.get('horizontal_resolution')),
  62. 'abr': float_or_none(f.get('audio_bitrate')),
  63. 'vbr': float_or_none(f.get('video_bitrate')),
  64. })
  65. self._sort_formats(formats)
  66. meta = video.get('meta', {})
  67. title = self._og_search_title(webpage, default=None) or meta['title']
  68. description = self._og_search_description(webpage, default=None) or meta.get('description')
  69. duration = meta.get('length') or meta.get('lenght')
  70. timestamp = parse_iso8601(meta.get('addDate'), ' ')
  71. return {
  72. 'id': video_id,
  73. 'title': title,
  74. 'description': description,
  75. 'duration': duration,
  76. 'timestamp': timestamp,
  77. 'formats': formats,
  78. }
  79. class OnetIE(OnetBaseIE):
  80. _VALID_URL = 'https?://(?:www\.)?onet\.tv/[a-z]/[a-z]+/(?P<display_id>[0-9a-z-]+)/(?P<id>[0-9a-z]+)'
  81. IE_NAME = 'onet.tv'
  82. _TEST = {
  83. 'url': 'http://onet.tv/k/openerfestival/open-er-festival-2016-najdziwniejsze-wymagania-gwiazd/qbpyqc',
  84. 'md5': 'e3ffbf47590032ac3f27249204173d50',
  85. 'info_dict': {
  86. 'id': 'qbpyqc',
  87. 'display_id': 'open-er-festival-2016-najdziwniejsze-wymagania-gwiazd',
  88. 'ext': 'mp4',
  89. 'title': 'Open\'er Festival 2016: najdziwniejsze wymagania gwiazd',
  90. 'description': 'Trzy samochody, których nigdy nie użyto, prywatne spa, hotel dekorowany czarnym suknem czy nielegalne używki. Organizatorzy koncertów i festiwali muszą stawać przed nie lada wyzwaniem zapraszając gwia...',
  91. 'upload_date': '20160705',
  92. 'timestamp': 1467721580,
  93. },
  94. }
  95. def _real_extract(self, url):
  96. mobj = re.match(self._VALID_URL, url)
  97. display_id, video_id = mobj.group('display_id', 'id')
  98. webpage = self._download_webpage(url, display_id)
  99. mvp_id = self._search_mvp_id(webpage)
  100. info_dict = self._extract_from_id(mvp_id, webpage)
  101. info_dict.update({
  102. 'id': video_id,
  103. 'display_id': display_id,
  104. })
  105. return info_dict
  106. class OnetChannelIE(OnetBaseIE):
  107. _VALID_URL = r'https?://(?:www\.)?onet\.tv/[a-z]/(?P<id>[a-z]+)(?:[?#]|$)'
  108. IE_NAME = 'onet.tv:channel'
  109. _TEST = {
  110. 'url': 'http://onet.tv/k/openerfestival',
  111. 'info_dict': {
  112. 'id': 'openerfestival',
  113. 'title': 'Open\'er Festival Live',
  114. 'description': 'Dziękujemy, że oglądaliście transmisje. Zobaczcie nasze relacje i wywiady z artystami.',
  115. },
  116. 'playlist_mincount': 46,
  117. }
  118. def _real_extract(self, url):
  119. channel_id = self._match_id(url)
  120. webpage = self._download_webpage(url, channel_id)
  121. current_clip_info = self._parse_json(self._search_regex(
  122. r'var\s+currentClip\s*=\s*({[^}]+})', webpage, 'video info'), channel_id,
  123. transform_source=lambda s: js_to_json(re.sub(r'\'\s*\+\s*\'', '', s)))
  124. video_id = remove_start(current_clip_info['ckmId'], 'mvp:')
  125. video_name = url_basename(current_clip_info['url'])
  126. if self._downloader.params.get('noplaylist'):
  127. self.to_screen(
  128. 'Downloading just video %s because of --no-playlist' % video_name)
  129. return self._extract_from_id(video_id, webpage)
  130. self.to_screen(
  131. 'Downloading channel %s - add --no-playlist to just download video %s' % (
  132. channel_id, video_name))
  133. matches = re.findall(
  134. r'<a[^>]+href=[\'"](https?://(?:www\.)?onet\.tv/[a-z]/[a-z]+/[0-9a-z-]+/[0-9a-z]+)',
  135. webpage)
  136. entries = [
  137. self.url_result(video_link, OnetIE.ie_key())
  138. for video_link in matches]
  139. channel_title = strip_or_none(get_element_by_class('o_channelName', webpage))
  140. channel_description = strip_or_none(get_element_by_class('o_channelDesc', webpage))
  141. return self.playlist_result(entries, channel_id, channel_title, channel_description)