logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

odnoklassniki.py (9492B)


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