]>
Commit | Line | Data |
---|---|---|
8e3fd7e0 | 1 | import random |
0e96b408 | 2 | import urllib.parse |
9bb2c767 | 3 | |
0c43a481 S |
4 | from .common import InfoExtractor |
5 | from ..utils import ( | |
6 | ExtractorError, | |
0c43a481 | 7 | float_or_none, |
0e96b408 | 8 | int_or_none, |
9 | traverse_obj, | |
29f7c58a | 10 | try_get, |
11 | unescapeHTML, | |
97abf05a | 12 | url_or_none, |
0c43a481 S |
13 | ) |
14 | ||
15 | ||
16 | class RedditIE(InfoExtractor): | |
8e3fd7e0 | 17 | _VALID_URL = r'https?://(?P<subdomain>[^/]+\.)?reddit(?:media)?\.com/r/(?P<slug>[^/]+/comments/(?P<id>[^/?#&]+))' |
0c43a481 S |
18 | _TESTS = [{ |
19 | 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/', | |
20 | 'info_dict': { | |
21 | 'id': 'zv89llsvexdz', | |
22 | 'ext': 'mp4', | |
07689fc1 | 23 | 'display_id': '6rrwyj', |
0c43a481 | 24 | 'title': 'That small heart attack.', |
29f7c58a | 25 | 'thumbnail': r're:^https?://.*\.(?:jpg|png)', |
26 | 'thumbnails': 'count:4', | |
0c43a481 S |
27 | 'timestamp': 1501941939, |
28 | 'upload_date': '20170805', | |
29 | 'uploader': 'Antw87', | |
29f7c58a | 30 | 'duration': 12, |
0c43a481 S |
31 | 'like_count': int, |
32 | 'dislike_count': int, | |
33 | 'comment_count': int, | |
34 | 'age_limit': 0, | |
35 | }, | |
36 | 'params': { | |
0c43a481 S |
37 | 'skip_download': True, |
38 | }, | |
2e565f5b | 39 | }, { |
40 | # 1080p fallback format | |
41 | 'url': 'https://www.reddit.com/r/aww/comments/90bu6w/heat_index_was_110_degrees_so_we_offered_him_a/', | |
42 | 'md5': '8b5902cfda3006bf90faea7adf765a49', | |
43 | 'info_dict': { | |
44 | 'id': 'gyh95hiqc0b11', | |
45 | 'ext': 'mp4', | |
46 | 'display_id': '90bu6w', | |
47 | 'title': 'Heat index was 110 degrees so we offered him a cold drink. He went for a full body soak instead', | |
48 | 'thumbnail': r're:^https?://.*\.(?:jpg|png)', | |
49 | 'thumbnails': 'count:7', | |
50 | 'timestamp': 1532051078, | |
51 | 'upload_date': '20180720', | |
52 | 'uploader': 'FootLoosePickleJuice', | |
53 | 'duration': 14, | |
54 | 'like_count': int, | |
55 | 'dislike_count': int, | |
56 | 'comment_count': int, | |
57 | 'age_limit': 0, | |
58 | }, | |
0e96b408 | 59 | }, { |
60 | # videos embedded in reddit text post | |
61 | 'url': 'https://www.reddit.com/r/KamenRider/comments/wzqkxp/finale_kamen_rider_revice_episode_50_family_to/', | |
62 | 'playlist_count': 2, | |
63 | 'info_dict': { | |
64 | 'id': 'wzqkxp', | |
65 | 'title': 'md5:72d3d19402aa11eff5bd32fc96369b37', | |
66 | }, | |
0c43a481 S |
67 | }, { |
68 | 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj', | |
69 | 'only_matching': True, | |
70 | }, { | |
71 | # imgur | |
72 | 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/', | |
73 | 'only_matching': True, | |
12b0d4e0 M |
74 | }, { |
75 | # imgur @ old reddit | |
76 | 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/', | |
77 | 'only_matching': True, | |
0c43a481 S |
78 | }, { |
79 | # streamable | |
80 | 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/', | |
81 | 'only_matching': True, | |
82 | }, { | |
83 | # youtube | |
84 | 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/', | |
85 | 'only_matching': True, | |
dbd5c502 | 86 | }, { |
87 | # reddit video @ nm reddit | |
88 | 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/', | |
89 | 'only_matching': True, | |
c470901c | 90 | }, { |
91 | 'url': 'https://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/', | |
92 | 'only_matching': True, | |
0c43a481 S |
93 | }] |
94 | ||
8e3fd7e0 | 95 | @staticmethod |
96 | def _gen_session_id(): | |
97 | id_length = 16 | |
98 | rand_max = 1 << (id_length * 4) | |
99 | return '%0.*x' % (id_length, random.randrange(rand_max)) | |
0c43a481 | 100 | |
8e3fd7e0 | 101 | def _real_extract(self, url): |
102 | subdomain, slug, video_id = self._match_valid_url(url).group('subdomain', 'slug', 'id') | |
103 | ||
104 | self._set_cookie('.reddit.com', 'reddit_session', self._gen_session_id()) | |
105 | self._set_cookie('.reddit.com', '_options', '%7B%22pref_quarantine_optin%22%3A%20true%7D') | |
1b6bb4a8 | 106 | data = self._download_json(f'https://{subdomain}reddit.com/r/{slug}/.json', video_id, fatal=False) |
8e3fd7e0 | 107 | if not data: |
108 | # Fall back to old.reddit.com in case the requested subdomain fails | |
109 | data = self._download_json(f'https://old.reddit.com/r/{slug}/.json', video_id) | |
110 | data = data[0]['data']['children'][0]['data'] | |
0c43a481 S |
111 | video_url = data['url'] |
112 | ||
0c43a481 S |
113 | over_18 = data.get('over_18') |
114 | if over_18 is True: | |
115 | age_limit = 18 | |
116 | elif over_18 is False: | |
117 | age_limit = 0 | |
118 | else: | |
119 | age_limit = None | |
120 | ||
29f7c58a | 121 | thumbnails = [] |
122 | ||
123 | def add_thumbnail(src): | |
124 | if not isinstance(src, dict): | |
125 | return | |
126 | thumbnail_url = url_or_none(src.get('url')) | |
127 | if not thumbnail_url: | |
128 | return | |
129 | thumbnails.append({ | |
130 | 'url': unescapeHTML(thumbnail_url), | |
131 | 'width': int_or_none(src.get('width')), | |
132 | 'height': int_or_none(src.get('height')), | |
133 | }) | |
134 | ||
135 | for image in try_get(data, lambda x: x['preview']['images']) or []: | |
136 | if not isinstance(image, dict): | |
137 | continue | |
138 | add_thumbnail(image.get('source')) | |
139 | resolutions = image.get('resolutions') | |
140 | if isinstance(resolutions, list): | |
141 | for resolution in resolutions: | |
142 | add_thumbnail(resolution) | |
143 | ||
e16fefd8 | 144 | info = { |
0c43a481 | 145 | 'title': data.get('title'), |
29f7c58a | 146 | 'thumbnails': thumbnails, |
0c43a481 S |
147 | 'timestamp': float_or_none(data.get('created_utc')), |
148 | 'uploader': data.get('author'), | |
149 | 'like_count': int_or_none(data.get('ups')), | |
150 | 'dislike_count': int_or_none(data.get('downs')), | |
151 | 'comment_count': int_or_none(data.get('num_comments')), | |
152 | 'age_limit': age_limit, | |
153 | } | |
e16fefd8 | 154 | |
0e96b408 | 155 | parsed_url = urllib.parse.urlparse(video_url) |
156 | ||
157 | # Check for embeds in text posts, or else raise to avoid recursing into the same reddit URL | |
158 | if 'reddit.com' in parsed_url.netloc and f'/{video_id}/' in parsed_url.path: | |
159 | entries = [] | |
160 | for media in traverse_obj(data, ('media_metadata', ...), expected_type=dict): | |
161 | if not media.get('id') or media.get('e') != 'RedditVideo': | |
162 | continue | |
163 | formats = [] | |
164 | if media.get('hlsUrl'): | |
165 | formats.extend(self._extract_m3u8_formats( | |
166 | unescapeHTML(media['hlsUrl']), video_id, 'mp4', m3u8_id='hls', fatal=False)) | |
167 | if media.get('dashUrl'): | |
168 | formats.extend(self._extract_mpd_formats( | |
169 | unescapeHTML(media['dashUrl']), video_id, mpd_id='dash', fatal=False)) | |
170 | if formats: | |
171 | entries.append({ | |
172 | 'id': media['id'], | |
173 | 'display_id': video_id, | |
174 | 'formats': formats, | |
175 | **info, | |
176 | }) | |
177 | if entries: | |
178 | return self.playlist_result(entries, video_id, info.get('title')) | |
179 | raise ExtractorError('No media found', expected=True) | |
180 | ||
e16fefd8 JL |
181 | # Check if media is hosted on reddit: |
182 | reddit_video = traverse_obj(data, (('media', 'secure_media'), 'reddit_video'), get_all=False) | |
183 | if reddit_video: | |
184 | playlist_urls = [ | |
185 | try_get(reddit_video, lambda x: unescapeHTML(x[y])) | |
186 | for y in ('dash_url', 'hls_url') | |
187 | ] | |
188 | ||
189 | # Update video_id | |
190 | display_id = video_id | |
191 | video_id = self._search_regex( | |
192 | r'https?://v\.redd\.it/(?P<id>[^/?#&]+)', reddit_video['fallback_url'], | |
193 | 'video_id', default=display_id) | |
194 | ||
195 | dash_playlist_url = playlist_urls[0] or f'https://v.redd.it/{video_id}/DASHPlaylist.mpd' | |
196 | hls_playlist_url = playlist_urls[1] or f'https://v.redd.it/{video_id}/HLSPlaylist.m3u8' | |
197 | ||
2e565f5b | 198 | formats = [{ |
199 | 'url': unescapeHTML(reddit_video['fallback_url']), | |
200 | 'height': int_or_none(reddit_video.get('height')), | |
201 | 'width': int_or_none(reddit_video.get('width')), | |
202 | 'tbr': int_or_none(reddit_video.get('bitrate_kbps')), | |
203 | 'acodec': 'none', | |
02b2f9fa | 204 | 'vcodec': 'h264', |
2e565f5b | 205 | 'ext': 'mp4', |
206 | 'format_id': 'fallback', | |
207 | 'format_note': 'DASH video, mp4_dash', | |
208 | }] | |
209 | formats.extend(self._extract_m3u8_formats( | |
210 | hls_playlist_url, display_id, 'mp4', m3u8_id='hls', fatal=False)) | |
e16fefd8 JL |
211 | formats.extend(self._extract_mpd_formats( |
212 | dash_playlist_url, display_id, mpd_id='dash', fatal=False)) | |
e16fefd8 JL |
213 | |
214 | return { | |
215 | **info, | |
216 | 'id': video_id, | |
217 | 'display_id': display_id, | |
218 | 'formats': formats, | |
219 | 'duration': int_or_none(reddit_video.get('duration')), | |
220 | } | |
221 | ||
07689fc1 | 222 | if parsed_url.netloc == 'v.redd.it': |
223 | self.raise_no_formats('This video is processing', expected=True, video_id=video_id) | |
224 | return { | |
225 | **info, | |
226 | 'id': parsed_url.path.split('/')[1], | |
227 | 'display_id': video_id, | |
228 | } | |
229 | ||
e16fefd8 JL |
230 | # Not hosted on reddit, must continue extraction |
231 | return { | |
232 | **info, | |
233 | 'display_id': video_id, | |
234 | '_type': 'url_transparent', | |
235 | 'url': video_url, | |
236 | } |