]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/reddit.py
[reddit] Workaround for 429 by redirecting to old.reddit.com
[yt-dlp.git] / yt_dlp / extractor / reddit.py
CommitLineData
0c43a481
S
1from __future__ import unicode_literals
2
9bb2c767 3
0c43a481
S
4from .common import InfoExtractor
5from ..utils import (
6 ExtractorError,
7 int_or_none,
8 float_or_none,
29f7c58a 9 try_get,
10 unescapeHTML,
97abf05a 11 url_or_none,
0c43a481
S
12)
13
14
15class RedditIE(InfoExtractor):
16 _VALID_URL = r'https?://v\.redd\.it/(?P<id>[^/?#&]+)'
17 _TEST = {
18 # from https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/
19 'url': 'https://v.redd.it/zv89llsvexdz',
8315ee6c 20 'md5': '0a070c53eba7ec4534d95a5a1259e253',
0c43a481
S
21 'info_dict': {
22 'id': 'zv89llsvexdz',
23 'ext': 'mp4',
24 'title': 'zv89llsvexdz',
25 },
26 'params': {
27 'format': 'bestvideo',
28 },
29 }
30
31 def _real_extract(self, url):
32 video_id = self._match_id(url)
33
34 formats = self._extract_m3u8_formats(
35 'https://v.redd.it/%s/HLSPlaylist.m3u8' % video_id, video_id,
36 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
37
38 formats.extend(self._extract_mpd_formats(
39 'https://v.redd.it/%s/DASHPlaylist.mpd' % video_id, video_id,
40 mpd_id='dash', fatal=False))
41
54f37eea 42 self._sort_formats(formats)
665f42d8 43
0c43a481
S
44 return {
45 'id': video_id,
46 'title': video_id,
47 'formats': formats,
48 }
49
50
51class RedditRIE(InfoExtractor):
a76e2e0f 52 _VALID_URL = r'https?://(?:[^/]+\.)?reddit\.com/r/(?P<slug>[^/]+/comments/(?P<id>[^/?#&]+))'
0c43a481
S
53 _TESTS = [{
54 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/',
55 'info_dict': {
56 'id': 'zv89llsvexdz',
57 'ext': 'mp4',
58 'title': 'That small heart attack.',
29f7c58a 59 'thumbnail': r're:^https?://.*\.(?:jpg|png)',
60 'thumbnails': 'count:4',
0c43a481
S
61 'timestamp': 1501941939,
62 'upload_date': '20170805',
63 'uploader': 'Antw87',
29f7c58a 64 'duration': 12,
0c43a481
S
65 'like_count': int,
66 'dislike_count': int,
67 'comment_count': int,
68 'age_limit': 0,
69 },
70 'params': {
71 'format': 'bestvideo',
72 'skip_download': True,
73 },
74 }, {
75 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj',
76 'only_matching': True,
77 }, {
78 # imgur
79 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
80 'only_matching': True,
12b0d4e0
M
81 }, {
82 # imgur @ old reddit
83 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
84 'only_matching': True,
0c43a481
S
85 }, {
86 # streamable
87 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/',
88 'only_matching': True,
89 }, {
90 # youtube
91 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/',
92 'only_matching': True,
dbd5c502 93 }, {
94 # reddit video @ nm reddit
95 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/',
96 'only_matching': True,
0c43a481
S
97 }]
98
99 def _real_extract(self, url):
a76e2e0f 100 slug, video_id = self._match_valid_url(url).group('slug', 'id')
0c43a481 101
f0e53663 102 self._set_cookie('reddit.com', '_options', '%7B%22pref_quarantine_optin%22%3A%20true%7D')
0c43a481 103 data = self._download_json(
a76e2e0f 104 f'https://old.reddit.com/r/{slug}/.json', video_id)[0]['data']['children'][0]['data']
0c43a481
S
105
106 video_url = data['url']
107
108 # Avoid recursing into the same reddit URL
109 if 'reddit.com/' in video_url and '/%s/' % video_id in video_url:
110 raise ExtractorError('No media found', expected=True)
111
112 over_18 = data.get('over_18')
113 if over_18 is True:
114 age_limit = 18
115 elif over_18 is False:
116 age_limit = 0
117 else:
118 age_limit = None
119
29f7c58a 120 thumbnails = []
121
122 def add_thumbnail(src):
123 if not isinstance(src, dict):
124 return
125 thumbnail_url = url_or_none(src.get('url'))
126 if not thumbnail_url:
127 return
128 thumbnails.append({
129 'url': unescapeHTML(thumbnail_url),
130 'width': int_or_none(src.get('width')),
131 'height': int_or_none(src.get('height')),
132 })
133
134 for image in try_get(data, lambda x: x['preview']['images']) or []:
135 if not isinstance(image, dict):
136 continue
137 add_thumbnail(image.get('source'))
138 resolutions = image.get('resolutions')
139 if isinstance(resolutions, list):
140 for resolution in resolutions:
141 add_thumbnail(resolution)
142
0c43a481
S
143 return {
144 '_type': 'url_transparent',
145 'url': video_url,
146 'title': data.get('title'),
29f7c58a 147 'thumbnails': thumbnails,
0c43a481
S
148 'timestamp': float_or_none(data.get('created_utc')),
149 'uploader': data.get('author'),
29f7c58a 150 'duration': int_or_none(try_get(
151 data,
152 (lambda x: x['media']['reddit_video']['duration'],
153 lambda x: x['secure_media']['reddit_video']['duration']))),
0c43a481
S
154 'like_count': int_or_none(data.get('ups')),
155 'dislike_count': int_or_none(data.get('downs')),
156 'comment_count': int_or_none(data.get('num_comments')),
157 'age_limit': age_limit,
158 }