]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/reddit.py
4 from .common
import InfoExtractor
16 class RedditIE(InfoExtractor
):
17 _VALID_URL
= r
'https?://(?P<subdomain>[^/]+\.)?reddit(?:media)?\.com/(?P<slug>(?:r|user)/[^/]+/comments/(?P<id>[^/?#&]+))'
19 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/',
23 'display_id': '6rrwyj',
24 'title': 'That small heart attack.',
25 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
26 'thumbnails': 'count:4',
27 'timestamp': 1501941939,
28 'upload_date': '20170805',
35 'channel_id': 'videos',
38 'skip_download': True,
41 # 1080p fallback format
42 'url': 'https://www.reddit.com/r/aww/comments/90bu6w/heat_index_was_110_degrees_so_we_offered_him_a/',
43 'md5': '8b5902cfda3006bf90faea7adf765a49',
45 'id': 'gyh95hiqc0b11',
47 'display_id': '90bu6w',
48 'title': 'Heat index was 110 degrees so we offered him a cold drink. He went for a full body soak instead',
49 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
50 'thumbnails': 'count:7',
51 'timestamp': 1532051078,
52 'upload_date': '20180720',
53 'uploader': 'FootLoosePickleJuice',
63 'url': 'https://www.reddit.com/user/creepyt0es/comments/nip71r/i_plan_to_make_more_stickers_and_prints_check/',
65 'id': 'zasobba6wp071',
67 'display_id': 'nip71r',
68 'title': 'I plan to make more stickers and prints! Check them out on my Etsy! Or get them through my Patreon. Links below.',
69 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
70 'thumbnails': 'count:5',
71 'timestamp': 1621709093,
72 'upload_date': '20210522',
73 'uploader': 'creepyt0es',
79 'channel_id': 'u_creepyt0es',
82 'skip_download': True,
85 # videos embedded in reddit text post
86 'url': 'https://www.reddit.com/r/KamenRider/comments/wzqkxp/finale_kamen_rider_revice_episode_50_family_to/',
90 'title': 'md5:72d3d19402aa11eff5bd32fc96369b37',
93 # crossposted reddit-hosted media
94 'url': 'https://www.reddit.com/r/dumbfuckers_club/comments/zjjw82/cringe/',
95 'md5': '746180895c7b75a9d6b05341f507699a',
97 'id': 'a1oneun6pa5a1',
99 'display_id': 'zjjw82',
101 'uploader': 'Otaku-senpai69420',
102 'thumbnail': r
're:^https?://.*\.(?:jpg|png)',
103 'upload_date': '20221212',
104 'timestamp': 1670812309,
107 'dislike_count': int,
108 'comment_count': int,
110 'channel_id': 'dumbfuckers_club',
113 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj',
114 'only_matching': True,
117 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
118 'only_matching': True,
121 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
122 'only_matching': True,
125 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/',
126 'only_matching': True,
129 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/',
130 'only_matching': True,
132 # reddit video @ nm reddit
133 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/',
134 'only_matching': True,
136 'url': 'https://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/',
137 'only_matching': True,
141 def _gen_session_id():
143 rand_max
= 1 << (id_length
* 4)
144 return '%0.*x' % (id_length
, random
.randrange(rand_max
))
146 def _real_extract(self
, url
):
147 subdomain
, slug
, video_id
= self
._match
_valid
_url
(url
).group('subdomain', 'slug', 'id')
149 self
._set
_cookie
('.reddit.com', 'reddit_session', self
._gen
_session
_id
())
150 self
._set
_cookie
('.reddit.com', '_options', '%7B%22pref_quarantine_optin%22%3A%20true%7D')
151 data
= self
._download
_json
(f
'https://{subdomain}reddit.com/{slug}/.json', video_id
, fatal
=False)
153 # Fall back to old.reddit.com in case the requested subdomain fails
154 data
= self
._download
_json
(f
'https://old.reddit.com/{slug}/.json', video_id
)
155 data
= data
[0]['data']['children'][0]['data']
156 video_url
= data
['url']
158 over_18
= data
.get('over_18')
161 elif over_18
is False:
168 def add_thumbnail(src
):
169 if not isinstance(src
, dict):
171 thumbnail_url
= url_or_none(src
.get('url'))
172 if not thumbnail_url
:
175 'url': unescapeHTML(thumbnail_url
),
176 'width': int_or_none(src
.get('width')),
177 'height': int_or_none(src
.get('height')),
180 for image
in try_get(data
, lambda x
: x
['preview']['images']) or []:
181 if not isinstance(image
, dict):
183 add_thumbnail(image
.get('source'))
184 resolutions
= image
.get('resolutions')
185 if isinstance(resolutions
, list):
186 for resolution
in resolutions
:
187 add_thumbnail(resolution
)
190 'title': data
.get('title'),
191 'thumbnails': thumbnails
,
192 'timestamp': float_or_none(data
.get('created_utc')),
193 'uploader': data
.get('author'),
194 'channel_id': data
.get('subreddit'),
195 'like_count': int_or_none(data
.get('ups')),
196 'dislike_count': int_or_none(data
.get('downs')),
197 'comment_count': int_or_none(data
.get('num_comments')),
198 'age_limit': age_limit
,
201 parsed_url
= urllib
.parse
.urlparse(video_url
)
203 # Check for embeds in text posts, or else raise to avoid recursing into the same reddit URL
204 if 'reddit.com' in parsed_url
.netloc
and f
'/{video_id}/' in parsed_url
.path
:
206 for media
in traverse_obj(data
, ('media_metadata', ...), expected_type
=dict):
207 if not media
.get('id') or media
.get('e') != 'RedditVideo':
210 if media
.get('hlsUrl'):
211 formats
.extend(self
._extract
_m
3u8_formats
(
212 unescapeHTML(media
['hlsUrl']), video_id
, 'mp4', m3u8_id
='hls', fatal
=False))
213 if media
.get('dashUrl'):
214 formats
.extend(self
._extract
_mpd
_formats
(
215 unescapeHTML(media
['dashUrl']), video_id
, mpd_id
='dash', fatal
=False))
219 'display_id': video_id
,
224 return self
.playlist_result(entries
, video_id
, info
.get('title'))
225 raise ExtractorError('No media found', expected
=True)
227 # Check if media is hosted on reddit:
228 reddit_video
= traverse_obj(data
, (
229 (None, ('crosspost_parent_list', ...)), ('secure_media', 'media'), 'reddit_video'), get_all
=False)
232 try_get(reddit_video
, lambda x
: unescapeHTML(x
[y
]))
233 for y
in ('dash_url', 'hls_url')
237 display_id
= video_id
238 video_id
= self
._search
_regex
(
239 r
'https?://v\.redd\.it/(?P<id>[^/?#&]+)', reddit_video
['fallback_url'],
240 'video_id', default
=display_id
)
242 dash_playlist_url
= playlist_urls
[0] or f
'https://v.redd.it/{video_id}/DASHPlaylist.mpd'
243 hls_playlist_url
= playlist_urls
[1] or f
'https://v.redd.it/{video_id}/HLSPlaylist.m3u8'
246 'url': unescapeHTML(reddit_video
['fallback_url']),
247 'height': int_or_none(reddit_video
.get('height')),
248 'width': int_or_none(reddit_video
.get('width')),
249 'tbr': int_or_none(reddit_video
.get('bitrate_kbps')),
253 'format_id': 'fallback',
254 'format_note': 'DASH video, mp4_dash',
256 formats
.extend(self
._extract
_m
3u8_formats
(
257 hls_playlist_url
, display_id
, 'mp4', m3u8_id
='hls', fatal
=False))
258 formats
.extend(self
._extract
_mpd
_formats
(
259 dash_playlist_url
, display_id
, mpd_id
='dash', fatal
=False))
264 'display_id': display_id
,
266 'duration': int_or_none(reddit_video
.get('duration')),
269 if parsed_url
.netloc
== 'v.redd.it':
270 self
.raise_no_formats('This video is processing', expected
=True, video_id
=video_id
)
273 'id': parsed_url
.path
.split('/')[1],
274 'display_id': video_id
,
277 # Not hosted on reddit, must continue extraction
280 'display_id': video_id
,
281 '_type': 'url_transparent',