]>
Commit | Line | Data |
---|---|---|
1 | import itertools | |
2 | ||
3 | from .common import InfoExtractor | |
4 | from ..compat import compat_str | |
5 | from ..utils import ( | |
6 | ExtractorError, | |
7 | format_field, | |
8 | int_or_none, | |
9 | try_get, | |
10 | ) | |
11 | ||
12 | CDN_API_BASE = 'https://cdn.younow.com/php/api' | |
13 | MOMENT_URL_FORMAT = '%s/moment/fetch/id=%%s' % CDN_API_BASE | |
14 | ||
15 | ||
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', | |
32 | }, | |
33 | 'skip': True, | |
34 | } | |
35 | ||
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 | ||
42 | def _real_extract(self, url): | |
43 | username = self._match_id(url) | |
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 | |
55 | ||
56 | return { | |
57 | 'id': uploader, | |
58 | 'is_live': True, | |
59 | 'title': uploader, | |
60 | 'thumbnail': data.get('awsUrl'), | |
61 | 'tags': data.get('tags'), | |
62 | 'categories': data.get('tags'), | |
63 | 'uploader': uploader, | |
64 | 'uploader_id': data.get('userId'), | |
65 | 'uploader_url': 'https://www.younow.com/%s' % username, | |
66 | 'creator': uploader, | |
67 | 'view_count': int_or_none(data.get('viewers')), | |
68 | 'like_count': int_or_none(data.get('likes')), | |
69 | 'formats': [{ | |
70 | 'url': '%s/broadcast/videoPath/hls=1/broadcastId=%s/channelId=%s' | |
71 | % (CDN_API_BASE, data['broadcastId'], data['userId']), | |
72 | 'ext': 'mp4', | |
73 | 'protocol': 'm3u8', | |
74 | }], | |
75 | } | |
76 | ||
77 | ||
78 | def _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 | ||
87 | title = item.get('text') | |
88 | if not title: | |
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']) | |
94 | uploader_url = format_field(uploader, None, 'https://www.younow.com/%s') | |
95 | ||
96 | entry = { | |
97 | 'extractor_key': 'YouNowMoment', | |
98 | 'id': moment_id, | |
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')), | |
103 | 'creator': uploader, | |
104 | 'uploader': uploader, | |
105 | 'uploader_id': uploader_id, | |
106 | 'uploader_url': uploader_url, | |
107 | 'formats': [{ | |
108 | 'url': 'https://hls.younow.com/momentsplaylists/live/%s/%s.m3u8' | |
109 | % (moment_id, moment_id), | |
110 | 'ext': 'mp4', | |
111 | 'protocol': 'm3u8_native', | |
112 | }], | |
113 | } | |
114 | ||
115 | return entry | |
116 | ||
117 | ||
118 | class YouNowChannelIE(InfoExtractor): | |
119 | _VALID_URL = r'https?://(?:www\.)?younow\.com/(?P<id>[^/]+)/channel' | |
120 | _TEST = { | |
121 | 'url': 'https://www.younow.com/its_Kateee_/channel', | |
122 | 'info_dict': { | |
123 | 'id': '14629760', | |
124 | 'title': 'its_Kateee_ moments' | |
125 | }, | |
126 | 'playlist_mincount': 8, | |
127 | } | |
128 | ||
129 | def _entries(self, username, channel_id): | |
130 | created_before = 0 | |
131 | for page_num in itertools.count(1): | |
132 | if created_before is None: | |
133 | break | |
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')) | |
162 | ||
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) | |
171 | ||
172 | ||
173 | class YouNowMomentIE(InfoExtractor): | |
174 | _VALID_URL = r'https?://(?:www\.)?younow\.com/[^/]+/(?P<id>[^/?#&]+)' | |
175 | _TEST = { | |
176 | 'url': 'https://www.younow.com/GABO.../20712117/36319236/3b316doc/m', | |
177 | 'md5': 'a30c70eadb9fb39a1aa3c8c0d22a0807', | |
178 | 'info_dict': { | |
179 | 'id': '20712117', | |
180 | 'ext': 'mp4', | |
181 | 'title': 'YouNow capture', | |
182 | 'view_count': int, | |
183 | 'like_count': int, | |
184 | 'timestamp': 1490432040, | |
185 | 'upload_date': '20170325', | |
186 | 'uploader': 'GABO...', | |
187 | 'uploader_id': 35917228, | |
188 | }, | |
189 | } | |
190 | ||
191 | @classmethod | |
192 | def suitable(cls, url): | |
193 | return (False | |
194 | if YouNowChannelIE.suitable(url) | |
195 | else super(YouNowMomentIE, cls).suitable(url)) | |
196 | ||
197 | def _real_extract(self, url): | |
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']) |