forked from thisisppn/reddit-media-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.py
executable file
·150 lines (117 loc) · 4.65 KB
/
reddit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python3
import argparse
import os
import shutil
from concurrent.futures import ThreadPoolExecutor
import requests as r
from tqdm import tqdm
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-s', '--sort', default='top', help='top or hot')
parser.add_argument('-r', '--subreddit', help='name of the subreddit')
parser.add_argument('-u', '--user', help='user name')
parser.add_argument('-t', '--threads', type=int, default=1, help='parallel threads per page amount, default 1')
parser.add_argument('-i', '--images-only', action='store_true', help='Download only files with content-type images')
args = parser.parse_args()
if args.subreddit:
sub_reddit = args.subreddit
if args.sort == 'top':
url = 'https://www.reddit.com/r/{0}/top.json?sort=top&t=all'.format(sub_reddit)
elif args.sort == 'hot':
url = 'https://www.reddit.com/r/{0}/.json?'.format(sub_reddit)
else:
raise parser.error('Unsupported sorting mode')
elif args.user:
sub_reddit = args.user
url = 'https://www.reddit.com/user/{0}.json'.format(sub_reddit)
else:
raise parser.error('No user or subreddit name')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
}
exceptions = {}
downloads = []
def get_url(domain_name, name):
urls = {
'gfycat.com': 'https://gfycat.com/cajax/get/{0}',
'redgifs.com': 'https://api.redgifs.com/v1/gfycats/{}',
}
response = r.get(urls[domain_name].format(name), headers=headers)
if response.status_code == 200:
response_json = response.json()
mp4url = response_json['gfyItem']['mp4Url']
return mp4url
else:
return False
def http_download(img_url, folder_name, file_name, timeout):
if img_url in downloads:
return False
downloads.append(img_url)
file_suffix = os.path.splitext(img_url)[1]
filename = '{}{}{}{}'.format(folder_name, os.sep, file_name, file_suffix)
if os.path.exists(filename):
start_position = os.stat(filename).st_size
else:
start_position = 0
header = r.head(img_url, timeout=timeout)
total_size = int(header.headers.get('content-length'))
content_type = header.headers.get('content-type')
if content_type and args.images_only and (not content_type.startswith('image/') or content_type == 'image/gif' or img_url.endswith('.gif')):
return False
if start_position >= total_size-1:
return
headers = None
if start_position > 0:
headers = {'Range': f'{start_position}-{total_size}'}
resp = r.get(img_url, stream=True, timeout=timeout, headers=headers)
with open(filename, 'wb') as f, tqdm(
desc=img_url, total=total_size, unit='B', unit_scale=True, leave=False) as pbar:
for chunk in resp.iter_content(chunk_size=1024):
if chunk:
bytes = f.write(chunk)
pbar.update(bytes)
def download_media(img_url, file_name, source, folder_name):
try:
if not os.path.exists(folder_name):
os.makedirs(folder_name, exist_ok=True)
if source in ['gfycat.com', 'redgifs.com']:
name = img_url.split('/')[-1]
img_url = get_url(source, name)
elif source in ['i.imgur.com', 'i.redd.it']:
img_url = img_url.replace('.gifv', '.mp4')
else:
exceptions[source] = img_url
return
if img_url:
http_download(img_url, folder_name, file_name, 10)
except IOError as e:
print('??????', e)
except Exception as e:
print('?? ?', e)
def process_post(post):
try:
download_media(
post['data']['url'],
post['data']['title'].replace('/', '_'),
post['data']['domain'],
'downloads/{}'.format(sub_reddit))
except KeyError:
pass
def request_reddit(media_url, workers):
response = r.get(media_url, headers=headers, timeout=10)
if response.status_code == 200:
response_json = response.json()
next_page = response_json['data']['after']
posts = response_json['data']['children']
with ThreadPoolExecutor(max_workers=workers) as runner:
for post in posts:
runner.submit(process_post, post)
if next_page is not None:
if '?' in media_url:
media_url = media_url + '&after=' + next_page
else:
media_url = media_url + '?after=' + next_page
runner.submit(request_reddit, media_url, workers)
else:
print(response)
request_reddit(url, args.threads)
print(exceptions)