[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / chaturbate.py
blobd031d3985e3385f0d115a7377078fe1cafd85107
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 lowercase_escape,
7 url_or_none,
8 urlencode_postdata,
12 class ChaturbateIE(InfoExtractor):
13 _VALID_URL = r'https?://(?:[^/]+\.)?chaturbate\.(?P<tld>com|eu|global)/(?:fullvideo/?\?.*?\bb=)?(?P<id>[^/?&#]+)'
14 _TESTS = [{
15 'url': 'https://www.chaturbate.com/siswet19/',
16 'info_dict': {
17 'id': 'siswet19',
18 'ext': 'mp4',
19 'title': 're:^siswet19 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
20 'age_limit': 18,
21 'is_live': True,
23 'params': {
24 'skip_download': True,
26 'skip': 'Room is offline',
27 }, {
28 'url': 'https://chaturbate.com/fullvideo/?b=caylin',
29 'only_matching': True,
30 }, {
31 'url': 'https://en.chaturbate.com/siswet19/',
32 'only_matching': True,
33 }, {
34 'url': 'https://chaturbate.eu/siswet19/',
35 'only_matching': True,
36 }, {
37 'url': 'https://chaturbate.eu/fullvideo/?b=caylin',
38 'only_matching': True,
39 }, {
40 'url': 'https://chaturbate.global/siswet19/',
41 'only_matching': True,
44 _ERROR_MAP = {
45 'offline': 'Room is currently offline',
46 'private': 'Room is currently in a private show',
47 'away': 'Performer is currently away',
48 'password protected': 'Room is password protected',
49 'hidden': 'Hidden session in progress',
52 def _extract_from_api(self, video_id, tld):
53 response = self._download_json(
54 f'https://chaturbate.{tld}/get_edge_hls_url_ajax/', video_id,
55 data=urlencode_postdata({'room_slug': video_id}),
56 headers={
57 **self.geo_verification_headers(),
58 'X-Requested-With': 'XMLHttpRequest',
59 'Accept': 'application/json',
60 }, fatal=False, impersonate=True) or {}
62 m3u8_url = response.get('url')
63 if not m3u8_url:
64 status = response.get('room_status')
65 if error := self._ERROR_MAP.get(status):
66 raise ExtractorError(error, expected=True)
67 if status == 'public':
68 self.raise_geo_restricted()
69 self.report_warning(f'Got status "{status}" from API; falling back to webpage extraction')
70 return None
72 return {
73 'id': video_id,
74 'title': video_id,
75 'thumbnail': f'https://roomimg.stream.highwebmedia.com/ri/{video_id}.jpg',
76 'is_live': True,
77 'age_limit': 18,
78 'formats': self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4', live=True),
81 def _extract_from_html(self, video_id, tld):
82 webpage = self._download_webpage(
83 f'https://chaturbate.{tld}/{video_id}/', video_id,
84 headers=self.geo_verification_headers(), impersonate=True)
86 found_m3u8_urls = []
88 data = self._parse_json(
89 self._search_regex(
90 r'initialRoomDossier\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1',
91 webpage, 'data', default='{}', group='value'),
92 video_id, transform_source=lowercase_escape, fatal=False)
93 if data:
94 m3u8_url = url_or_none(data.get('hls_source'))
95 if m3u8_url:
96 found_m3u8_urls.append(m3u8_url)
98 if not found_m3u8_urls:
99 for m in re.finditer(
100 r'(\\u002[27])(?P<url>http.+?\.m3u8.*?)\1', webpage):
101 found_m3u8_urls.append(lowercase_escape(m.group('url')))
103 if not found_m3u8_urls:
104 for m in re.finditer(
105 r'(["\'])(?P<url>http.+?\.m3u8.*?)\1', webpage):
106 found_m3u8_urls.append(m.group('url'))
108 m3u8_urls = []
109 for found_m3u8_url in found_m3u8_urls:
110 m3u8_fast_url, m3u8_no_fast_url = found_m3u8_url, found_m3u8_url.replace('_fast', '')
111 for m3u8_url in (m3u8_fast_url, m3u8_no_fast_url):
112 if m3u8_url not in m3u8_urls:
113 m3u8_urls.append(m3u8_url)
115 if not m3u8_urls:
116 error = self._search_regex(
117 [r'<span[^>]+class=(["\'])desc_span\1[^>]*>(?P<error>[^<]+)</span>',
118 r'<div[^>]+id=(["\'])defchat\1[^>]*>\s*<p><strong>(?P<error>[^<]+)<'],
119 webpage, 'error', group='error', default=None)
120 if not error:
121 if any(p in webpage for p in (
122 self._ERROR_MAP['offline'], 'offline_tipping', 'tip_offline')):
123 error = self._ERROR_MAP['offline']
124 if error:
125 raise ExtractorError(error, expected=True)
126 raise ExtractorError('Unable to find stream URL')
128 formats = []
129 for m3u8_url in m3u8_urls:
130 for known_id in ('fast', 'slow'):
131 if f'_{known_id}' in m3u8_url:
132 m3u8_id = known_id
133 break
134 else:
135 m3u8_id = None
136 formats.extend(self._extract_m3u8_formats(
137 m3u8_url, video_id, ext='mp4',
138 # ffmpeg skips segments for fast m3u8
139 preference=-10 if m3u8_id == 'fast' else None,
140 m3u8_id=m3u8_id, fatal=False, live=True))
142 return {
143 'id': video_id,
144 'title': video_id,
145 'thumbnail': f'https://roomimg.stream.highwebmedia.com/ri/{video_id}.jpg',
146 'age_limit': self._rta_search(webpage),
147 'is_live': True,
148 'formats': formats,
151 def _real_extract(self, url):
152 video_id, tld = self._match_valid_url(url).group('id', 'tld')
153 return self._extract_from_api(video_id, tld) or self._extract_from_html(video_id, tld)