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