]>
Commit | Line | Data |
---|---|---|
8e3fd7e0 | 1 | import random |
07689fc1 | 2 | from urllib.parse import urlparse |
9bb2c767 | 3 | |
0c43a481 S |
4 | from .common import InfoExtractor |
5 | from ..utils import ( | |
6 | ExtractorError, | |
7 | int_or_none, | |
8 | float_or_none, | |
29f7c58a | 9 | try_get, |
10 | unescapeHTML, | |
97abf05a | 11 | url_or_none, |
e16fefd8 | 12 | traverse_obj |
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 | }, | |
0c43a481 S |
59 | }, { |
60 | 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj', | |
61 | 'only_matching': True, | |
62 | }, { | |
63 | # imgur | |
64 | 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/', | |
65 | 'only_matching': True, | |
12b0d4e0 M |
66 | }, { |
67 | # imgur @ old reddit | |
68 | 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/', | |
69 | 'only_matching': True, | |
0c43a481 S |
70 | }, { |
71 | # streamable | |
72 | 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/', | |
73 | 'only_matching': True, | |
74 | }, { | |
75 | # youtube | |
76 | 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/', | |
77 | 'only_matching': True, | |
dbd5c502 | 78 | }, { |
79 | # reddit video @ nm reddit | |
80 | 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/', | |
81 | 'only_matching': True, | |
c470901c | 82 | }, { |
83 | 'url': 'https://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/', | |
84 | 'only_matching': True, | |
0c43a481 S |
85 | }] |
86 | ||
8e3fd7e0 | 87 | @staticmethod |
88 | def _gen_session_id(): | |
89 | id_length = 16 | |
90 | rand_max = 1 << (id_length * 4) | |
91 | return '%0.*x' % (id_length, random.randrange(rand_max)) | |
0c43a481 | 92 | |
8e3fd7e0 | 93 | def _real_extract(self, url): |
94 | subdomain, slug, video_id = self._match_valid_url(url).group('subdomain', 'slug', 'id') | |
95 | ||
96 | self._set_cookie('.reddit.com', 'reddit_session', self._gen_session_id()) | |
97 | self._set_cookie('.reddit.com', '_options', '%7B%22pref_quarantine_optin%22%3A%20true%7D') | |
1b6bb4a8 | 98 | data = self._download_json(f'https://{subdomain}reddit.com/r/{slug}/.json', video_id, fatal=False) |
8e3fd7e0 | 99 | if not data: |
100 | # Fall back to old.reddit.com in case the requested subdomain fails | |
101 | data = self._download_json(f'https://old.reddit.com/r/{slug}/.json', video_id) | |
102 | data = data[0]['data']['children'][0]['data'] | |
0c43a481 S |
103 | video_url = data['url'] |
104 | ||
105 | # Avoid recursing into the same reddit URL | |
106 | if 'reddit.com/' in video_url and '/%s/' % video_id in video_url: | |
107 | raise ExtractorError('No media found', expected=True) | |
108 | ||
109 | over_18 = data.get('over_18') | |
110 | if over_18 is True: | |
111 | age_limit = 18 | |
112 | elif over_18 is False: | |
113 | age_limit = 0 | |
114 | else: | |
115 | age_limit = None | |
116 | ||
29f7c58a | 117 | thumbnails = [] |
118 | ||
119 | def add_thumbnail(src): | |
120 | if not isinstance(src, dict): | |
121 | return | |
122 | thumbnail_url = url_or_none(src.get('url')) | |
123 | if not thumbnail_url: | |
124 | return | |
125 | thumbnails.append({ | |
126 | 'url': unescapeHTML(thumbnail_url), | |
127 | 'width': int_or_none(src.get('width')), | |
128 | 'height': int_or_none(src.get('height')), | |
129 | }) | |
130 | ||
131 | for image in try_get(data, lambda x: x['preview']['images']) or []: | |
132 | if not isinstance(image, dict): | |
133 | continue | |
134 | add_thumbnail(image.get('source')) | |
135 | resolutions = image.get('resolutions') | |
136 | if isinstance(resolutions, list): | |
137 | for resolution in resolutions: | |
138 | add_thumbnail(resolution) | |
139 | ||
e16fefd8 | 140 | info = { |
0c43a481 | 141 | 'title': data.get('title'), |
29f7c58a | 142 | 'thumbnails': thumbnails, |
0c43a481 S |
143 | 'timestamp': float_or_none(data.get('created_utc')), |
144 | 'uploader': data.get('author'), | |
145 | 'like_count': int_or_none(data.get('ups')), | |
146 | 'dislike_count': int_or_none(data.get('downs')), | |
147 | 'comment_count': int_or_none(data.get('num_comments')), | |
148 | 'age_limit': age_limit, | |
149 | } | |
e16fefd8 JL |
150 | |
151 | # Check if media is hosted on reddit: | |
152 | reddit_video = traverse_obj(data, (('media', 'secure_media'), 'reddit_video'), get_all=False) | |
153 | if reddit_video: | |
154 | playlist_urls = [ | |
155 | try_get(reddit_video, lambda x: unescapeHTML(x[y])) | |
156 | for y in ('dash_url', 'hls_url') | |
157 | ] | |
158 | ||
159 | # Update video_id | |
160 | display_id = video_id | |
161 | video_id = self._search_regex( | |
162 | r'https?://v\.redd\.it/(?P<id>[^/?#&]+)', reddit_video['fallback_url'], | |
163 | 'video_id', default=display_id) | |
164 | ||
165 | dash_playlist_url = playlist_urls[0] or f'https://v.redd.it/{video_id}/DASHPlaylist.mpd' | |
166 | hls_playlist_url = playlist_urls[1] or f'https://v.redd.it/{video_id}/HLSPlaylist.m3u8' | |
167 | ||
2e565f5b | 168 | formats = [{ |
169 | 'url': unescapeHTML(reddit_video['fallback_url']), | |
170 | 'height': int_or_none(reddit_video.get('height')), | |
171 | 'width': int_or_none(reddit_video.get('width')), | |
172 | 'tbr': int_or_none(reddit_video.get('bitrate_kbps')), | |
173 | 'acodec': 'none', | |
174 | 'ext': 'mp4', | |
175 | 'format_id': 'fallback', | |
176 | 'format_note': 'DASH video, mp4_dash', | |
177 | }] | |
178 | formats.extend(self._extract_m3u8_formats( | |
179 | hls_playlist_url, display_id, 'mp4', m3u8_id='hls', fatal=False)) | |
e16fefd8 JL |
180 | formats.extend(self._extract_mpd_formats( |
181 | dash_playlist_url, display_id, mpd_id='dash', fatal=False)) | |
182 | self._sort_formats(formats) | |
183 | ||
184 | return { | |
185 | **info, | |
186 | 'id': video_id, | |
187 | 'display_id': display_id, | |
188 | 'formats': formats, | |
189 | 'duration': int_or_none(reddit_video.get('duration')), | |
190 | } | |
191 | ||
07689fc1 | 192 | parsed_url = urlparse(video_url) |
193 | if parsed_url.netloc == 'v.redd.it': | |
194 | self.raise_no_formats('This video is processing', expected=True, video_id=video_id) | |
195 | return { | |
196 | **info, | |
197 | 'id': parsed_url.path.split('/')[1], | |
198 | 'display_id': video_id, | |
199 | } | |
200 | ||
e16fefd8 JL |
201 | # Not hosted on reddit, must continue extraction |
202 | return { | |
203 | **info, | |
204 | 'display_id': video_id, | |
205 | '_type': 'url_transparent', | |
206 | 'url': video_url, | |
207 | } |