]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/reddit.py
Readme changes
[yt-dlp.git] / youtube_dlc / extractor / reddit.py
CommitLineData
0c43a481
S
1from __future__ import unicode_literals
2
9bb2c767
S
3import re
4
0c43a481
S
5from .common import InfoExtractor
6from ..utils import (
7 ExtractorError,
8 int_or_none,
9 float_or_none,
97abf05a 10 url_or_none,
0c43a481
S
11)
12
13
14class RedditIE(InfoExtractor):
15 _VALID_URL = r'https?://v\.redd\.it/(?P<id>[^/?#&]+)'
16 _TEST = {
17 # from https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/
18 'url': 'https://v.redd.it/zv89llsvexdz',
8315ee6c 19 'md5': '0a070c53eba7ec4534d95a5a1259e253',
0c43a481
S
20 'info_dict': {
21 'id': 'zv89llsvexdz',
22 'ext': 'mp4',
23 'title': 'zv89llsvexdz',
24 },
25 'params': {
26 'format': 'bestvideo',
27 },
28 }
29
30 def _real_extract(self, url):
31 video_id = self._match_id(url)
32
33 formats = self._extract_m3u8_formats(
34 'https://v.redd.it/%s/HLSPlaylist.m3u8' % video_id, video_id,
35 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
36
37 formats.extend(self._extract_mpd_formats(
38 'https://v.redd.it/%s/DASHPlaylist.mpd' % video_id, video_id,
39 mpd_id='dash', fatal=False))
40
07bafb4a 41 self._sort_formats(formats, ('height', 'width'))
665f42d8 42
0c43a481
S
43 return {
44 'id': video_id,
45 'title': video_id,
46 'formats': formats,
47 }
48
49
50class RedditRIE(InfoExtractor):
dbd5c502 51 _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?reddit\.com/r/[^/]+/comments/(?P<id>[^/?#&]+))'
0c43a481
S
52 _TESTS = [{
53 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/',
54 'info_dict': {
55 'id': 'zv89llsvexdz',
56 'ext': 'mp4',
57 'title': 'That small heart attack.',
58 'thumbnail': r're:^https?://.*\.jpg$',
59 'timestamp': 1501941939,
60 'upload_date': '20170805',
61 'uploader': 'Antw87',
62 'like_count': int,
63 'dislike_count': int,
64 'comment_count': int,
65 'age_limit': 0,
66 },
67 'params': {
68 'format': 'bestvideo',
69 'skip_download': True,
70 },
71 }, {
72 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj',
73 'only_matching': True,
74 }, {
75 # imgur
76 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
77 'only_matching': True,
12b0d4e0
M
78 }, {
79 # imgur @ old reddit
80 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
81 'only_matching': True,
0c43a481
S
82 }, {
83 # streamable
84 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/',
85 'only_matching': True,
86 }, {
87 # youtube
88 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/',
89 'only_matching': True,
dbd5c502 90 }, {
91 # reddit video @ nm reddit
92 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/',
93 'only_matching': True,
0c43a481
S
94 }]
95
96 def _real_extract(self, url):
9bb2c767
S
97 mobj = re.match(self._VALID_URL, url)
98 url, video_id = mobj.group('url', 'id')
99
0c43a481
S
100 video_id = self._match_id(url)
101
102 data = self._download_json(
9bb2c767 103 url + '/.json', video_id)[0]['data']['children'][0]['data']
0c43a481
S
104
105 video_url = data['url']
106
107 # Avoid recursing into the same reddit URL
108 if 'reddit.com/' in video_url and '/%s/' % video_id in video_url:
109 raise ExtractorError('No media found', expected=True)
110
111 over_18 = data.get('over_18')
112 if over_18 is True:
113 age_limit = 18
114 elif over_18 is False:
115 age_limit = 0
116 else:
117 age_limit = None
118
119 return {
120 '_type': 'url_transparent',
121 'url': video_url,
122 'title': data.get('title'),
97abf05a 123 'thumbnail': url_or_none(data.get('thumbnail')),
0c43a481
S
124 'timestamp': float_or_none(data.get('created_utc')),
125 'uploader': data.get('author'),
126 'like_count': int_or_none(data.get('ups')),
127 'dislike_count': int_or_none(data.get('downs')),
128 'comment_count': int_or_none(data.get('num_comments')),
129 'age_limit': age_limit,
130 }