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

Generated by cgit