odnoklassniki.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. # YouTube embed (metadata, provider == USER_YOUTUBE, no metadata.movie.title field)
  62. 'url': 'http://ok.ru/video/62036049272859-0',
  63. 'info_dict': {
  64. 'id': '62036049272859-0',
  65. 'ext': 'mp4',
  66. 'title': 'МУЗЫКА ДОЖДЯ .',
  67. 'description': 'md5:6f1867132bd96e33bf53eda1091e8ed0',
  68. 'upload_date': '20120106',
  69. 'uploader_id': '473534735899',
  70. 'uploader': 'МARINA D',
  71. 'age_limit': 0,
  72. },
  73. 'params': {
  74. 'skip_download': True,
  75. },
  76. }, {
  77. 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
  78. 'only_matching': True,
  79. }, {
  80. 'url': 'http://www.ok.ru/video/20648036891',
  81. 'only_matching': True,
  82. }, {
  83. 'url': 'http://www.ok.ru/videoembed/20648036891',
  84. 'only_matching': True,
  85. }, {
  86. 'url': 'http://m.ok.ru/video/20079905452',
  87. 'only_matching': True,
  88. }, {
  89. 'url': 'http://mobile.ok.ru/video/20079905452',
  90. 'only_matching': True,
  91. }]
  92. def _real_extract(self, url):
  93. video_id = self._match_id(url)
  94. webpage = self._download_webpage(
  95. 'http://ok.ru/video/%s' % video_id, video_id)
  96. error = self._search_regex(
  97. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  98. webpage, 'error', default=None)
  99. if error:
  100. raise ExtractorError(error, expected=True)
  101. player = self._parse_json(
  102. unescapeHTML(self._search_regex(
  103. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  104. webpage, 'player', group='player')),
  105. video_id)
  106. flashvars = player['flashvars']
  107. metadata = flashvars.get('metadata')
  108. if metadata:
  109. metadata = self._parse_json(metadata, video_id)
  110. else:
  111. metadata = self._download_json(
  112. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  113. video_id, 'Downloading metadata JSON')
  114. movie = metadata['movie']
  115. # Some embedded videos may not contain title in movie dict (e.g.
  116. # http://ok.ru/video/62036049272859-0) thus we allow missing title
  117. # here and it's going to be extracted later by an extractor that
  118. # will process the actual embed.
  119. provider = metadata.get('provider')
  120. title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
  121. thumbnail = movie.get('poster')
  122. duration = int_or_none(movie.get('duration'))
  123. author = metadata.get('author', {})
  124. uploader_id = author.get('id')
  125. uploader = author.get('name')
  126. upload_date = unified_strdate(self._html_search_meta(
  127. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  128. age_limit = None
  129. adult = self._html_search_meta(
  130. 'ya:ovs:adult', webpage, 'age limit', default=None)
  131. if adult:
  132. age_limit = 18 if adult == 'true' else 0
  133. like_count = int_or_none(metadata.get('likeCount'))
  134. info = {
  135. 'id': video_id,
  136. 'title': title,
  137. 'thumbnail': thumbnail,
  138. 'duration': duration,
  139. 'upload_date': upload_date,
  140. 'uploader': uploader,
  141. 'uploader_id': uploader_id,
  142. 'like_count': like_count,
  143. 'age_limit': age_limit,
  144. }
  145. if provider == 'USER_YOUTUBE':
  146. info.update({
  147. '_type': 'url_transparent',
  148. 'url': movie['contentId'],
  149. })
  150. return info
  151. quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
  152. formats = [{
  153. 'url': f['url'],
  154. 'ext': 'mp4',
  155. 'format_id': f['name'],
  156. 'quality': quality(f['name']),
  157. } for f in metadata['videos']]
  158. self._sort_formats(formats)
  159. info['formats'] = formats
  160. return info