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.

141 lines
4.6 KiB

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