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.

158 lines
5.1 KiB

9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse_unquote
  5. from ..utils import (
  6. ExtractorError,
  7. unified_strdate,
  8. int_or_none,
  9. qualities,
  10. unescapeHTML,
  11. )
  12. class OdnoklassnikiIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
  14. _TESTS = [{
  15. # metadata in JSON
  16. 'url': 'http://ok.ru/video/20079905452',
  17. 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
  18. 'info_dict': {
  19. 'id': '20079905452',
  20. 'ext': 'mp4',
  21. 'title': 'Культура меняет нас (прекрасный ролик!))',
  22. 'duration': 100,
  23. 'upload_date': '20141207',
  24. 'uploader_id': '330537914540',
  25. 'uploader': 'Виталий Добровольский',
  26. 'like_count': int,
  27. 'age_limit': 0,
  28. },
  29. 'skip': 'Video has been blocked',
  30. }, {
  31. # metadataUrl
  32. 'url': 'http://ok.ru/video/63567059965189-0',
  33. 'md5': '9676cf86eff5391d35dea675d224e131',
  34. 'info_dict': {
  35. 'id': '63567059965189-0',
  36. 'ext': 'mp4',
  37. 'title': 'Девушка без комплексов ...',
  38. 'duration': 191,
  39. 'upload_date': '20150518',
  40. 'uploader_id': '534380003155',
  41. 'uploader': '☭ Андрей Мещанинов ☭',
  42. 'like_count': int,
  43. 'age_limit': 0,
  44. },
  45. }, {
  46. # YouTube embed (metadataUrl, provider == USER_YOUTUBE)
  47. 'url': 'http://ok.ru/video/64211978996595-1',
  48. 'md5': '5d7475d428845cd2e13bae6f1a992278',
  49. 'info_dict': {
  50. 'id': '64211978996595-1',
  51. 'ext': 'mp4',
  52. 'title': 'Космическая среда от 26 августа 2015',
  53. 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0',
  54. 'duration': 440,
  55. 'upload_date': '20150826',
  56. 'uploader_id': '750099571',
  57. 'uploader': 'Алина П',
  58. 'age_limit': 0,
  59. },
  60. }, {
  61. 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
  62. 'only_matching': True,
  63. }, {
  64. 'url': 'http://www.ok.ru/video/20648036891',
  65. 'only_matching': True,
  66. }, {
  67. 'url': 'http://www.ok.ru/videoembed/20648036891',
  68. 'only_matching': True,
  69. }, {
  70. 'url': 'http://m.ok.ru/video/20079905452',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'http://mobile.ok.ru/video/20079905452',
  74. 'only_matching': True,
  75. }]
  76. def _real_extract(self, url):
  77. video_id = self._match_id(url)
  78. webpage = self._download_webpage(
  79. 'http://ok.ru/video/%s' % video_id, video_id)
  80. error = self._search_regex(
  81. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  82. webpage, 'error', default=None)
  83. if error:
  84. raise ExtractorError(error, expected=True)
  85. player = self._parse_json(
  86. unescapeHTML(self._search_regex(
  87. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  88. webpage, 'player', group='player')),
  89. video_id)
  90. flashvars = player['flashvars']
  91. metadata = flashvars.get('metadata')
  92. if metadata:
  93. metadata = self._parse_json(metadata, video_id)
  94. else:
  95. metadata = self._download_json(
  96. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  97. video_id, 'Downloading metadata JSON')
  98. movie = metadata['movie']
  99. title = movie['title']
  100. thumbnail = movie.get('poster')
  101. duration = int_or_none(movie.get('duration'))
  102. author = metadata.get('author', {})
  103. uploader_id = author.get('id')
  104. uploader = author.get('name')
  105. upload_date = unified_strdate(self._html_search_meta(
  106. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  107. age_limit = None
  108. adult = self._html_search_meta(
  109. 'ya:ovs:adult', webpage, 'age limit', default=None)
  110. if adult:
  111. age_limit = 18 if adult == 'true' else 0
  112. like_count = int_or_none(metadata.get('likeCount'))
  113. info = {
  114. 'id': video_id,
  115. 'title': title,
  116. 'thumbnail': thumbnail,
  117. 'duration': duration,
  118. 'upload_date': upload_date,
  119. 'uploader': uploader,
  120. 'uploader_id': uploader_id,
  121. 'like_count': like_count,
  122. 'age_limit': age_limit,
  123. }
  124. if metadata.get('provider') == 'USER_YOUTUBE':
  125. info.update({
  126. '_type': 'url_transparent',
  127. 'url': movie['contentId'],
  128. })
  129. return info
  130. quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
  131. formats = [{
  132. 'url': f['url'],
  133. 'ext': 'mp4',
  134. 'format_id': f['name'],
  135. 'quality': quality(f['name']),
  136. } for f in metadata['videos']]
  137. self._sort_formats(formats)
  138. info['formats'] = formats
  139. return info