forked from benwah/reddit_wallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit_wallpaper_getter.py
executable file
·187 lines (152 loc) · 5.02 KB
/
reddit_wallpaper_getter.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Thanks to Benoit C. Sirois for the original code
#https://github.com/benwah
try:
from urllib.request import urlopen
from urllib.error import HTTPError
except ImportError:
from urllib2 import urlopen
from urllib2 import HTTPError
import time
import sys
import subprocess
import os
import json
import re
import random
import argparse
import socket
import ctypes
#My preferred subreddit
REDDIT_URL = 'http://www.reddit.com/u/ChosenBeard/m/sfw.json?t=week&limit=100'
TIMEOUT = 5
#Highly original name for a wallpaper folder (I'm using the working directory)
DATA_DIR = "wallpaper"
MAX_ATTEMPTS = 5
SLEEP_SECONDS_AFTER_ATTEMPT = 2
def get_image(url):
"""
Makes a call to reddit and returns one post randomly from the page
specified in url.
"""
i = 0
while True:
if i == MAX_ATTEMPTS:
raise Exception('Sorry, can\'t reach reddit.')
try:
data = json.loads(
urlopen(url, timeout=TIMEOUT).read().decode('utf-8'))
break
except HTTPError as e:
# Too many requests, give reddit a break, try again.
print("JSON api throttled, attempt %s on %s" % (i, MAX_ATTEMPTS))
if getattr(e, 'code', None) == 429:
time.sleep(SLEEP_SECONDS_AFTER_ATTEMPT)
i += 1
except socket.timeout:
print("Timeout, attempt %s on %s" % (i, MAX_ATTEMPTS))
time.sleep(SLEEP_SECONDS_AFTER_ATTEMPT)
i += 1
candidates = data.get('data', {}).get('children', {})
#pick a random one
image = candidates[random.randrange(0, len(candidates))]['data']
#return the [URL,image name,post title]
return [
image['preview']['images'][0]['source']['url'],
'wallpaper_image.jpg',
image['title']
]
def save_image(url, file_path):
try:
os.remove(file_path)
except:
pass
f = open(file_path, 'wb')
i = 0
while True:
if i == MAX_ATTEMPTS:
f.close()
raise Exception('Sorry, can\'t reach imgur.')
try:
data = urlopen(url, timeout=TIMEOUT).read()
if len(data) > 0:
f.write(data)
else:
raise Exception('0 Bytes in download, exiting')
f.close()
break
except HTTPError:
time.sleep(1)
i += 1
except socket.timeout:
# Socket timeout, try again.
i += 1
def display_image(image_name,title):
cwd = os.getcwd()
image_path = os.path.join(cwd, image_name)
#THIS THING ADDS TITLES FOR CONTEXT
from PIL import ImageFont, ImageDraw, ImageEnhance, Image
source_img = Image.open(image_path)
width,height = source_img.size
draw = ImageDraw.Draw(source_img)
#RECTANGLE 100% of width and 7% of the height drawn on the top
draw.rectangle(((0, 00), (int(1*width), int(0.07*height))), fill="black")
#TEXT is 1% of height and starts 3% from the side
draw.text((int(0.03*width), int(0.01*height)), title, font=ImageFont.truetype("C:/Windows/fonts/Arial.ttf",int(0.03*height)))
source_img.save(image_path, "JPEG")
#This part sets the wallpaper
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, str(image_path), 3)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=('Use reddit for wallpapers'))
#CHANGED DEFAULT HERE
parser.add_argument(
'--destination',
type=str,
default=DATA_DIR,
help='Destination directory (default: %s)' % DATA_DIR,
)
parser.add_argument(
'--output-name',
type=str,
default=None,
help='Output filename (defaults to imgur name)',
)
parser.add_argument(
'--overwrite-existing',
type=str,
default='',
help=(
'Overwrite file if exists? (True / False), default is'
' False'),
)
parser.add_argument(
'--reddit-json-url',
type=str,
default=REDDIT_URL,
help='Specify a subreddit .json url. (default %s)' % REDDIT_URL,
)
parser.add_argument(
'--set-wallpaper',
type=str,
default='True',
help='Set wallpaper? (True / False), default is True',
)
args = parser.parse_args()
if not os.path.exists(args.destination) and args.destination == DATA_DIR:
os.mkdir(args.destination)
if not os.path.exists(args.destination):
raise Exception(
('Destination directory %s does not exist, or is '
'unreadable') % args.destination)
image = get_image(args.reddit_json_url)
if not image:
print("No image found")
sys.exit(1)
target_file_name = args.output_name or image[1]
file_path = os.path.join(args.destination, target_file_name)
save_image(image[0], file_path)
if args.set_wallpaper == 'True':
display_image(file_path,image[2])