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.

314 lines
11 KiB

  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. def _real_extract(self, url):
  87. mobj = re.match(self._VALID_URL, url)
  88. meta_id = mobj.group('metaid')
  89. video_id = None
  90. if meta_id:
  91. meta_url = 'https://my.mail.ru/+/video/meta/%s' % meta_id
  92. else:
  93. video_id = mobj.group('idv1')
  94. if not video_id:
  95. video_id = mobj.group('idv2prefix') + mobj.group('idv2suffix')
  96. webpage = self._download_webpage(url, video_id)
  97. page_config = self._parse_json(self._search_regex(
  98. r'(?s)<script[^>]+class="sp-video__page-config"[^>]*>(.+?)</script>',
  99. webpage, 'page config', default='{}'), video_id, fatal=False)
  100. if page_config:
  101. meta_url = page_config.get('metaUrl') or page_config.get('video', {}).get('metaUrl')
  102. else:
  103. meta_url = None
  104. video_data = None
  105. if meta_url:
  106. video_data = self._download_json(
  107. meta_url, video_id or meta_id, 'Downloading video meta JSON',
  108. fatal=not video_id)
  109. # Fallback old approach
  110. if not video_data:
  111. video_data = self._download_json(
  112. 'http://api.video.mail.ru/videos/%s.json?new=1' % video_id,
  113. video_id, 'Downloading video JSON')
  114. formats = []
  115. for f in video_data['videos']:
  116. video_url = f.get('url')
  117. if not video_url:
  118. continue
  119. format_id = f.get('key')
  120. height = int_or_none(self._search_regex(
  121. r'^(\d+)[pP]$', format_id, 'height', default=None)) if format_id else None
  122. formats.append({
  123. 'url': video_url,
  124. 'format_id': format_id,
  125. 'height': height,
  126. })
  127. self._sort_formats(formats)
  128. meta_data = video_data['meta']
  129. title = remove_end(meta_data['title'], '.mp4')
  130. author = video_data.get('author')
  131. uploader = author.get('name')
  132. uploader_id = author.get('id') or author.get('email')
  133. view_count = int_or_none(video_data.get('viewsCount') or video_data.get('views_count'))
  134. acc_id = meta_data.get('accId')
  135. item_id = meta_data.get('itemId')
  136. content_id = '%s_%s' % (acc_id, item_id) if acc_id and item_id else video_id
  137. thumbnail = meta_data.get('poster')
  138. duration = int_or_none(meta_data.get('duration'))
  139. timestamp = int_or_none(meta_data.get('timestamp'))
  140. return {
  141. 'id': content_id,
  142. 'title': title,
  143. 'thumbnail': thumbnail,
  144. 'timestamp': timestamp,
  145. 'uploader': uploader,
  146. 'uploader_id': uploader_id,
  147. 'duration': duration,
  148. 'view_count': view_count,
  149. 'formats': formats,
  150. }
  151. class MailRuMusicSearchBaseIE(InfoExtractor):
  152. def _search(self, query, url, audio_id, limit=100, offset=0):
  153. search = self._download_json(
  154. 'https://my.mail.ru/cgi-bin/my/ajax', audio_id,
  155. 'Downloading songs JSON page %d' % (offset // limit + 1),
  156. headers={
  157. 'Referer': url,
  158. 'X-Requested-With': 'XMLHttpRequest',
  159. }, query={
  160. 'xemail': '',
  161. 'ajax_call': '1',
  162. 'func_name': 'music.search',
  163. 'mna': '',
  164. 'mnb': '',
  165. 'arg_query': query,
  166. 'arg_extended': '1',
  167. 'arg_search_params': json.dumps({
  168. 'music': {
  169. 'limit': limit,
  170. 'offset': offset,
  171. },
  172. }),
  173. 'arg_limit': limit,
  174. 'arg_offset': offset,
  175. })
  176. return next(e for e in search if isinstance(e, dict))
  177. @staticmethod
  178. def _extract_track(t, fatal=True):
  179. audio_url = t['URL'] if fatal else t.get('URL')
  180. if not audio_url:
  181. return
  182. audio_id = t['File'] if fatal else t.get('File')
  183. if not audio_id:
  184. return
  185. thumbnail = t.get('AlbumCoverURL') or t.get('FiledAlbumCover')
  186. uploader = t.get('OwnerName') or t.get('OwnerName_Text_HTML')
  187. uploader_id = t.get('UploaderID')
  188. duration = int_or_none(t.get('DurationInSeconds')) or parse_duration(
  189. t.get('Duration') or t.get('DurationStr'))
  190. view_count = int_or_none(t.get('PlayCount') or t.get('PlayCount_hr'))
  191. track = t.get('Name') or t.get('Name_Text_HTML')
  192. artist = t.get('Author') or t.get('Author_Text_HTML')
  193. if track:
  194. title = '%s - %s' % (artist, track) if artist else track
  195. else:
  196. title = audio_id
  197. return {
  198. 'extractor_key': MailRuMusicIE.ie_key(),
  199. 'id': audio_id,
  200. 'title': title,
  201. 'thumbnail': thumbnail,
  202. 'uploader': uploader,
  203. 'uploader_id': uploader_id,
  204. 'duration': duration,
  205. 'view_count': view_count,
  206. 'vcodec': 'none',
  207. 'abr': int_or_none(t.get('BitRate')),
  208. 'track': track,
  209. 'artist': artist,
  210. 'album': t.get('Album'),
  211. 'url': audio_url,
  212. }
  213. class MailRuMusicIE(MailRuMusicSearchBaseIE):
  214. IE_NAME = 'mailru:music'
  215. IE_DESC = 'Музыка@Mail.Ru'
  216. _VALID_URL = r'https?://my\.mail\.ru/music/songs/[^/?#&]+-(?P<id>[\da-f]+)'
  217. _TESTS = [{
  218. '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',
  219. 'md5': '0f8c22ef8c5d665b13ac709e63025610',
  220. 'info_dict': {
  221. 'id': '4e31f7125d0dfaef505d947642366893',
  222. 'ext': 'mp3',
  223. 'title': 'L.A.H. (Luciferian Aesthetics of Herrschaft) single, 2017 - М8Л8ТХ',
  224. 'uploader': 'Игорь Мудрый',
  225. 'uploader_id': '1459196328',
  226. 'duration': 280,
  227. 'view_count': int,
  228. 'vcodec': 'none',
  229. 'abr': 320,
  230. 'track': 'L.A.H. (Luciferian Aesthetics of Herrschaft) single, 2017',
  231. 'artist': 'М8Л8ТХ',
  232. },
  233. }]
  234. def _real_extract(self, url):
  235. audio_id = self._match_id(url)
  236. webpage = self._download_webpage(url, audio_id)
  237. title = self._og_search_title(webpage)
  238. music_data = self._search(title, url, audio_id)['MusicData']
  239. t = next(t for t in music_data if t.get('File') == audio_id)
  240. info = self._extract_track(t)
  241. info['title'] = title
  242. return info
  243. class MailRuMusicSearchIE(MailRuMusicSearchBaseIE):
  244. IE_NAME = 'mailru:music:search'
  245. IE_DESC = 'Музыка@Mail.Ru'
  246. _VALID_URL = r'https?://my\.mail\.ru/music/search/(?P<id>[^/?#&]+)'
  247. _TESTS = [{
  248. 'url': 'https://my.mail.ru/music/search/black%20shadow',
  249. 'info_dict': {
  250. 'id': 'black shadow',
  251. },
  252. 'playlist_mincount': 532,
  253. }]
  254. def _real_extract(self, url):
  255. query = compat_urllib_parse_unquote(self._match_id(url))
  256. entries = []
  257. LIMIT = 100
  258. offset = 0
  259. for _ in itertools.count(1):
  260. search = self._search(query, url, query, LIMIT, offset)
  261. music_data = search.get('MusicData')
  262. if not music_data or not isinstance(music_data, list):
  263. break
  264. for t in music_data:
  265. track = self._extract_track(t, fatal=False)
  266. if track:
  267. entries.append(track)
  268. total = try_get(
  269. search, lambda x: x['Results']['music']['Total'], int)
  270. if total is not None:
  271. if offset > total:
  272. break
  273. offset += LIMIT
  274. return self.playlist_result(entries, query)