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.

172 lines
6.5 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. # TODO: Current DASH formats are broken - $Time$ pattern in
  55. # <SegmentTemplate> not implemented yet
  56. # formats.extend(self._extract_mpd_formats(
  57. # video_url, video_id, mpd_id='dash', fatal=False))
  58. continue
  59. else:
  60. formats.append({
  61. 'url': video_url,
  62. 'format_id': format_id,
  63. 'height': int_or_none(f.get('vertical_resolution')),
  64. 'width': int_or_none(f.get('horizontal_resolution')),
  65. 'abr': float_or_none(f.get('audio_bitrate')),
  66. 'vbr': float_or_none(f.get('video_bitrate')),
  67. })
  68. self._sort_formats(formats)
  69. meta = video.get('meta', {})
  70. title = self._og_search_title(webpage, default=None) or meta['title']
  71. description = self._og_search_description(webpage, default=None) or meta.get('description')
  72. duration = meta.get('length') or meta.get('lenght')
  73. timestamp = parse_iso8601(meta.get('addDate'), ' ')
  74. return {
  75. 'id': video_id,
  76. 'title': title,
  77. 'description': description,
  78. 'duration': duration,
  79. 'timestamp': timestamp,
  80. 'formats': formats,
  81. }
  82. class OnetIE(OnetBaseIE):
  83. _VALID_URL = 'https?://(?:www\.)?onet\.tv/[a-z]/[a-z]+/(?P<display_id>[0-9a-z-]+)/(?P<id>[0-9a-z]+)'
  84. IE_NAME = 'onet.tv'
  85. _TEST = {
  86. 'url': 'http://onet.tv/k/openerfestival/open-er-festival-2016-najdziwniejsze-wymagania-gwiazd/qbpyqc',
  87. 'md5': 'e3ffbf47590032ac3f27249204173d50',
  88. 'info_dict': {
  89. 'id': 'qbpyqc',
  90. 'display_id': 'open-er-festival-2016-najdziwniejsze-wymagania-gwiazd',
  91. 'ext': 'mp4',
  92. 'title': 'Open\'er Festival 2016: najdziwniejsze wymagania gwiazd',
  93. '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...',
  94. 'upload_date': '20160705',
  95. 'timestamp': 1467721580,
  96. },
  97. }
  98. def _real_extract(self, url):
  99. mobj = re.match(self._VALID_URL, url)
  100. display_id, video_id = mobj.group('display_id', 'id')
  101. webpage = self._download_webpage(url, display_id)
  102. mvp_id = self._search_mvp_id(webpage)
  103. info_dict = self._extract_from_id(mvp_id, webpage)
  104. info_dict.update({
  105. 'id': video_id,
  106. 'display_id': display_id,
  107. })
  108. return info_dict
  109. class OnetChannelIE(OnetBaseIE):
  110. _VALID_URL = r'https?://(?:www\.)?onet\.tv/[a-z]/(?P<id>[a-z]+)(?:[?#]|$)'
  111. IE_NAME = 'onet.tv:channel'
  112. _TEST = {
  113. 'url': 'http://onet.tv/k/openerfestival',
  114. 'info_dict': {
  115. 'id': 'openerfestival',
  116. 'title': 'Open\'er Festival Live',
  117. 'description': 'Dziękujemy, że oglądaliście transmisje. Zobaczcie nasze relacje i wywiady z artystami.',
  118. },
  119. 'playlist_mincount': 46,
  120. }
  121. def _real_extract(self, url):
  122. channel_id = self._match_id(url)
  123. webpage = self._download_webpage(url, channel_id)
  124. current_clip_info = self._parse_json(self._search_regex(
  125. r'var\s+currentClip\s*=\s*({[^}]+})', webpage, 'video info'), channel_id,
  126. transform_source=lambda s: js_to_json(re.sub(r'\'\s*\+\s*\'', '', s)))
  127. video_id = remove_start(current_clip_info['ckmId'], 'mvp:')
  128. video_name = url_basename(current_clip_info['url'])
  129. if self._downloader.params.get('noplaylist'):
  130. self.to_screen(
  131. 'Downloading just video %s because of --no-playlist' % video_name)
  132. return self._extract_from_id(video_id, webpage)
  133. self.to_screen(
  134. 'Downloading channel %s - add --no-playlist to just download video %s' % (
  135. channel_id, video_name))
  136. matches = re.findall(
  137. r'<a[^>]+href=[\'"](https?://(?:www\.)?onet\.tv/[a-z]/[a-z]+/[0-9a-z-]+/[0-9a-z]+)',
  138. webpage)
  139. entries = [
  140. self.url_result(video_link, OnetIE.ie_key())
  141. for video_link in matches]
  142. channel_title = strip_or_none(get_element_by_class('o_channelName', webpage))
  143. channel_description = strip_or_none(get_element_by_class('o_channelDesc', webpage))
  144. return self.playlist_result(entries, channel_id, channel_title, channel_description)