[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / younow.py
blob409ee758a89b4db3d05d72a386e0bc70f629f9ad
1 import itertools
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 format_field,
7 int_or_none,
8 str_or_none,
9 try_get,
12 CDN_API_BASE = 'https://cdn.younow.com/php/api'
13 MOMENT_URL_FORMAT = f'{CDN_API_BASE}/moment/fetch/id=%s'
16 class YouNowLiveIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?younow\.com/(?P<id>[^/?#&]+)'
18 _TEST = {
19 'url': 'https://www.younow.com/AmandaPadeezy',
20 'info_dict': {
21 'id': 'AmandaPadeezy',
22 'ext': 'mp4',
23 'is_live': True,
24 'title': 'March 26, 2017',
25 'thumbnail': r're:^https?://.*\.jpg$',
26 'tags': ['girls'],
27 'categories': ['girls'],
28 'uploader': 'AmandaPadeezy',
29 'uploader_id': '6716501',
30 'uploader_url': 'https://www.younow.com/AmandaPadeezy',
31 'creator': 'AmandaPadeezy',
33 'skip': True,
36 @classmethod
37 def suitable(cls, url):
38 return (False
39 if YouNowChannelIE.suitable(url) or YouNowMomentIE.suitable(url)
40 else super().suitable(url))
42 def _real_extract(self, url):
43 username = self._match_id(url)
45 data = self._download_json(
46 f'https://api.younow.com/php/api/broadcast/info/curId=0/user={username}', username)
48 if data.get('errorCode') != 0:
49 raise ExtractorError(data['errorMsg'], expected=True)
51 uploader = try_get(
52 data, lambda x: x['user']['profileUrlString'],
53 str) or username
55 return {
56 'id': uploader,
57 'is_live': True,
58 'title': uploader,
59 'thumbnail': data.get('awsUrl'),
60 'tags': data.get('tags'),
61 'categories': data.get('tags'),
62 'uploader': uploader,
63 'uploader_id': data.get('userId'),
64 'uploader_url': f'https://www.younow.com/{username}',
65 'creator': uploader,
66 'view_count': int_or_none(data.get('viewers')),
67 'like_count': int_or_none(data.get('likes')),
68 'formats': [{
69 'url': '{}/broadcast/videoPath/hls=1/broadcastId={}/channelId={}'.format(CDN_API_BASE, data['broadcastId'], data['userId']),
70 'ext': 'mp4',
71 'protocol': 'm3u8',
72 }],
76 def _extract_moment(item, fatal=True):
77 moment_id = item.get('momentId')
78 if not moment_id:
79 if not fatal:
80 return
81 raise ExtractorError('Unable to extract moment id')
83 moment_id = str(moment_id)
85 title = item.get('text')
86 if not title:
87 title = 'YouNow %s' % (
88 item.get('momentType') or item.get('titleType') or 'moment')
90 uploader = try_get(item, lambda x: x['owner']['name'], str)
91 uploader_id = try_get(item, lambda x: x['owner']['userId'])
92 uploader_url = format_field(uploader, None, 'https://www.younow.com/%s')
94 return {
95 'extractor_key': 'YouNowMoment',
96 'id': moment_id,
97 'title': title,
98 'view_count': int_or_none(item.get('views')),
99 'like_count': int_or_none(item.get('likes')),
100 'timestamp': int_or_none(item.get('created')),
101 'creator': uploader,
102 'uploader': uploader,
103 'uploader_id': str_or_none(uploader_id),
104 'uploader_url': uploader_url,
105 'formats': [{
106 'url': f'https://hls.younow.com/momentsplaylists/live/{moment_id}/{moment_id}.m3u8',
107 'ext': 'mp4',
108 'protocol': 'm3u8_native',
113 class YouNowChannelIE(InfoExtractor):
114 _VALID_URL = r'https?://(?:www\.)?younow\.com/(?P<id>[^/]+)/channel'
115 _TEST = {
116 'url': 'https://www.younow.com/its_Kateee_/channel',
117 'info_dict': {
118 'id': '14629760',
119 'title': 'its_Kateee_ moments',
121 'playlist_mincount': 8,
124 def _entries(self, username, channel_id):
125 created_before = 0
126 for page_num in itertools.count(1):
127 if created_before is None:
128 break
129 info = self._download_json(
130 f'{CDN_API_BASE}/moment/profile/channelId={channel_id}/createdBefore={created_before}/records=20',
131 username, note=f'Downloading moments page {page_num}')
132 items = info.get('items')
133 if not items or not isinstance(items, list):
134 break
135 for item in items:
136 if not isinstance(item, dict):
137 continue
138 item_type = item.get('type')
139 if item_type == 'moment':
140 entry = _extract_moment(item, fatal=False)
141 if entry:
142 yield entry
143 elif item_type == 'collection':
144 moments = item.get('momentsIds')
145 if isinstance(moments, list):
146 for moment_id in moments:
147 m = self._download_json(
148 MOMENT_URL_FORMAT % moment_id, username,
149 note=f'Downloading {moment_id} moment JSON',
150 fatal=False)
151 if m and isinstance(m, dict) and m.get('item'):
152 entry = _extract_moment(m['item'])
153 if entry:
154 yield entry
155 created_before = int_or_none(item.get('created'))
157 def _real_extract(self, url):
158 username = self._match_id(url)
159 channel_id = str(self._download_json(
160 f'https://api.younow.com/php/api/broadcast/info/curId=0/user={username}',
161 username, note='Downloading user information')['userId'])
162 return self.playlist_result(
163 self._entries(username, channel_id), channel_id,
164 f'{username} moments')
167 class YouNowMomentIE(InfoExtractor):
168 _VALID_URL = r'https?://(?:www\.)?younow\.com/[^/]+/(?P<id>[^/?#&]+)'
169 _TEST = {
170 'url': 'https://www.younow.com/GABO.../20712117/36319236/3b316doc/m',
171 'md5': 'a30c70eadb9fb39a1aa3c8c0d22a0807',
172 'info_dict': {
173 'id': '20712117',
174 'ext': 'mp4',
175 'title': 'YouNow capture',
176 'view_count': int,
177 'like_count': int,
178 'timestamp': 1490432040,
179 'upload_date': '20170325',
180 'uploader': 'GABO...',
181 'uploader_id': '35917228',
185 @classmethod
186 def suitable(cls, url):
187 return (False
188 if YouNowChannelIE.suitable(url)
189 else super().suitable(url))
191 def _real_extract(self, url):
192 video_id = self._match_id(url)
193 item = self._download_json(MOMENT_URL_FORMAT % video_id, video_id)
194 return _extract_moment(item['item'])