mailru.py 12 KB

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