656e715aed66d149d1f5a82c83b0cc0faa95c497
[youtube-dl] / youtube_dl / extractor / chaturbate.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import ExtractorError
7
8
9 class ChaturbateIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:[^/]+\.)?chaturbate\.com/(?:fullvideo/?\?.*?\bb=)?(?P<id>[^/?&#]+)'
11     _TESTS = [{
12         'url': 'https://www.chaturbate.com/siswet19/',
13         'info_dict': {
14             'id': 'siswet19',
15             'ext': 'mp4',
16             'title': 're:^siswet19 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
17             'age_limit': 18,
18             'is_live': True,
19         },
20         'params': {
21             'skip_download': True,
22         },
23         'skip': 'Room is offline',
24     }, {
25         'url': 'https://chaturbate.com/fullvideo/?b=caylin',
26         'only_matching': True,
27     }, {
28         'url': 'https://en.chaturbate.com/siswet19/',
29         'only_matching': True,
30     }]
31
32     _ROOM_OFFLINE = 'Room is currently offline'
33
34     def _real_extract(self, url):
35         video_id = self._match_id(url)
36
37         webpage = self._download_webpage(
38             'https://chaturbate.com/%s/' % video_id, video_id,
39             headers=self.geo_verification_headers())
40
41         m3u8_urls = []
42
43         for m in re.finditer(
44                 r'(["\'])(?P<url>http.+?\.m3u8.*?)\1', webpage):
45             m3u8_fast_url, m3u8_no_fast_url = m.group('url'), m.group(
46                 'url').replace('_fast', '')
47             for m3u8_url in (m3u8_fast_url, m3u8_no_fast_url):
48                 if m3u8_url not in m3u8_urls:
49                     m3u8_urls.append(m3u8_url)
50
51         if not m3u8_urls:
52             error = self._search_regex(
53                 [r'<span[^>]+class=(["\'])desc_span\1[^>]*>(?P<error>[^<]+)</span>',
54                  r'<div[^>]+id=(["\'])defchat\1[^>]*>\s*<p><strong>(?P<error>[^<]+)<'],
55                 webpage, 'error', group='error', default=None)
56             if not error:
57                 if any(p in webpage for p in (
58                         self._ROOM_OFFLINE, 'offline_tipping', 'tip_offline')):
59                     error = self._ROOM_OFFLINE
60             if error:
61                 raise ExtractorError(error, expected=True)
62             raise ExtractorError('Unable to find stream URL')
63
64         formats = []
65         for m3u8_url in m3u8_urls:
66             m3u8_id = 'fast' if '_fast' in m3u8_url else 'slow'
67             formats.extend(self._extract_m3u8_formats(
68                 m3u8_url, video_id, ext='mp4',
69                 # ffmpeg skips segments for fast m3u8
70                 preference=-10 if m3u8_id == 'fast' else None,
71                 m3u8_id=m3u8_id, fatal=False, live=True))
72         self._sort_formats(formats)
73
74         return {
75             'id': video_id,
76             'title': self._live_title(video_id),
77             'thumbnail': 'https://roomimg.stream.highwebmedia.com/ri/%s.jpg' % video_id,
78             'age_limit': self._rta_search(webpage),
79             'is_live': True,
80             'formats': formats,
81         }