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.

250 lines
8.7 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. def _real_extract(self, url):
  117. start_time = int_or_none(compat_parse_qs(
  118. compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
  119. video_id = self._match_id(url)
  120. webpage = self._download_webpage(
  121. 'http://ok.ru/video/%s' % video_id, video_id)
  122. error = self._search_regex(
  123. r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
  124. webpage, 'error', default=None)
  125. if error:
  126. raise ExtractorError(error, expected=True)
  127. player = self._parse_json(
  128. unescapeHTML(self._search_regex(
  129. r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
  130. webpage, 'player', group='player')),
  131. video_id)
  132. flashvars = player['flashvars']
  133. metadata = flashvars.get('metadata')
  134. if metadata:
  135. metadata = self._parse_json(metadata, video_id)
  136. else:
  137. data = {}
  138. st_location = flashvars.get('location')
  139. if st_location:
  140. data['st.location'] = st_location
  141. metadata = self._download_json(
  142. compat_urllib_parse_unquote(flashvars['metadataUrl']),
  143. video_id, 'Downloading metadata JSON',
  144. data=urlencode_postdata(data))
  145. movie = metadata['movie']
  146. # Some embedded videos may not contain title in movie dict (e.g.
  147. # http://ok.ru/video/62036049272859-0) thus we allow missing title
  148. # here and it's going to be extracted later by an extractor that
  149. # will process the actual embed.
  150. provider = metadata.get('provider')
  151. title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
  152. thumbnail = movie.get('poster')
  153. duration = int_or_none(movie.get('duration'))
  154. author = metadata.get('author', {})
  155. uploader_id = author.get('id')
  156. uploader = author.get('name')
  157. upload_date = unified_strdate(self._html_search_meta(
  158. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  159. age_limit = None
  160. adult = self._html_search_meta(
  161. 'ya:ovs:adult', webpage, 'age limit', default=None)
  162. if adult:
  163. age_limit = 18 if adult == 'true' else 0
  164. like_count = int_or_none(metadata.get('likeCount'))
  165. info = {
  166. 'id': video_id,
  167. 'title': title,
  168. 'thumbnail': thumbnail,
  169. 'duration': duration,
  170. 'upload_date': upload_date,
  171. 'uploader': uploader,
  172. 'uploader_id': uploader_id,
  173. 'like_count': like_count,
  174. 'age_limit': age_limit,
  175. 'start_time': start_time,
  176. }
  177. if provider == 'USER_YOUTUBE':
  178. info.update({
  179. '_type': 'url_transparent',
  180. 'url': movie['contentId'],
  181. })
  182. return info
  183. assert title
  184. if provider == 'LIVE_TV_APP':
  185. info['title'] = self._live_title(title)
  186. quality = qualities(('4', '0', '1', '2', '3', '5'))
  187. formats = [{
  188. 'url': f['url'],
  189. 'ext': 'mp4',
  190. 'format_id': f['name'],
  191. } for f in metadata['videos']]
  192. m3u8_url = metadata.get('hlsManifestUrl')
  193. if m3u8_url:
  194. formats.extend(self._extract_m3u8_formats(
  195. m3u8_url, video_id, 'mp4', 'm3u8_native',
  196. m3u8_id='hls', fatal=False))
  197. dash_manifest = metadata.get('metadataEmbedded')
  198. if dash_manifest:
  199. formats.extend(self._parse_mpd_formats(
  200. compat_etree_fromstring(dash_manifest), 'mpd'))
  201. for fmt in formats:
  202. fmt_type = self._search_regex(
  203. r'\btype[/=](\d)', fmt['url'],
  204. 'format type', default=None)
  205. if fmt_type:
  206. fmt['quality'] = quality(fmt_type)
  207. # Live formats
  208. m3u8_url = metadata.get('hlsMasterPlaylistUrl')
  209. if m3u8_url:
  210. formats.extend(self._extract_m3u8_formats(
  211. m3u8_url, video_id, 'mp4', entry_protocol='m3u8',
  212. m3u8_id='hls', fatal=False))
  213. rtmp_url = metadata.get('rtmpUrl')
  214. if rtmp_url:
  215. formats.append({
  216. 'url': rtmp_url,
  217. 'format_id': 'rtmp',
  218. 'ext': 'flv',
  219. })
  220. self._sort_formats(formats)
  221. info['formats'] = formats
  222. return info