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.

108 lines
3.4 KiB

  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?://(?: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': '8e24ad2da6f387948e7a7d44eb8668fe',
  17. 'info_dict': {
  18. 'id': '20079905452',
  19. 'ext': 'mp4',
  20. 'title': 'Культура меняет нас (прекрасный ролик!))',
  21. 'duration': 100,
  22. 'uploader_id': '330537914540',
  23. 'uploader': 'Виталий Добровольский',
  24. 'like_count': int,
  25. },
  26. }, {
  27. # metadataUrl
  28. 'url': 'http://ok.ru/video/63567059965189-0',
  29. 'md5': '9676cf86eff5391d35dea675d224e131',
  30. 'info_dict': {
  31. 'id': '63567059965189-0',
  32. 'ext': 'mp4',
  33. 'title': 'Девушка без комплексов ...',
  34. 'duration': 191,
  35. 'uploader_id': '534380003155',
  36. 'uploader': 'Андрей Мещанинов',
  37. 'like_count': int,
  38. },
  39. }, {
  40. 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
  41. 'only_matching': True,
  42. }]
  43. def _real_extract(self, url):
  44. video_id = self._match_id(url)
  45. webpage = self._download_webpage(
  46. 'http://ok.ru/video/%s' % video_id, video_id)
  47. player = self._parse_json(
  48. unescapeHTML(self._search_regex(
  49. r'data-attributes="([^"]+)"', webpage, 'player')),
  50. video_id)
  51. flashvars = player['flashvars']
  52. metadata = flashvars.get('metadata')
  53. if metadata:
  54. metadata = self._parse_json(metadata, video_id)
  55. else:
  56. metadata = self._download_json(
  57. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  58. video_id, 'Downloading metadata JSON')
  59. movie = metadata['movie']
  60. title = movie['title']
  61. thumbnail = movie.get('poster')
  62. duration = int_or_none(movie.get('duration'))
  63. author = metadata.get('author', {})
  64. uploader_id = author.get('id')
  65. uploader = author.get('name')
  66. upload_date = unified_strdate(self._html_search_meta(
  67. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  68. age_limit = None
  69. adult = self._html_search_meta(
  70. 'ya:ovs:adult', webpage, 'age limit', default=None)
  71. if adult:
  72. age_limit = 18 if adult == 'true' else 0
  73. like_count = int_or_none(metadata.get('likeCount'))
  74. quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
  75. formats = [{
  76. 'url': f['url'],
  77. 'ext': 'mp4',
  78. 'format_id': f['name'],
  79. 'quality': quality(f['name']),
  80. } for f in metadata['videos']]
  81. return {
  82. 'id': video_id,
  83. 'title': title,
  84. 'thumbnail': thumbnail,
  85. 'duration': duration,
  86. 'upload_date': upload_date,
  87. 'uploader': uploader,
  88. 'uploader_id': uploader_id,
  89. 'like_count': like_count,
  90. 'age_limit': age_limit,
  91. 'formats': formats,
  92. }