[cammodels] Add another error pattern
[youtube-dl] / youtube_dl / extractor / cammodels.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     ExtractorError,
8     int_or_none,
9 )
10
11
12 class CamModelsIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?cammodels\.com/cam/(?P<id>[^/?#&]+)'
14     _TESTS = [{
15         'url': 'https://www.cammodels.com/cam/AutumnKnight/',
16         'only_matching': True,
17     }]
18
19     def _real_extract(self, url):
20         user_id = self._match_id(url)
21
22         webpage = self._download_webpage(url, user_id)
23
24         manifest_root = self._html_search_regex(
25             r'manifestUrlRoot=([^&\']+)', webpage, 'manifest', default=None)
26
27         if not manifest_root:
28             ERRORS = (
29                 ("I'm offline, but let's stay connected", 'This user is currently offline'),
30                 ('in a private show', 'This user is in a private show'),
31                 ('is currently performing LIVE', 'This model is currently performing live'),
32             )
33             for pattern, message in ERRORS:
34                 if pattern in webpage:
35                     error = message
36                     expected = True
37                     break
38             else:
39                 error = 'Unable to find manifest URL root'
40                 expected = False
41             raise ExtractorError(error, expected=expected)
42
43         manifest = self._download_json(
44             '%s%s.json' % (manifest_root, user_id), user_id)
45
46         formats = []
47         for format_id, format_dict in manifest['formats'].items():
48             if not isinstance(format_dict, dict):
49                 continue
50             encodings = format_dict.get('encodings')
51             if not isinstance(encodings, list):
52                 continue
53             vcodec = format_dict.get('videoCodec')
54             acodec = format_dict.get('audioCodec')
55             for media in encodings:
56                 if not isinstance(media, dict):
57                     continue
58                 media_url = media.get('location')
59                 if not media_url or not isinstance(media_url, compat_str):
60                     continue
61
62                 format_id_list = [format_id]
63                 height = int_or_none(media.get('videoHeight'))
64                 if height is not None:
65                     format_id_list.append('%dp' % height)
66                 f = {
67                     'url': media_url,
68                     'format_id': '-'.join(format_id_list),
69                     'width': int_or_none(media.get('videoWidth')),
70                     'height': height,
71                     'vbr': int_or_none(media.get('videoKbps')),
72                     'abr': int_or_none(media.get('audioKbps')),
73                     'fps': int_or_none(media.get('fps')),
74                     'vcodec': vcodec,
75                     'acodec': acodec,
76                 }
77                 if 'rtmp' in format_id:
78                     f['ext'] = 'flv'
79                 elif 'hls' in format_id:
80                     f.update({
81                         'ext': 'mp4',
82                         # hls skips fragments, preferring rtmp
83                         'preference': -1,
84                     })
85                 else:
86                     continue
87                 formats.append(f)
88         self._sort_formats(formats)
89
90         return {
91             'id': user_id,
92             'title': self._live_title(user_id),
93             'is_live': True,
94             'formats': formats,
95         }