/
add_images.py
57 lines (47 loc) · 1.53 KB
/
add_images.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
import praw
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from urllib.parse import quote
import sys
import os
storage_directory = "/Users/Destaq/Desktop/Background_Images"
os.chdir(storage_directory) # change to your blank directory
print("Navigated to directory...")
# clear directory each day to prevent duplicates
filelist = [f for f in os.listdir(storage_directory)]
for f in filelist:
os.remove(f)
print("Removed previous files...\n")
SCRIPT = "HIDDEN_SCRIPT" # check README
SECRET = "HIDDEN_SECRET" # check README
NUM_IMAGES = 100
reddit = praw.Reddit(
client_id=SCRIPT,
client_secret=SECRET,
user_agent="ImageBot",
username="MY_USERNAME", # your username
password="HIDDEN_PASSWORD", # your password
)
subreddit = reddit.subreddit("wallpaper")
images = []
for submission in subreddit.hot(limit=NUM_IMAGES):
images.append(submission.url)
sys.stdout.flush()
for i, image in enumerate(images):
sys.stdout.write(f"\rWriting image {i+1} of {len(images)}.")
try:
img_data = urlopen(image).read()
fname = f"image_{i+1}.{image.split('.')[-1]}"
with open(fname, "wb") as handler:
handler.write(img_data)
# delete images under 200 kB; too fuzzy
if os.stat(fname).st_size < 200000:
os.remove(fname)
except HTTPError:
print("\nHTTP Error 500!")
except FileNotFoundError:
print(f"\n{image} is an invalid link!")
except WindowsError:
pass
sys.stdout.write("\n")