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.

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