[chaturbate] Fix extraction
[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/(?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://en.chaturbate.com/siswet19/',
26         'only_matching': True,
27     }]
28
29     _ROOM_OFFLINE = 'Room is currently offline'
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33
34         webpage = self._download_webpage(url, video_id)
35
36         m3u8_urls = re.findall(
37             r'var hlsSource.+? = (["\'])(?P<url>http.+?\.m3u8)', webpage)
38
39         if not m3u8_urls:
40             error = self._search_regex(
41                 [r'<span[^>]+class=(["\'])desc_span\1[^>]*>(?P<error>[^<]+)</span>',
42                  r'<div[^>]+id=(["\'])defchat\1[^>]*>\s*<p><strong>(?P<error>[^<]+)<'],
43                 webpage, 'error', group='error', default=None)
44             if not error:
45                 if any(p in webpage for p in (
46                         self._ROOM_OFFLINE, 'offline_tipping', 'tip_offline')):
47                     error = self._ROOM_OFFLINE
48             if error:
49                 raise ExtractorError(error, expected=True)
50             raise ExtractorError('Unable to find stream URL')
51
52         formats = []
53         for m3u8_url in m3u8_urls:
54             formats.append(self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4')[0])
55
56         self._sort_formats(formats)
57
58         return {
59             'id': video_id,
60             'title': self._live_title(video_id),
61             'thumbnail': 'https://roomimg.stream.highwebmedia.com/ri/%s.jpg' % video_id,
62             'age_limit': self._rta_search(webpage),
63             'is_live': True,
64             'formats': formats,
65         }