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.

210 lines
7.0 KiB

9 years ago
  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. )
  17. class OdnoklassnikiIE(InfoExtractor):
  18. _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
  19. _TESTS = [{
  20. # metadata in JSON
  21. 'url': 'http://ok.ru/video/20079905452',
  22. 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
  23. 'info_dict': {
  24. 'id': '20079905452',
  25. 'ext': 'mp4',
  26. 'title': 'Культура меняет нас (прекрасный ролик!))',
  27. 'duration': 100,
  28. 'upload_date': '20141207',
  29. 'uploader_id': '330537914540',
  30. 'uploader': 'Виталий Добровольский',
  31. 'like_count': int,
  32. 'age_limit': 0,
  33. },
  34. 'skip': 'Video has been blocked',
  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': '64211978996595-1',
  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. def _real_extract(self, url):
  100. start_time = int_or_none(compat_parse_qs(
  101. compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
  102. video_id = self._match_id(url)
  103. webpage = self._download_webpage(
  104. 'http://ok.ru/video/%s' % video_id, video_id)
  105. error = self._search_regex(
  106. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  107. webpage, 'error', default=None)
  108. if error:
  109. raise ExtractorError(error, expected=True)
  110. player = self._parse_json(
  111. unescapeHTML(self._search_regex(
  112. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  113. webpage, 'player', group='player')),
  114. video_id)
  115. flashvars = player['flashvars']
  116. metadata = flashvars.get('metadata')
  117. if metadata:
  118. metadata = self._parse_json(metadata, video_id)
  119. else:
  120. metadata = self._download_json(
  121. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  122. video_id, 'Downloading metadata JSON')
  123. movie = metadata['movie']
  124. # Some embedded videos may not contain title in movie dict (e.g.
  125. # http://ok.ru/video/62036049272859-0) thus we allow missing title
  126. # here and it's going to be extracted later by an extractor that
  127. # will process the actual embed.
  128. provider = metadata.get('provider')
  129. title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
  130. thumbnail = movie.get('poster')
  131. duration = int_or_none(movie.get('duration'))
  132. author = metadata.get('author', {})
  133. uploader_id = author.get('id')
  134. uploader = author.get('name')
  135. upload_date = unified_strdate(self._html_search_meta(
  136. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  137. age_limit = None
  138. adult = self._html_search_meta(
  139. 'ya:ovs:adult', webpage, 'age limit', default=None)
  140. if adult:
  141. age_limit = 18 if adult == 'true' else 0
  142. like_count = int_or_none(metadata.get('likeCount'))
  143. info = {
  144. 'id': video_id,
  145. 'title': title,
  146. 'thumbnail': thumbnail,
  147. 'duration': duration,
  148. 'upload_date': upload_date,
  149. 'uploader': uploader,
  150. 'uploader_id': uploader_id,
  151. 'like_count': like_count,
  152. 'age_limit': age_limit,
  153. 'start_time': start_time,
  154. }
  155. if provider == 'USER_YOUTUBE':
  156. info.update({
  157. '_type': 'url_transparent',
  158. 'url': movie['contentId'],
  159. })
  160. return info
  161. quality = qualities(('4', '0', '1', '2', '3', '5'))
  162. formats = [{
  163. 'url': f['url'],
  164. 'ext': 'mp4',
  165. 'format_id': f['name'],
  166. } for f in metadata['videos']]
  167. m3u8_url = metadata.get('hlsManifestUrl')
  168. if m3u8_url:
  169. formats.extend(self._extract_m3u8_formats(
  170. m3u8_url, video_id, 'mp4', 'm3u8_native',
  171. m3u8_id='hls', fatal=False))
  172. dash_manifest = metadata.get('metadataEmbedded')
  173. if dash_manifest:
  174. formats.extend(self._parse_mpd_formats(
  175. compat_etree_fromstring(dash_manifest), 'mpd'))
  176. for fmt in formats:
  177. fmt_type = self._search_regex(
  178. r'\btype[/=](\d)', fmt['url'],
  179. 'format type', default=None)
  180. if fmt_type:
  181. fmt['quality'] = quality(fmt_type)
  182. self._sort_formats(formats)
  183. info['formats'] = formats
  184. return info