mailru.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. class MailRuIE(InfoExtractor):
  6. IE_NAME = 'mailru'
  7. IE_DESC = 'Видео@Mail.Ru'
  8. _VALID_URL = r'http://(?:www\.)?my\.mail\.ru/(?:video/.*#video=/?(?P<idv1>(?:[^/]+/){3}\d+)|(?:(?P<idv2prefix>(?:[^/]+/){2})video/(?P<idv2suffix>[^/]+/\d+))\.html)'
  9. _TESTS = [
  10. {
  11. 'url': 'http://my.mail.ru/video/top#video=/mail/sonypicturesrus/75/76',
  12. 'md5': 'dea205f03120046894db4ebb6159879a',
  13. 'info_dict': {
  14. 'id': '46301138_76',
  15. 'ext': 'mp4',
  16. 'title': 'Новый Человек-Паук. Высокое напряжение. Восстание Электро',
  17. 'timestamp': 1393232740,
  18. 'upload_date': '20140224',
  19. 'uploader': 'sonypicturesrus',
  20. 'uploader_id': 'sonypicturesrus@mail.ru',
  21. 'duration': 184,
  22. },
  23. 'skip': 'Not accessible from Travis CI server',
  24. },
  25. {
  26. 'url': 'http://my.mail.ru/corp/hitech/video/news_hi-tech_mail_ru/1263.html',
  27. 'md5': '00a91a58c3402204dcced523777b475f',
  28. 'info_dict': {
  29. 'id': '46843144_1263',
  30. 'ext': 'mp4',
  31. 'title': 'Samsung Galaxy S5 Hammer Smash Fail Battery Explosion',
  32. 'timestamp': 1397217632,
  33. 'upload_date': '20140411',
  34. 'uploader': 'hitech',
  35. 'uploader_id': 'hitech@corp.mail.ru',
  36. 'duration': 245,
  37. },
  38. 'skip': 'Not accessible from Travis CI server',
  39. },
  40. {
  41. # only available via metaUrl API
  42. 'url': 'http://my.mail.ru/mail/720pizle/video/_myvideo/502.html',
  43. 'md5': '3b26d2491c6949d031a32b96bd97c096',
  44. 'info_dict': {
  45. 'id': '56664382_502',
  46. 'ext': 'mp4',
  47. 'title': ':8336',
  48. 'timestamp': 1449094163,
  49. 'upload_date': '20151202',
  50. 'uploader': '720pizle@mail.ru',
  51. 'uploader_id': '720pizle@mail.ru',
  52. 'duration': 6001,
  53. },
  54. 'skip': 'Not accessible from Travis CI server',
  55. }
  56. ]
  57. def _real_extract(self, url):
  58. mobj = re.match(self._VALID_URL, url)
  59. video_id = mobj.group('idv1')
  60. if not video_id:
  61. video_id = mobj.group('idv2prefix') + mobj.group('idv2suffix')
  62. webpage = self._download_webpage(url, video_id)
  63. video_data = None
  64. page_config = self._parse_json(self._search_regex(
  65. r'(?s)<script[^>]+class="sp-video__page-config"[^>]*>(.+?)</script>',
  66. webpage, 'page config', default='{}'), video_id, fatal=False)
  67. if page_config:
  68. meta_url = page_config.get('metaUrl') or page_config.get('video', {}).get('metaUrl')
  69. if meta_url:
  70. video_data = self._download_json(
  71. meta_url, video_id, 'Downloading video meta JSON', fatal=False)
  72. # Fallback old approach
  73. if not video_data:
  74. video_data = self._download_json(
  75. 'http://api.video.mail.ru/videos/%s.json?new=1' % video_id,
  76. video_id, 'Downloading video JSON')
  77. author = video_data['author']
  78. uploader = author['name']
  79. uploader_id = author.get('id') or author.get('email')
  80. view_count = video_data.get('views_count')
  81. meta_data = video_data['meta']
  82. content_id = '%s_%s' % (
  83. meta_data.get('accId', ''), meta_data['itemId'])
  84. title = meta_data['title']
  85. if title.endswith('.mp4'):
  86. title = title[:-4]
  87. thumbnail = meta_data['poster']
  88. duration = meta_data['duration']
  89. timestamp = meta_data['timestamp']
  90. formats = [
  91. {
  92. 'url': video['url'],
  93. 'format_id': video['key'],
  94. 'height': int(video['key'].rstrip('p'))
  95. } for video in video_data['videos']
  96. ]
  97. self._sort_formats(formats)
  98. return {
  99. 'id': content_id,
  100. 'title': title,
  101. 'thumbnail': thumbnail,
  102. 'timestamp': timestamp,
  103. 'uploader': uploader,
  104. 'uploader_id': uploader_id,
  105. 'duration': duration,
  106. 'view_count': view_count,
  107. 'formats': formats,
  108. }