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.

190 lines
6.3 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_parse_qs,
  6. compat_urllib_parse_unquote,
  7. compat_urllib_parse_urlparse,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. unified_strdate,
  12. int_or_none,
  13. qualities,
  14. unescapeHTML,
  15. )
  16. class OdnoklassnikiIE(InfoExtractor):
  17. _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
  18. _TESTS = [{
  19. # metadata in JSON
  20. 'url': 'http://ok.ru/video/20079905452',
  21. 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
  22. 'info_dict': {
  23. 'id': '20079905452',
  24. 'ext': 'mp4',
  25. 'title': 'Культура меняет нас (прекрасный ролик!))',
  26. 'duration': 100,
  27. 'upload_date': '20141207',
  28. 'uploader_id': '330537914540',
  29. 'uploader': 'Виталий Добровольский',
  30. 'like_count': int,
  31. 'age_limit': 0,
  32. },
  33. 'skip': 'Video has been blocked',
  34. }, {
  35. # metadataUrl
  36. 'url': 'http://ok.ru/video/63567059965189-0?fromTime=5',
  37. 'md5': '9676cf86eff5391d35dea675d224e131',
  38. 'info_dict': {
  39. 'id': '63567059965189-0',
  40. 'ext': 'mp4',
  41. 'title': 'Девушка без комплексов ...',
  42. 'duration': 191,
  43. 'upload_date': '20150518',
  44. 'uploader_id': '534380003155',
  45. 'uploader': '☭ Андрей Мещанинов ☭',
  46. 'like_count': int,
  47. 'age_limit': 0,
  48. 'start_time': 5,
  49. },
  50. }, {
  51. # YouTube embed (metadataUrl, provider == USER_YOUTUBE)
  52. 'url': 'http://ok.ru/video/64211978996595-1',
  53. 'md5': '5d7475d428845cd2e13bae6f1a992278',
  54. 'info_dict': {
  55. 'id': '64211978996595-1',
  56. 'ext': 'mp4',
  57. 'title': 'Космическая среда от 26 августа 2015',
  58. 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0',
  59. 'duration': 440,
  60. 'upload_date': '20150826',
  61. 'uploader_id': '750099571',
  62. 'uploader': 'Алина П',
  63. 'age_limit': 0,
  64. },
  65. }, {
  66. # YouTube embed (metadata, provider == USER_YOUTUBE, no metadata.movie.title field)
  67. 'url': 'http://ok.ru/video/62036049272859-0',
  68. 'info_dict': {
  69. 'id': '62036049272859-0',
  70. 'ext': 'mp4',
  71. 'title': 'МУЗЫКА ДОЖДЯ .',
  72. 'description': 'md5:6f1867132bd96e33bf53eda1091e8ed0',
  73. 'upload_date': '20120106',
  74. 'uploader_id': '473534735899',
  75. 'uploader': 'МARINA D',
  76. 'age_limit': 0,
  77. },
  78. 'params': {
  79. 'skip_download': True,
  80. },
  81. }, {
  82. 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
  83. 'only_matching': True,
  84. }, {
  85. 'url': 'http://www.ok.ru/video/20648036891',
  86. 'only_matching': True,
  87. }, {
  88. 'url': 'http://www.ok.ru/videoembed/20648036891',
  89. 'only_matching': True,
  90. }, {
  91. 'url': 'http://m.ok.ru/video/20079905452',
  92. 'only_matching': True,
  93. }, {
  94. 'url': 'http://mobile.ok.ru/video/20079905452',
  95. 'only_matching': True,
  96. }]
  97. def _real_extract(self, url):
  98. start_time = int_or_none(compat_parse_qs(
  99. compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
  100. video_id = self._match_id(url)
  101. webpage = self._download_webpage(
  102. 'http://ok.ru/video/%s' % video_id, video_id)
  103. error = self._search_regex(
  104. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  105. webpage, 'error', default=None)
  106. if error:
  107. raise ExtractorError(error, expected=True)
  108. player = self._parse_json(
  109. unescapeHTML(self._search_regex(
  110. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  111. webpage, 'player', group='player')),
  112. video_id)
  113. flashvars = player['flashvars']
  114. metadata = flashvars.get('metadata')
  115. if metadata:
  116. metadata = self._parse_json(metadata, video_id)
  117. else:
  118. metadata = self._download_json(
  119. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  120. video_id, 'Downloading metadata JSON')
  121. movie = metadata['movie']
  122. # Some embedded videos may not contain title in movie dict (e.g.
  123. # http://ok.ru/video/62036049272859-0) thus we allow missing title
  124. # here and it's going to be extracted later by an extractor that
  125. # will process the actual embed.
  126. provider = metadata.get('provider')
  127. title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
  128. thumbnail = movie.get('poster')
  129. duration = int_or_none(movie.get('duration'))
  130. author = metadata.get('author', {})
  131. uploader_id = author.get('id')
  132. uploader = author.get('name')
  133. upload_date = unified_strdate(self._html_search_meta(
  134. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  135. age_limit = None
  136. adult = self._html_search_meta(
  137. 'ya:ovs:adult', webpage, 'age limit', default=None)
  138. if adult:
  139. age_limit = 18 if adult == 'true' else 0
  140. like_count = int_or_none(metadata.get('likeCount'))
  141. info = {
  142. 'id': video_id,
  143. 'title': title,
  144. 'thumbnail': thumbnail,
  145. 'duration': duration,
  146. 'upload_date': upload_date,
  147. 'uploader': uploader,
  148. 'uploader_id': uploader_id,
  149. 'like_count': like_count,
  150. 'age_limit': age_limit,
  151. 'start_time': start_time,
  152. }
  153. if provider == 'USER_YOUTUBE':
  154. info.update({
  155. '_type': 'url_transparent',
  156. 'url': movie['contentId'],
  157. })
  158. return info
  159. quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
  160. formats = [{
  161. 'url': f['url'],
  162. 'ext': 'mp4',
  163. 'format_id': f['name'],
  164. 'quality': quality(f['name']),
  165. } for f in metadata['videos']]
  166. self._sort_formats(formats)
  167. info['formats'] = formats
  168. return info