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.

216 lines
7.2 KiB

9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_etree_fromstring,
  6. compat_parse_qs,
  7. compat_urllib_parse_unquote,
  8. compat_urllib_parse_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. unified_strdate,
  13. int_or_none,
  14. qualities,
  15. unescapeHTML,
  16. urlencode_postdata,
  17. )
  18. class OdnoklassnikiIE(InfoExtractor):
  19. _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
  20. _TESTS = [{
  21. # metadata in JSON
  22. 'url': 'http://ok.ru/video/20079905452',
  23. 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
  24. 'info_dict': {
  25. 'id': '20079905452',
  26. 'ext': 'mp4',
  27. 'title': 'Культура меняет нас (прекрасный ролик!))',
  28. 'duration': 100,
  29. 'upload_date': '20141207',
  30. 'uploader_id': '330537914540',
  31. 'uploader': 'Виталий Добровольский',
  32. 'like_count': int,
  33. 'age_limit': 0,
  34. },
  35. 'skip': 'Video has been blocked',
  36. }, {
  37. # metadataUrl
  38. 'url': 'http://ok.ru/video/63567059965189-0?fromTime=5',
  39. 'md5': '6ff470ea2dd51d5d18c295a355b0b6bc',
  40. 'info_dict': {
  41. 'id': '63567059965189-0',
  42. 'ext': 'mp4',
  43. 'title': 'Девушка без комплексов ...',
  44. 'duration': 191,
  45. 'upload_date': '20150518',
  46. 'uploader_id': '534380003155',
  47. 'uploader': '☭ Андрей Мещанинов ☭',
  48. 'like_count': int,
  49. 'age_limit': 0,
  50. 'start_time': 5,
  51. },
  52. }, {
  53. # YouTube embed (metadataUrl, provider == USER_YOUTUBE)
  54. 'url': 'http://ok.ru/video/64211978996595-1',
  55. 'md5': '2f206894ffb5dbfcce2c5a14b909eea5',
  56. 'info_dict': {
  57. 'id': 'V_VztHT5BzY',
  58. 'ext': 'mp4',
  59. 'title': 'Космическая среда от 26 августа 2015',
  60. 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0',
  61. 'duration': 440,
  62. 'upload_date': '20150826',
  63. 'uploader_id': 'tvroscosmos',
  64. 'uploader': 'Телестудия Роскосмоса',
  65. 'age_limit': 0,
  66. },
  67. }, {
  68. # YouTube embed (metadata, provider == USER_YOUTUBE, no metadata.movie.title field)
  69. 'url': 'http://ok.ru/video/62036049272859-0',
  70. 'info_dict': {
  71. 'id': '62036049272859-0',
  72. 'ext': 'mp4',
  73. 'title': 'МУЗЫКА ДОЖДЯ .',
  74. 'description': 'md5:6f1867132bd96e33bf53eda1091e8ed0',
  75. 'upload_date': '20120106',
  76. 'uploader_id': '473534735899',
  77. 'uploader': 'МARINA D',
  78. 'age_limit': 0,
  79. },
  80. 'params': {
  81. 'skip_download': True,
  82. },
  83. 'skip': 'Video has not been found',
  84. }, {
  85. 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
  86. 'only_matching': True,
  87. }, {
  88. 'url': 'http://www.ok.ru/video/20648036891',
  89. 'only_matching': True,
  90. }, {
  91. 'url': 'http://www.ok.ru/videoembed/20648036891',
  92. 'only_matching': True,
  93. }, {
  94. 'url': 'http://m.ok.ru/video/20079905452',
  95. 'only_matching': True,
  96. }, {
  97. 'url': 'http://mobile.ok.ru/video/20079905452',
  98. 'only_matching': True,
  99. }]
  100. def _real_extract(self, url):
  101. start_time = int_or_none(compat_parse_qs(
  102. compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
  103. video_id = self._match_id(url)
  104. webpage = self._download_webpage(
  105. 'http://ok.ru/video/%s' % video_id, video_id)
  106. error = self._search_regex(
  107. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  108. webpage, 'error', default=None)
  109. if error:
  110. raise ExtractorError(error, expected=True)
  111. player = self._parse_json(
  112. unescapeHTML(self._search_regex(
  113. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  114. webpage, 'player', group='player')),
  115. video_id)
  116. flashvars = player['flashvars']
  117. metadata = flashvars.get('metadata')
  118. if metadata:
  119. metadata = self._parse_json(metadata, video_id)
  120. else:
  121. data = {}
  122. st_location = flashvars.get('location')
  123. if st_location:
  124. data['st.location'] = st_location
  125. metadata = self._download_json(
  126. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  127. video_id, 'Downloading metadata JSON',
  128. data=urlencode_postdata(data))
  129. movie = metadata['movie']
  130. # Some embedded videos may not contain title in movie dict (e.g.
  131. # http://ok.ru/video/62036049272859-0) thus we allow missing title
  132. # here and it's going to be extracted later by an extractor that
  133. # will process the actual embed.
  134. provider = metadata.get('provider')
  135. title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
  136. thumbnail = movie.get('poster')
  137. duration = int_or_none(movie.get('duration'))
  138. author = metadata.get('author', {})
  139. uploader_id = author.get('id')
  140. uploader = author.get('name')
  141. upload_date = unified_strdate(self._html_search_meta(
  142. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  143. age_limit = None
  144. adult = self._html_search_meta(
  145. 'ya:ovs:adult', webpage, 'age limit', default=None)
  146. if adult:
  147. age_limit = 18 if adult == 'true' else 0
  148. like_count = int_or_none(metadata.get('likeCount'))
  149. info = {
  150. 'id': video_id,
  151. 'title': title,
  152. 'thumbnail': thumbnail,
  153. 'duration': duration,
  154. 'upload_date': upload_date,
  155. 'uploader': uploader,
  156. 'uploader_id': uploader_id,
  157. 'like_count': like_count,
  158. 'age_limit': age_limit,
  159. 'start_time': start_time,
  160. }
  161. if provider == 'USER_YOUTUBE':
  162. info.update({
  163. '_type': 'url_transparent',
  164. 'url': movie['contentId'],
  165. })
  166. return info
  167. quality = qualities(('4', '0', '1', '2', '3', '5'))
  168. formats = [{
  169. 'url': f['url'],
  170. 'ext': 'mp4',
  171. 'format_id': f['name'],
  172. } for f in metadata['videos']]
  173. m3u8_url = metadata.get('hlsManifestUrl')
  174. if m3u8_url:
  175. formats.extend(self._extract_m3u8_formats(
  176. m3u8_url, video_id, 'mp4', 'm3u8_native',
  177. m3u8_id='hls', fatal=False))
  178. dash_manifest = metadata.get('metadataEmbedded')
  179. if dash_manifest:
  180. formats.extend(self._parse_mpd_formats(
  181. compat_etree_fromstring(dash_manifest), 'mpd'))
  182. for fmt in formats:
  183. fmt_type = self._search_regex(
  184. r'\btype[/=](\d)', fmt['url'],
  185. 'format type', default=None)
  186. if fmt_type:
  187. fmt['quality'] = quality(fmt_type)
  188. self._sort_formats(formats)
  189. info['formats'] = formats
  190. return info