]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/reddit.py
3 from .common
import InfoExtractor
16 class RedditIE(InfoExtractor
):
17 _NETRC_MACHINE
= 'reddit'
18 _VALID_URL
= r
'https?://(?P<host>(?:\w+\.)?reddit(?:media)?\.com)/(?P<slug>(?:(?:r|user)/[^/]+/)?comments/(?P<id>[^/?#&]+))'
20 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/',
24 'display_id': '6rrwyj',
25 'title': 'That small heart attack.',
26 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
27 'thumbnails': 'count:4',
28 'timestamp': 1501941939,
29 'upload_date': '20170805',
36 'channel_id': 'videos',
39 'skip_download': True,
42 # 1080p fallback format
43 'url': 'https://www.reddit.com/r/aww/comments/90bu6w/heat_index_was_110_degrees_so_we_offered_him_a/',
44 'md5': '8b5902cfda3006bf90faea7adf765a49',
46 'id': 'gyh95hiqc0b11',
48 'display_id': '90bu6w',
49 'title': 'Heat index was 110 degrees so we offered him a cold drink. He went for a full body soak instead',
50 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
51 'thumbnails': 'count:7',
52 'timestamp': 1532051078,
53 'upload_date': '20180720',
54 'uploader': 'FootLoosePickleJuice',
64 'url': 'https://www.reddit.com/user/creepyt0es/comments/nip71r/i_plan_to_make_more_stickers_and_prints_check/',
66 'id': 'zasobba6wp071',
68 'display_id': 'nip71r',
69 'title': 'I plan to make more stickers and prints! Check them out on my Etsy! Or get them through my Patreon. Links below.',
70 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
71 'thumbnails': 'count:5',
72 'timestamp': 1621709093,
73 'upload_date': '20210522',
74 'uploader': 'creepyt0es',
80 'channel_id': 'u_creepyt0es',
83 'skip_download': True,
86 # videos embedded in reddit text post
87 'url': 'https://www.reddit.com/r/KamenRider/comments/wzqkxp/finale_kamen_rider_revice_episode_50_family_to/',
91 'title': 'md5:72d3d19402aa11eff5bd32fc96369b37',
94 # crossposted reddit-hosted media
95 'url': 'https://www.reddit.com/r/dumbfuckers_club/comments/zjjw82/cringe/',
96 'md5': '746180895c7b75a9d6b05341f507699a',
98 'id': 'a1oneun6pa5a1',
100 'display_id': 'zjjw82',
102 'uploader': 'Otaku-senpai69420',
103 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
104 'upload_date': '20221212',
105 'timestamp': 1670812309,
108 'dislike_count': int,
109 'comment_count': int,
111 'channel_id': 'dumbfuckers_club',
114 # post link without subreddit
115 'url': 'https://www.reddit.com/comments/124pp33',
116 'md5': '15eec9d828adcef4468b741a7e45a395',
118 'id': 'antsenjc2jqa1',
120 'display_id': '124pp33',
121 'title': 'Harmless prank of some old friends',
122 'uploader': 'Dudezila',
123 'channel_id': 'ContagiousLaughter',
125 'upload_date': '20230328',
126 'timestamp': 1680012043,
127 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
129 'comment_count': int,
130 'dislike_count': int,
134 # quarantined subreddit post
135 'url': 'https://old.reddit.com/r/GenZedong/comments/12fujy3/based_hasan/',
136 'md5': '3156ea69e3c1f1b6259683c5abd36e71',
138 'id': '8bwtclfggpsa1',
140 'display_id': '12fujy3',
141 'title': 'Based Hasan?',
142 'uploader': 'KingNigelXLII',
143 'channel_id': 'GenZedong',
145 'upload_date': '20230408',
146 'timestamp': 1680979138,
148 'comment_count': int,
149 'dislike_count': int,
152 'skip': 'Requires account that has opted-in to the GenZedong subreddit',
154 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj',
155 'only_matching': True,
158 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
159 'only_matching': True,
162 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
163 'only_matching': True,
166 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/',
167 'only_matching': True,
170 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/',
171 'only_matching': True,
173 # reddit video @ nm reddit
174 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/',
175 'only_matching': True,
177 'url': 'https://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/',
178 'only_matching': True,
181 def _perform_login(self
, username
, password
):
182 captcha
= self
._download
_json
(
183 'https://www.reddit.com/api/requires_captcha/login.json', None,
184 'Checking login requirement')['required']
186 raise ExtractorError('Reddit is requiring captcha before login', expected
=True)
187 login
= self
._download
_json
(
188 f
'https://www.reddit.com/api/login/{username}', None, data
=urlencode_postdata({
193 }), note
='Logging in', errnote
='Login request failed')
194 errors
= '; '.join(traverse_obj(login
, ('json', 'errors', ..., 1)))
196 raise ExtractorError(f
'Unable to login, Reddit API says {errors}', expected
=True)
197 elif not traverse_obj(login
, ('json', 'data', 'cookie', {str}
)):
198 raise ExtractorError('Unable to login, no cookie was returned')
200 def _real_extract(self
, url
):
201 host
, slug
, video_id
= self
._match
_valid
_url
(url
).group('host', 'slug', 'id')
203 data
= self
._download
_json
(
204 f
'https://{host}/{slug}/.json', video_id
, fatal
=False, expected_status
=403)
206 fallback_host
= 'old.reddit.com' if host
!= 'old.reddit.com' else 'www.reddit.com'
207 self
.to_screen(f
'{host} request failed, retrying with {fallback_host}')
208 data
= self
._download
_json
(
209 f
'https://{fallback_host}/{slug}/.json', video_id
, expected_status
=403)
211 if traverse_obj(data
, 'error') == 403:
212 reason
= data
.get('reason')
213 if reason
== 'quarantined':
214 self
.raise_login_required('Quarantined subreddit; an account that has opted in is required')
215 elif reason
== 'private':
216 self
.raise_login_required('Private subreddit; an account that has been approved is required')
218 raise ExtractorError(f
'HTTP Error 403 Forbidden; reason given: {reason}')
220 data
= data
[0]['data']['children'][0]['data']
221 video_url
= data
['url']
223 over_18
= data
.get('over_18')
226 elif over_18
is False:
233 def add_thumbnail(src
):
234 if not isinstance(src
, dict):
236 thumbnail_url
= url_or_none(src
.get('url'))
237 if not thumbnail_url
:
240 'url': unescapeHTML(thumbnail_url
),
241 'width': int_or_none(src
.get('width')),
242 'height': int_or_none(src
.get('height')),
245 for image
in try_get(data
, lambda x
: x
['preview']['images']) or []:
246 if not isinstance(image
, dict):
248 add_thumbnail(image
.get('source'))
249 resolutions
= image
.get('resolutions')
250 if isinstance(resolutions
, list):
251 for resolution
in resolutions
:
252 add_thumbnail(resolution
)
255 'title': data
.get('title'),
256 'thumbnails': thumbnails
,
257 'timestamp': float_or_none(data
.get('created_utc')),
258 'uploader': data
.get('author'),
259 'channel_id': data
.get('subreddit'),
260 'like_count': int_or_none(data
.get('ups')),
261 'dislike_count': int_or_none(data
.get('downs')),
262 'comment_count': int_or_none(data
.get('num_comments')),
263 'age_limit': age_limit
,
266 parsed_url
= urllib
.parse
.urlparse(video_url
)
268 # Check for embeds in text posts, or else raise to avoid recursing into the same reddit URL
269 if 'reddit.com' in parsed_url
.netloc
and f
'/{video_id}/' in parsed_url
.path
:
271 for media
in traverse_obj(data
, ('media_metadata', ...), expected_type
=dict):
272 if not media
.get('id') or media
.get('e') != 'RedditVideo':
275 if media
.get('hlsUrl'):
276 formats
.extend(self
._extract
_m
3u8_formats
(
277 unescapeHTML(media
['hlsUrl']), video_id
, 'mp4', m3u8_id
='hls', fatal
=False))
278 if media
.get('dashUrl'):
279 formats
.extend(self
._extract
_mpd
_formats
(
280 unescapeHTML(media
['dashUrl']), video_id
, mpd_id
='dash', fatal
=False))
284 'display_id': video_id
,
289 return self
.playlist_result(entries
, video_id
, info
.get('title'))
290 raise ExtractorError('No media found', expected
=True)
292 # Check if media is hosted on reddit:
293 reddit_video
= traverse_obj(data
, (
294 (None, ('crosspost_parent_list', ...)), ('secure_media', 'media'), 'reddit_video'), get_all
=False)
297 try_get(reddit_video
, lambda x
: unescapeHTML(x
[y
]))
298 for y
in ('dash_url', 'hls_url')
302 display_id
= video_id
303 video_id
= self
._search
_regex
(
304 r
'https?://v\.redd\.it/(?P<id>[^/?#&]+)', reddit_video
['fallback_url'],
305 'video_id', default
=display_id
)
307 dash_playlist_url
= playlist_urls
[0] or f
'https://v.redd.it/{video_id}/DASHPlaylist.mpd'
308 hls_playlist_url
= playlist_urls
[1] or f
'https://v.redd.it/{video_id}/HLSPlaylist.m3u8'
311 'url': unescapeHTML(reddit_video
['fallback_url']),
312 'height': int_or_none(reddit_video
.get('height')),
313 'width': int_or_none(reddit_video
.get('width')),
314 'tbr': int_or_none(reddit_video
.get('bitrate_kbps')),
318 'format_id': 'fallback',
319 'format_note': 'DASH video, mp4_dash',
321 formats
.extend(self
._extract
_m
3u8_formats
(
322 hls_playlist_url
, display_id
, 'mp4', m3u8_id
='hls', fatal
=False))
323 formats
.extend(self
._extract
_mpd
_formats
(
324 dash_playlist_url
, display_id
, mpd_id
='dash', fatal
=False))
329 'display_id': display_id
,
331 'duration': int_or_none(reddit_video
.get('duration')),
334 if parsed_url
.netloc
== 'v.redd.it':
335 self
.raise_no_formats('This video is processing', expected
=True, video_id
=video_id
)
338 'id': parsed_url
.path
.split('/')[1],
339 'display_id': video_id
,
342 # Not hosted on reddit, must continue extraction
345 'display_id': video_id
,
346 '_type': 'url_transparent',