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