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.

236 lines
7.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_etree_fromstring,
  6. compat_parse_qs,
  7. compat_urllib_parse_unquote,
  8. compat_urllib_parse_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. unified_strdate,
  13. int_or_none,
  14. qualities,
  15. unescapeHTML,
  16. urlencode_postdata,
  17. )
  18. class OdnoklassnikiIE(InfoExtractor):
  19. _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer|live)/(?P<id>[\d-]+)'
  20. _TESTS = [{
  21. # metadata in JSON
  22. 'url': 'http://ok.ru/video/20079905452',
  23. 'md5': '0b62089b479e06681abaaca9d204f152',
  24. 'info_dict': {
  25. 'id': '20079905452',
  26. 'ext': 'mp4',
  27. 'title': 'Культура меняет нас (прекрасный ролик!))',
  28. 'duration': 100,
  29. 'upload_date': '20141207',
  30. 'uploader_id': '330537914540',
  31. 'uploader': 'Виталий Добровольский',
  32. 'like_count': int,
  33. 'age_limit': 0,
  34. },
  35. }, {
  36. # metadataUrl
  37. 'url': 'http://ok.ru/video/63567059965189-0?fromTime=5',
  38. 'md5': '6ff470ea2dd51d5d18c295a355b0b6bc',
  39. 'info_dict': {
  40. 'id': '63567059965189-0',
  41. 'ext': 'mp4',
  42. 'title': 'Девушка без комплексов ...',
  43. 'duration': 191,
  44. 'upload_date': '20150518',
  45. 'uploader_id': '534380003155',
  46. 'uploader': '☭ Андрей Мещанинов ☭',
  47. 'like_count': int,
  48. 'age_limit': 0,
  49. 'start_time': 5,
  50. },
  51. }, {
  52. # YouTube embed (metadataUrl, provider == USER_YOUTUBE)
  53. 'url': 'http://ok.ru/video/64211978996595-1',
  54. 'md5': '2f206894ffb5dbfcce2c5a14b909eea5',
  55. 'info_dict': {
  56. 'id': 'V_VztHT5BzY',
  57. 'ext': 'mp4',
  58. 'title': 'Космическая среда от 26 августа 2015',
  59. 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0',
  60. 'duration': 440,
  61. 'upload_date': '20150826',
  62. 'uploader_id': 'tvroscosmos',
  63. 'uploader': 'Телестудия Роскосмоса',
  64. 'age_limit': 0,
  65. },
  66. }, {
  67. # YouTube embed (metadata, provider == USER_YOUTUBE, no metadata.movie.title field)
  68. 'url': 'http://ok.ru/video/62036049272859-0',
  69. 'info_dict': {
  70. 'id': '62036049272859-0',
  71. 'ext': 'mp4',
  72. 'title': 'МУЗЫКА ДОЖДЯ .',
  73. 'description': 'md5:6f1867132bd96e33bf53eda1091e8ed0',
  74. 'upload_date': '20120106',
  75. 'uploader_id': '473534735899',
  76. 'uploader': 'МARINA D',
  77. 'age_limit': 0,
  78. },
  79. 'params': {
  80. 'skip_download': True,
  81. },
  82. 'skip': 'Video has not been found',
  83. }, {
  84. 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
  85. 'only_matching': True,
  86. }, {
  87. 'url': 'http://www.ok.ru/video/20648036891',
  88. 'only_matching': True,
  89. }, {
  90. 'url': 'http://www.ok.ru/videoembed/20648036891',
  91. 'only_matching': True,
  92. }, {
  93. 'url': 'http://m.ok.ru/video/20079905452',
  94. 'only_matching': True,
  95. }, {
  96. 'url': 'http://mobile.ok.ru/video/20079905452',
  97. 'only_matching': True,
  98. }, {
  99. 'url': 'https://www.ok.ru/live/484531969818',
  100. 'only_matching': True,
  101. }]
  102. def _real_extract(self, url):
  103. start_time = int_or_none(compat_parse_qs(
  104. compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
  105. video_id = self._match_id(url)
  106. webpage = self._download_webpage(
  107. 'http://ok.ru/video/%s' % video_id, video_id)
  108. error = self._search_regex(
  109. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  110. webpage, 'error', default=None)
  111. if error:
  112. raise ExtractorError(error, expected=True)
  113. player = self._parse_json(
  114. unescapeHTML(self._search_regex(
  115. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  116. webpage, 'player', group='player')),
  117. video_id)
  118. flashvars = player['flashvars']
  119. metadata = flashvars.get('metadata')
  120. if metadata:
  121. metadata = self._parse_json(metadata, video_id)
  122. else:
  123. data = {}
  124. st_location = flashvars.get('location')
  125. if st_location:
  126. data['st.location'] = st_location
  127. metadata = self._download_json(
  128. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  129. video_id, 'Downloading metadata JSON',
  130. data=urlencode_postdata(data))
  131. movie = metadata['movie']
  132. # Some embedded videos may not contain title in movie dict (e.g.
  133. # http://ok.ru/video/62036049272859-0) thus we allow missing title
  134. # here and it's going to be extracted later by an extractor that
  135. # will process the actual embed.
  136. provider = metadata.get('provider')
  137. title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
  138. thumbnail = movie.get('poster')
  139. duration = int_or_none(movie.get('duration'))
  140. author = metadata.get('author', {})
  141. uploader_id = author.get('id')
  142. uploader = author.get('name')
  143. upload_date = unified_strdate(self._html_search_meta(
  144. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  145. age_limit = None
  146. adult = self._html_search_meta(
  147. 'ya:ovs:adult', webpage, 'age limit', default=None)
  148. if adult:
  149. age_limit = 18 if adult == 'true' else 0
  150. like_count = int_or_none(metadata.get('likeCount'))
  151. info = {
  152. 'id': video_id,
  153. 'title': title,
  154. 'thumbnail': thumbnail,
  155. 'duration': duration,
  156. 'upload_date': upload_date,
  157. 'uploader': uploader,
  158. 'uploader_id': uploader_id,
  159. 'like_count': like_count,
  160. 'age_limit': age_limit,
  161. 'start_time': start_time,
  162. }
  163. if provider == 'USER_YOUTUBE':
  164. info.update({
  165. '_type': 'url_transparent',
  166. 'url': movie['contentId'],
  167. })
  168. return info
  169. assert title
  170. if provider == 'LIVE_TV_APP':
  171. info['title'] = self._live_title(title)
  172. quality = qualities(('4', '0', '1', '2', '3', '5'))
  173. formats = [{
  174. 'url': f['url'],
  175. 'ext': 'mp4',
  176. 'format_id': f['name'],
  177. } for f in metadata['videos']]
  178. m3u8_url = metadata.get('hlsManifestUrl')
  179. if m3u8_url:
  180. formats.extend(self._extract_m3u8_formats(
  181. m3u8_url, video_id, 'mp4', 'm3u8_native',
  182. m3u8_id='hls', fatal=False))
  183. dash_manifest = metadata.get('metadataEmbedded')
  184. if dash_manifest:
  185. formats.extend(self._parse_mpd_formats(
  186. compat_etree_fromstring(dash_manifest), 'mpd'))
  187. for fmt in formats:
  188. fmt_type = self._search_regex(
  189. r'\btype[/=](\d)', fmt['url'],
  190. 'format type', default=None)
  191. if fmt_type:
  192. fmt['quality'] = quality(fmt_type)
  193. # Live formats
  194. m3u8_url = metadata.get('hlsMasterPlaylistUrl')
  195. if m3u8_url:
  196. formats.extend(self._extract_m3u8_formats(
  197. m3u8_url, video_id, 'mp4', entry_protocol='m3u8',
  198. m3u8_id='hls', fatal=False))
  199. rtmp_url = metadata.get('rtmpUrl')
  200. if rtmp_url:
  201. formats.append({
  202. 'url': rtmp_url,
  203. 'format_id': 'rtmp',
  204. 'ext': 'flv',
  205. })
  206. self._sort_formats(formats)
  207. info['formats'] = formats
  208. return info