logo

youtube-dl

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

mailru.py (11526B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import json
  5. import re
  6. from .common import InfoExtractor
  7. from ..compat import compat_urllib_parse_unquote
  8. from ..utils import (
  9. int_or_none,
  10. parse_duration,
  11. remove_end,
  12. try_get,
  13. )
  14. class MailRuIE(InfoExtractor):
  15. IE_NAME = 'mailru'
  16. IE_DESC = 'Видео@Mail.Ru'
  17. _VALID_URL = r'''(?x)
  18. https?://
  19. (?:(?:www|m)\.)?my\.mail\.ru/+
  20. (?:
  21. video/.*\#video=/?(?P<idv1>(?:[^/]+/){3}\d+)|
  22. (?:(?P<idv2prefix>(?:[^/]+/+){2})video/(?P<idv2suffix>[^/]+/\d+))\.html|
  23. (?:video/embed|\+/video/meta)/(?P<metaid>\d+)
  24. )
  25. '''
  26. _TESTS = [
  27. {
  28. 'url': 'http://my.mail.ru/video/top#video=/mail/sonypicturesrus/75/76',
  29. 'md5': 'dea205f03120046894db4ebb6159879a',
  30. 'info_dict': {
  31. 'id': '46301138_76',
  32. 'ext': 'mp4',
  33. 'title': 'Новый Человек-Паук. Высокое напряжение. Восстание Электро',
  34. 'timestamp': 1393235077,
  35. 'upload_date': '20140224',
  36. 'uploader': 'sonypicturesrus',
  37. 'uploader_id': 'sonypicturesrus@mail.ru',
  38. 'duration': 184,
  39. },
  40. 'skip': 'Not accessible from Travis CI server',
  41. },
  42. {
  43. 'url': 'http://my.mail.ru/corp/hitech/video/news_hi-tech_mail_ru/1263.html',
  44. 'md5': '00a91a58c3402204dcced523777b475f',
  45. 'info_dict': {
  46. 'id': '46843144_1263',
  47. 'ext': 'mp4',
  48. 'title': 'Samsung Galaxy S5 Hammer Smash Fail Battery Explosion',
  49. 'timestamp': 1397039888,
  50. 'upload_date': '20140409',
  51. 'uploader': 'hitech',
  52. 'uploader_id': 'hitech@corp.mail.ru',
  53. 'duration': 245,
  54. },
  55. 'skip': 'Not accessible from Travis CI server',
  56. },
  57. {
  58. # only available via metaUrl API
  59. 'url': 'http://my.mail.ru/mail/720pizle/video/_myvideo/502.html',
  60. 'md5': '3b26d2491c6949d031a32b96bd97c096',
  61. 'info_dict': {
  62. 'id': '56664382_502',
  63. 'ext': 'mp4',
  64. 'title': ':8336',
  65. 'timestamp': 1449094163,
  66. 'upload_date': '20151202',
  67. 'uploader': '720pizle@mail.ru',
  68. 'uploader_id': '720pizle@mail.ru',
  69. 'duration': 6001,
  70. },
  71. 'skip': 'Not accessible from Travis CI server',
  72. },
  73. {
  74. 'url': 'http://m.my.mail.ru/mail/3sktvtr/video/_myvideo/138.html',
  75. 'only_matching': True,
  76. },
  77. {
  78. 'url': 'https://my.mail.ru/video/embed/7949340477499637815',
  79. 'only_matching': True,
  80. },
  81. {
  82. 'url': 'http://my.mail.ru/+/video/meta/7949340477499637815',
  83. 'only_matching': True,
  84. },
  85. {
  86. 'url': 'https://my.mail.ru//list/sinyutin10/video/_myvideo/4.html',
  87. 'only_matching': True,
  88. },
  89. {
  90. 'url': 'https://my.mail.ru//list//sinyutin10/video/_myvideo/4.html',
  91. 'only_matching': True,
  92. }
  93. ]
  94. def _real_extract(self, url):
  95. mobj = re.match(self._VALID_URL, url)
  96. meta_id = mobj.group('metaid')
  97. video_id = None
  98. if meta_id:
  99. meta_url = 'https://my.mail.ru/+/video/meta/%s' % meta_id
  100. else:
  101. video_id = mobj.group('idv1')
  102. if not video_id:
  103. video_id = mobj.group('idv2prefix') + mobj.group('idv2suffix')
  104. webpage = self._download_webpage(url, video_id)
  105. page_config = self._parse_json(self._search_regex(
  106. r'(?s)<script[^>]+class="sp-video__page-config"[^>]*>(.+?)</script>',
  107. webpage, 'page config', default='{}'), video_id, fatal=False)
  108. if page_config:
  109. meta_url = page_config.get('metaUrl') or page_config.get('video', {}).get('metaUrl')
  110. else:
  111. meta_url = None
  112. video_data = None
  113. if meta_url:
  114. video_data = self._download_json(
  115. meta_url, video_id or meta_id, 'Downloading video meta JSON',
  116. fatal=not video_id)
  117. # Fallback old approach
  118. if not video_data:
  119. video_data = self._download_json(
  120. 'http://api.video.mail.ru/videos/%s.json?new=1' % video_id,
  121. video_id, 'Downloading video JSON')
  122. headers = {}
  123. video_key = self._get_cookies('https://my.mail.ru').get('video_key')
  124. if video_key:
  125. headers['Cookie'] = 'video_key=%s' % video_key.value
  126. formats = []
  127. for f in video_data['videos']:
  128. video_url = f.get('url')
  129. if not video_url:
  130. continue
  131. format_id = f.get('key')
  132. height = int_or_none(self._search_regex(
  133. r'^(\d+)[pP]$', format_id, 'height', default=None)) if format_id else None
  134. formats.append({
  135. 'url': video_url,
  136. 'format_id': format_id,
  137. 'height': height,
  138. 'http_headers': headers,
  139. })
  140. self._sort_formats(formats)
  141. meta_data = video_data['meta']
  142. title = remove_end(meta_data['title'], '.mp4')
  143. author = video_data.get('author')
  144. uploader = author.get('name')
  145. uploader_id = author.get('id') or author.get('email')
  146. view_count = int_or_none(video_data.get('viewsCount') or video_data.get('views_count'))
  147. acc_id = meta_data.get('accId')
  148. item_id = meta_data.get('itemId')
  149. content_id = '%s_%s' % (acc_id, item_id) if acc_id and item_id else video_id
  150. thumbnail = meta_data.get('poster')
  151. duration = int_or_none(meta_data.get('duration'))
  152. timestamp = int_or_none(meta_data.get('timestamp'))
  153. return {
  154. 'id': content_id,
  155. 'title': title,
  156. 'thumbnail': thumbnail,
  157. 'timestamp': timestamp,
  158. 'uploader': uploader,
  159. 'uploader_id': uploader_id,
  160. 'duration': duration,
  161. 'view_count': view_count,
  162. 'formats': formats,
  163. }
  164. class MailRuMusicSearchBaseIE(InfoExtractor):
  165. def _search(self, query, url, audio_id, limit=100, offset=0):
  166. search = self._download_json(
  167. 'https://my.mail.ru/cgi-bin/my/ajax', audio_id,
  168. 'Downloading songs JSON page %d' % (offset // limit + 1),
  169. headers={
  170. 'Referer': url,
  171. 'X-Requested-With': 'XMLHttpRequest',
  172. }, query={
  173. 'xemail': '',
  174. 'ajax_call': '1',
  175. 'func_name': 'music.search',
  176. 'mna': '',
  177. 'mnb': '',
  178. 'arg_query': query,
  179. 'arg_extended': '1',
  180. 'arg_search_params': json.dumps({
  181. 'music': {
  182. 'limit': limit,
  183. 'offset': offset,
  184. },
  185. }),
  186. 'arg_limit': limit,
  187. 'arg_offset': offset,
  188. })
  189. return next(e for e in search if isinstance(e, dict))
  190. @staticmethod
  191. def _extract_track(t, fatal=True):
  192. audio_url = t['URL'] if fatal else t.get('URL')
  193. if not audio_url:
  194. return
  195. audio_id = t['File'] if fatal else t.get('File')
  196. if not audio_id:
  197. return
  198. thumbnail = t.get('AlbumCoverURL') or t.get('FiledAlbumCover')
  199. uploader = t.get('OwnerName') or t.get('OwnerName_Text_HTML')
  200. uploader_id = t.get('UploaderID')
  201. duration = int_or_none(t.get('DurationInSeconds')) or parse_duration(
  202. t.get('Duration') or t.get('DurationStr'))
  203. view_count = int_or_none(t.get('PlayCount') or t.get('PlayCount_hr'))
  204. track = t.get('Name') or t.get('Name_Text_HTML')
  205. artist = t.get('Author') or t.get('Author_Text_HTML')
  206. if track:
  207. title = '%s - %s' % (artist, track) if artist else track
  208. else:
  209. title = audio_id
  210. return {
  211. 'extractor_key': MailRuMusicIE.ie_key(),
  212. 'id': audio_id,
  213. 'title': title,
  214. 'thumbnail': thumbnail,
  215. 'uploader': uploader,
  216. 'uploader_id': uploader_id,
  217. 'duration': duration,
  218. 'view_count': view_count,
  219. 'vcodec': 'none',
  220. 'abr': int_or_none(t.get('BitRate')),
  221. 'track': track,
  222. 'artist': artist,
  223. 'album': t.get('Album'),
  224. 'url': audio_url,
  225. }
  226. class MailRuMusicIE(MailRuMusicSearchBaseIE):
  227. IE_NAME = 'mailru:music'
  228. IE_DESC = 'Музыка@Mail.Ru'
  229. _VALID_URL = r'https?://my\.mail\.ru/+music/+songs/+[^/?#&]+-(?P<id>[\da-f]+)'
  230. _TESTS = [{
  231. 'url': 'https://my.mail.ru/music/songs/%D0%BC8%D0%BB8%D1%82%D1%85-l-a-h-luciferian-aesthetics-of-herrschaft-single-2017-4e31f7125d0dfaef505d947642366893',
  232. 'md5': '0f8c22ef8c5d665b13ac709e63025610',
  233. 'info_dict': {
  234. 'id': '4e31f7125d0dfaef505d947642366893',
  235. 'ext': 'mp3',
  236. 'title': 'L.A.H. (Luciferian Aesthetics of Herrschaft) single, 2017 - М8Л8ТХ',
  237. 'uploader': 'Игорь Мудрый',
  238. 'uploader_id': '1459196328',
  239. 'duration': 280,
  240. 'view_count': int,
  241. 'vcodec': 'none',
  242. 'abr': 320,
  243. 'track': 'L.A.H. (Luciferian Aesthetics of Herrschaft) single, 2017',
  244. 'artist': 'М8Л8ТХ',
  245. },
  246. }]
  247. def _real_extract(self, url):
  248. audio_id = self._match_id(url)
  249. webpage = self._download_webpage(url, audio_id)
  250. title = self._og_search_title(webpage)
  251. music_data = self._search(title, url, audio_id)['MusicData']
  252. t = next(t for t in music_data if t.get('File') == audio_id)
  253. info = self._extract_track(t)
  254. info['title'] = title
  255. return info
  256. class MailRuMusicSearchIE(MailRuMusicSearchBaseIE):
  257. IE_NAME = 'mailru:music:search'
  258. IE_DESC = 'Музыка@Mail.Ru'
  259. _VALID_URL = r'https?://my\.mail\.ru/+music/+search/+(?P<id>[^/?#&]+)'
  260. _TESTS = [{
  261. 'url': 'https://my.mail.ru/music/search/black%20shadow',
  262. 'info_dict': {
  263. 'id': 'black shadow',
  264. },
  265. 'playlist_mincount': 532,
  266. }]
  267. def _real_extract(self, url):
  268. query = compat_urllib_parse_unquote(self._match_id(url))
  269. entries = []
  270. LIMIT = 100
  271. offset = 0
  272. for _ in itertools.count(1):
  273. search = self._search(query, url, query, LIMIT, offset)
  274. music_data = search.get('MusicData')
  275. if not music_data or not isinstance(music_data, list):
  276. break
  277. for t in music_data:
  278. track = self._extract_track(t, fatal=False)
  279. if track:
  280. entries.append(track)
  281. total = try_get(
  282. search, lambda x: x['Results']['music']['Total'], int)
  283. if total is not None:
  284. if offset > total:
  285. break
  286. offset += LIMIT
  287. return self.playlist_result(entries, query)