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.

152 lines
4.9 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\.)?(?: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. def _real_extract(self, url):
  71. video_id = self._match_id(url)
  72. webpage = self._download_webpage(
  73. 'http://ok.ru/video/%s' % video_id, video_id)
  74. error = self._search_regex(
  75. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  76. webpage, 'error', default=None)
  77. if error:
  78. raise ExtractorError(error, expected=True)
  79. player = self._parse_json(
  80. unescapeHTML(self._search_regex(
  81. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  82. webpage, 'player', group='player')),
  83. video_id)
  84. flashvars = player['flashvars']
  85. metadata = flashvars.get('metadata')
  86. if metadata:
  87. metadata = self._parse_json(metadata, video_id)
  88. else:
  89. metadata = self._download_json(
  90. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  91. video_id, 'Downloading metadata JSON')
  92. movie = metadata['movie']
  93. title = movie['title']
  94. thumbnail = movie.get('poster')
  95. duration = int_or_none(movie.get('duration'))
  96. author = metadata.get('author', {})
  97. uploader_id = author.get('id')
  98. uploader = author.get('name')
  99. upload_date = unified_strdate(self._html_search_meta(
  100. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  101. age_limit = None
  102. adult = self._html_search_meta(
  103. 'ya:ovs:adult', webpage, 'age limit', default=None)
  104. if adult:
  105. age_limit = 18 if adult == 'true' else 0
  106. like_count = int_or_none(metadata.get('likeCount'))
  107. info = {
  108. 'id': video_id,
  109. 'title': title,
  110. 'thumbnail': thumbnail,
  111. 'duration': duration,
  112. 'upload_date': upload_date,
  113. 'uploader': uploader,
  114. 'uploader_id': uploader_id,
  115. 'like_count': like_count,
  116. 'age_limit': age_limit,
  117. }
  118. if metadata.get('provider') == 'USER_YOUTUBE':
  119. info.update({
  120. '_type': 'url_transparent',
  121. 'url': movie['contentId'],
  122. })
  123. return info
  124. quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
  125. formats = [{
  126. 'url': f['url'],
  127. 'ext': 'mp4',
  128. 'format_id': f['name'],
  129. 'quality': quality(f['name']),
  130. } for f in metadata['videos']]
  131. self._sort_formats(formats)
  132. info['formats'] = formats
  133. return info