logo

youtube-dl

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

manyvids.py (6058B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. determine_ext,
  8. extract_attributes,
  9. int_or_none,
  10. str_to_int,
  11. url_or_none,
  12. urlencode_postdata,
  13. )
  14. class ManyVidsIE(InfoExtractor):
  15. _VALID_URL = r'(?i)https?://(?:www\.)?manyvids\.com/video/(?P<id>\d+)'
  16. _TESTS = [{
  17. # preview video
  18. 'url': 'https://www.manyvids.com/Video/133957/everthing-about-me/',
  19. 'md5': '03f11bb21c52dd12a05be21a5c7dcc97',
  20. 'info_dict': {
  21. 'id': '133957',
  22. 'ext': 'mp4',
  23. 'title': 'everthing about me (Preview)',
  24. 'uploader': 'ellyxxix',
  25. 'view_count': int,
  26. 'like_count': int,
  27. },
  28. }, {
  29. # full video
  30. 'url': 'https://www.manyvids.com/Video/935718/MY-FACE-REVEAL/',
  31. 'md5': 'bb47bab0e0802c2a60c24ef079dfe60f',
  32. 'info_dict': {
  33. 'id': '935718',
  34. 'ext': 'mp4',
  35. 'title': 'MY FACE REVEAL',
  36. 'description': 'md5:ec5901d41808b3746fed90face161612',
  37. 'uploader': 'Sarah Calanthe',
  38. 'view_count': int,
  39. 'like_count': int,
  40. },
  41. }]
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. real_url = 'https://www.manyvids.com/video/%s/gtm.js' % (video_id, )
  45. try:
  46. webpage = self._download_webpage(real_url, video_id)
  47. except Exception:
  48. # probably useless fallback
  49. webpage = self._download_webpage(url, video_id)
  50. info = self._search_regex(
  51. r'''(<div\b[^>]*\bid\s*=\s*(['"])pageMetaDetails\2[^>]*>)''',
  52. webpage, 'meta details', default='')
  53. info = extract_attributes(info)
  54. player = self._search_regex(
  55. r'''(<div\b[^>]*\bid\s*=\s*(['"])rmpPlayerStream\2[^>]*>)''',
  56. webpage, 'player details', default='')
  57. player = extract_attributes(player)
  58. video_urls_and_ids = (
  59. (info.get('data-meta-video'), 'video'),
  60. (player.get('data-video-transcoded'), 'transcoded'),
  61. (player.get('data-video-filepath'), 'filepath'),
  62. (self._og_search_video_url(webpage, secure=False, default=None), 'og_video'),
  63. )
  64. def txt_or_none(s, default=None):
  65. return (s.strip() or default) if isinstance(s, compat_str) else default
  66. uploader = txt_or_none(info.get('data-meta-author'))
  67. def mung_title(s):
  68. if uploader:
  69. s = re.sub(r'^\s*%s\s+[|-]' % (re.escape(uploader), ), '', s)
  70. return txt_or_none(s)
  71. title = (
  72. mung_title(info.get('data-meta-title'))
  73. or self._html_search_regex(
  74. (r'<span[^>]+class=["\']item-title[^>]+>([^<]+)',
  75. r'<h2[^>]+class=["\']h2 m-0["\'][^>]*>([^<]+)'),
  76. webpage, 'title', default=None)
  77. or self._html_search_meta(
  78. 'twitter:title', webpage, 'title', fatal=True))
  79. title = re.sub(r'\s*[|-]\s+ManyVids\s*$', '', title) or title
  80. if any(p in webpage for p in ('preview_videos', '_preview.mp4')):
  81. title += ' (Preview)'
  82. mv_token = self._search_regex(
  83. r'data-mvtoken=(["\'])(?P<value>(?:(?!\1).)+)\1', webpage,
  84. 'mv token', default=None, group='value')
  85. if mv_token:
  86. # Sets some cookies
  87. self._download_webpage(
  88. 'https://www.manyvids.com/includes/ajax_repository/you_had_me_at_hello.php',
  89. video_id, note='Setting format cookies', fatal=False,
  90. data=urlencode_postdata({
  91. 'mvtoken': mv_token,
  92. 'vid': video_id,
  93. }), headers={
  94. 'Referer': url,
  95. 'X-Requested-With': 'XMLHttpRequest'
  96. })
  97. formats = []
  98. for v_url, fmt in video_urls_and_ids:
  99. v_url = url_or_none(v_url)
  100. if not v_url:
  101. continue
  102. if determine_ext(v_url) == 'm3u8':
  103. formats.extend(self._extract_m3u8_formats(
  104. v_url, video_id, 'mp4', entry_protocol='m3u8_native',
  105. m3u8_id='hls'))
  106. else:
  107. formats.append({
  108. 'url': v_url,
  109. 'format_id': fmt,
  110. })
  111. self._remove_duplicate_formats(formats)
  112. for f in formats:
  113. if f.get('height') is None:
  114. f['height'] = int_or_none(
  115. self._search_regex(r'_(\d{2,3}[02468])_', f['url'], 'video height', default=None))
  116. if '/preview/' in f['url']:
  117. f['format_id'] = '_'.join(filter(None, (f.get('format_id'), 'preview')))
  118. f['preference'] = -10
  119. if 'transcoded' in f['format_id']:
  120. f['preference'] = f.get('preference', -1) - 1
  121. self._sort_formats(formats)
  122. def get_likes():
  123. likes = self._search_regex(
  124. r'''(<a\b[^>]*\bdata-id\s*=\s*(['"])%s\2[^>]*>)''' % (video_id, ),
  125. webpage, 'likes', default='')
  126. likes = extract_attributes(likes)
  127. return int_or_none(likes.get('data-likes'))
  128. def get_views():
  129. return str_to_int(self._html_search_regex(
  130. r'''(?s)<span\b[^>]*\bclass\s*=["']views-wrapper\b[^>]+>.+?<span\b[^>]+>\s*(\d[\d,.]*)\s*</span>''',
  131. webpage, 'view count', default=None))
  132. return {
  133. 'id': video_id,
  134. 'title': title,
  135. 'formats': formats,
  136. 'description': txt_or_none(info.get('data-meta-description')),
  137. 'uploader': txt_or_none(info.get('data-meta-author')),
  138. 'thumbnail': (
  139. url_or_none(info.get('data-meta-image'))
  140. or url_or_none(player.get('data-video-screenshot'))),
  141. 'view_count': get_views(),
  142. 'like_count': get_likes(),
  143. }