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.

259 lines
9.0 KiB

  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'''(?x)
  20. https?://
  21. (?:(?:www|m|mobile)\.)?
  22. (?:odnoklassniki|ok)\.ru/
  23. (?:
  24. video(?:embed)?/|
  25. web-api/video/moviePlayer/|
  26. live/|
  27. dk\?.*?st\.mvId=
  28. )
  29. (?P<id>[\d-]+)
  30. '''
  31. _TESTS = [{
  32. # metadata in JSON
  33. 'url': 'http://ok.ru/video/20079905452',
  34. 'md5': '0b62089b479e06681abaaca9d204f152',
  35. 'info_dict': {
  36. 'id': '20079905452',
  37. 'ext': 'mp4',
  38. 'title': 'Культура меняет нас (прекрасный ролик!))',
  39. 'duration': 100,
  40. 'upload_date': '20141207',
  41. 'uploader_id': '330537914540',
  42. 'uploader': 'Виталий Добровольский',
  43. 'like_count': int,
  44. 'age_limit': 0,
  45. },
  46. }, {
  47. # metadataUrl
  48. 'url': 'http://ok.ru/video/63567059965189-0?fromTime=5',
  49. 'md5': '6ff470ea2dd51d5d18c295a355b0b6bc',
  50. 'info_dict': {
  51. 'id': '63567059965189-0',
  52. 'ext': 'mp4',
  53. 'title': 'Девушка без комплексов ...',
  54. 'duration': 191,
  55. 'upload_date': '20150518',
  56. 'uploader_id': '534380003155',
  57. 'uploader': '☭ Андрей Мещанинов ☭',
  58. 'like_count': int,
  59. 'age_limit': 0,
  60. 'start_time': 5,
  61. },
  62. }, {
  63. # YouTube embed (metadataUrl, provider == USER_YOUTUBE)
  64. 'url': 'http://ok.ru/video/64211978996595-1',
  65. 'md5': '2f206894ffb5dbfcce2c5a14b909eea5',
  66. 'info_dict': {
  67. 'id': 'V_VztHT5BzY',
  68. 'ext': 'mp4',
  69. 'title': 'Космическая среда от 26 августа 2015',
  70. 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0',
  71. 'duration': 440,
  72. 'upload_date': '20150826',
  73. 'uploader_id': 'tvroscosmos',
  74. 'uploader': 'Телестудия Роскосмоса',
  75. 'age_limit': 0,
  76. },
  77. }, {
  78. # YouTube embed (metadata, provider == USER_YOUTUBE, no metadata.movie.title field)
  79. 'url': 'http://ok.ru/video/62036049272859-0',
  80. 'info_dict': {
  81. 'id': '62036049272859-0',
  82. 'ext': 'mp4',
  83. 'title': 'МУЗЫКА ДОЖДЯ .',
  84. 'description': 'md5:6f1867132bd96e33bf53eda1091e8ed0',
  85. 'upload_date': '20120106',
  86. 'uploader_id': '473534735899',
  87. 'uploader': 'МARINA D',
  88. 'age_limit': 0,
  89. },
  90. 'params': {
  91. 'skip_download': True,
  92. },
  93. 'skip': 'Video has not been found',
  94. }, {
  95. 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
  96. 'only_matching': True,
  97. }, {
  98. 'url': 'http://www.ok.ru/video/20648036891',
  99. 'only_matching': True,
  100. }, {
  101. 'url': 'http://www.ok.ru/videoembed/20648036891',
  102. 'only_matching': True,
  103. }, {
  104. 'url': 'http://m.ok.ru/video/20079905452',
  105. 'only_matching': True,
  106. }, {
  107. 'url': 'http://mobile.ok.ru/video/20079905452',
  108. 'only_matching': True,
  109. }, {
  110. 'url': 'https://www.ok.ru/live/484531969818',
  111. 'only_matching': True,
  112. }, {
  113. 'url': 'https://m.ok.ru/dk?st.cmd=movieLayer&st.discId=863789452017&st.retLoc=friend&st.rtu=%2Fdk%3Fst.cmd%3DfriendMovies%26st.mode%3Down%26st.mrkId%3D%257B%2522uploadedMovieMarker%2522%253A%257B%2522marker%2522%253A%25221519410114503%2522%252C%2522hasMore%2522%253Atrue%257D%252C%2522sharedMovieMarker%2522%253A%257B%2522marker%2522%253Anull%252C%2522hasMore%2522%253Afalse%257D%257D%26st.friendId%3D561722190321%26st.frwd%3Don%26_prevCmd%3DfriendMovies%26tkn%3D7257&st.discType=MOVIE&st.mvId=863789452017&_prevCmd=friendMovies&tkn=3648#lst#',
  114. 'only_matching': True,
  115. }, {
  116. # Paid video
  117. 'url': 'https://ok.ru/video/954886983203',
  118. 'only_matching': True,
  119. }]
  120. def _real_extract(self, url):
  121. start_time = int_or_none(compat_parse_qs(
  122. compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
  123. video_id = self._match_id(url)
  124. webpage = self._download_webpage(
  125. 'http://ok.ru/video/%s' % video_id, video_id)
  126. error = self._search_regex(
  127. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  128. webpage, 'error', default=None)
  129. if error:
  130. raise ExtractorError(error, expected=True)
  131. player = self._parse_json(
  132. unescapeHTML(self._search_regex(
  133. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  134. webpage, 'player', group='player')),
  135. video_id)
  136. flashvars = player['flashvars']
  137. metadata = flashvars.get('metadata')
  138. if metadata:
  139. metadata = self._parse_json(metadata, video_id)
  140. else:
  141. data = {}
  142. st_location = flashvars.get('location')
  143. if st_location:
  144. data['st.location'] = st_location
  145. metadata = self._download_json(
  146. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  147. video_id, 'Downloading metadata JSON',
  148. data=urlencode_postdata(data))
  149. movie = metadata['movie']
  150. # Some embedded videos may not contain title in movie dict (e.g.
  151. # http://ok.ru/video/62036049272859-0) thus we allow missing title
  152. # here and it's going to be extracted later by an extractor that
  153. # will process the actual embed.
  154. provider = metadata.get('provider')
  155. title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
  156. thumbnail = movie.get('poster')
  157. duration = int_or_none(movie.get('duration'))
  158. author = metadata.get('author', {})
  159. uploader_id = author.get('id')
  160. uploader = author.get('name')
  161. upload_date = unified_strdate(self._html_search_meta(
  162. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  163. age_limit = None
  164. adult = self._html_search_meta(
  165. 'ya:ovs:adult', webpage, 'age limit', default=None)
  166. if adult:
  167. age_limit = 18 if adult == 'true' else 0
  168. like_count = int_or_none(metadata.get('likeCount'))
  169. info = {
  170. 'id': video_id,
  171. 'title': title,
  172. 'thumbnail': thumbnail,
  173. 'duration': duration,
  174. 'upload_date': upload_date,
  175. 'uploader': uploader,
  176. 'uploader_id': uploader_id,
  177. 'like_count': like_count,
  178. 'age_limit': age_limit,
  179. 'start_time': start_time,
  180. }
  181. if provider == 'USER_YOUTUBE':
  182. info.update({
  183. '_type': 'url_transparent',
  184. 'url': movie['contentId'],
  185. })
  186. return info
  187. assert title
  188. if provider == 'LIVE_TV_APP':
  189. info['title'] = self._live_title(title)
  190. quality = qualities(('4', '0', '1', '2', '3', '5'))
  191. formats = [{
  192. 'url': f['url'],
  193. 'ext': 'mp4',
  194. 'format_id': f['name'],
  195. } for f in metadata['videos']]
  196. m3u8_url = metadata.get('hlsManifestUrl')
  197. if m3u8_url:
  198. formats.extend(self._extract_m3u8_formats(
  199. m3u8_url, video_id, 'mp4', 'm3u8_native',
  200. m3u8_id='hls', fatal=False))
  201. dash_manifest = metadata.get('metadataEmbedded')
  202. if dash_manifest:
  203. formats.extend(self._parse_mpd_formats(
  204. compat_etree_fromstring(dash_manifest), 'mpd'))
  205. for fmt in formats:
  206. fmt_type = self._search_regex(
  207. r'\btype[/=](\d)', fmt['url'],
  208. 'format type', default=None)
  209. if fmt_type:
  210. fmt['quality'] = quality(fmt_type)
  211. # Live formats
  212. m3u8_url = metadata.get('hlsMasterPlaylistUrl')
  213. if m3u8_url:
  214. formats.extend(self._extract_m3u8_formats(
  215. m3u8_url, video_id, 'mp4', entry_protocol='m3u8',
  216. m3u8_id='hls', fatal=False))
  217. rtmp_url = metadata.get('rtmpUrl')
  218. if rtmp_url:
  219. formats.append({
  220. 'url': rtmp_url,
  221. 'format_id': 'rtmp',
  222. 'ext': 'flv',
  223. })
  224. if not formats:
  225. payment_info = metadata.get('paymentInfo')
  226. if payment_info:
  227. raise ExtractorError('This video is paid, subscribe to download it', expected=True)
  228. self._sort_formats(formats)
  229. info['formats'] = formats
  230. return info