manyvids.py 5.8 KB

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