-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrawPaper.py
More file actions
330 lines (267 loc) · 11 KB
/
rawPaper.py
File metadata and controls
330 lines (267 loc) · 11 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -------- Imports and variable assignments --------- #
import os # internal file use and system calls
try:
import praw # reddit api wrapper. Without python it's just raw
except ImportError, e:
print "PRAW is not installed\nInstalling PRAW..."
os.system("sudo pip install praw")
import praw
try:
from PIL import Image, ImageDraw, ImageFont # used for checking image specs and for sub tagging
except ImportError, e:
print "pillow is not installed\nInstalling pillow..."
os.system("sudo pip install pillow")
from PIL import Image, ImageDraw, ImageFont
import urllib # used for downloading things and putting them places
import shutil # used for moving between directories
import time # used to pull time of file
from random import randint # used to get a random font
from traceback import print_exception # debugging
import platform # to check what os we are on
from subprocess import Popen, PIPE, call # used to run applescript
user_agent = "rawPaper 0.0.7 by /u/jbonzo200"
r = praw.Reddit(user_agent=user_agent)
dicPic = {}
goodPics = []
isWindows = platform.system() == "Windows"
workingDirectory = os.getcwd()
userRoot = '%USERPROFILE%' if isWindows else '~'
baseDirectory = os.path.expandvars(os.path.join(userRoot, 'Pictures', 'redditWallpaper'))
rawDirectory = os.path.join(baseDirectory, 'rawPics', '')
goodDirectory = os.path.join(baseDirectory, 'goodPics', '')
# pick the right directory
# for path in rawDirectoryList:
# if os.path.exists(os.path.expanduser(path)):
# rawDirectory = os.path.expanduser(path)
# for path in goodDirectoryList:
# if os.path.exists(os.path.expanduser(path)):
# goodDirectory = os.path.expanduser(path)
limit = 20
# --------- Class Definitions --------- #
class Subreddit():
def __init__(self):
self.sub = ""
def set(self, sub_input):
self.sub = sub_input
def get(self):
return self.sub
# --------- Function Definitions --------- #
def pull_from_leader(from_):
if from_:
from_ = from_.get().lower()
app = get_app()
sub = r.get_subreddit(app.sub)
submission_type = {
"day": sub.get_top_from_day(limit=limit),
"week": sub.get_top_from_week(limit=limit),
"hot": sub.get_hot(limit=limit),
"all": sub.get_top_from_all(limit=limit),
}
print from_, app.sub
if from_ in submission_type.keys():
print "ok"
pullFrom__(submission_type[from_], app.sub)
def pullFrom__(submissions, sub_input):
counter = 0
# get x submissions
submissions = submissions
# goes through each submission and adds the pictures to a dictionary
for x in submissions:
urlString = str(x.url)
# if the url points to a jpg
if urlString[len(urlString) - 3:] == "jpg":
# add to dictionary
try:
dicPic[str(x.title[:10]).replace("\"", "")] = urlString
except UnicodeEncodeError, e:
dicPic["Unknown Title"] = urlString
# for every string link in the picList
for title, link in dicPic.iteritems():
try:
# makes the string filename
title = title.replace("\\", "")
title = title.replace("/", "")
rawFile = rawDirectory + title + ".jpg"
if rawFile[len(goodDirectory) - 1:] not in os.listdir(goodDirectory):
# downloads the link file and puts it in the drop zone
urllib.urlretrieve(link, rawFile)
image = Image.open(rawFile) # makes into image to be evaluated
# if the image is good specs
if image.size[0] >= 1000 and image.size[1] >= 700:
shutil.copy(rawFile, goodDirectory)
# image.show()
# corner = int(raw_input("Do you want the tag in the top left corner?(0)\nOr the bottom left corner?(1)\n"))
placeTag(sub_input, rawFile, 0)
# collects a list of added pictures for debugging
goodPics.append(rawFile)
counter += 1
except IOError, e:
print rawFile[len(goodDirectory) - 1:]
print_exception(IOError, e, None)
# raise e
pulledPhrase = "picture pulled" if counter == 1 else "pictures pulled"
print "\t", counter, pulledPhrase
# parameter corner is the corner that the tag
# will be placed on. Either top left (0) or
# bottom left (1)
def placeTag(subreddit, imageFile, corner):
fontDirectory = "C:/Windows/Fonts/" if isWindows else "/Library/Fonts/"
# list of fonts I like
windowsFonts = [
"BRADHITC.ttf",
"CALIFR.ttf",
"GOTHIC.ttf",
"ChaparralPro-LightIt.ttf",
"ITCEDSCR.ttf",
"simfang.ttf",
"IMPRISHA.ttf",
"INFROMAN.tt"
]
macFonts = ["Bradley Hand Bold.ttf", "Apple Chancery.ttf"]
# get random font from fonts list
# but before I do this I need to fix the size difference between each font
# font = fonts[randint(0, len(fonts) - 1)]
font = windowsFonts[0] if isWindows else macFonts[randint(0, len(macFonts) - 1)]
# for possFont in fonts:
# #print fontDirectory + possFont
# #print os.path.exists(fontDirectory + font)
# if os.path.exists(fontDirectory + font):
# font = possFont
# #print font
# break
# font = fonts[2]
# setting up image and its attributes
image = Image.open(imageFile).convert('RGBA')
imageWidth = image.size[0]
imageHeight = image.size[1]
textBase = Image.new('RGBA', image.size, (255, 255, 255, 0))
# text base for the tag
Image.new('RGBA', image.size, (255, 255, 255, 0))
# make the font and the draw object
try:
fnt = ImageFont.truetype(fontDirectory + font, size=200)
except IOError, e:
print e
fnt = ImageFont.truetype(fontDirectory + font, size=200)
fntHeight = fnt.getsize(subreddit)[1]
cornerPlacement = [(imageWidth / 48, -1), (imageWidth / 48, 47 * imageHeight / 48 - fntHeight)]
draw = ImageDraw.Draw(textBase)
draw.text(cornerPlacement[corner], "/r/" + subreddit, font=fnt, fill=(255, 255, 255, 255))
out = Image.alpha_composite(image, textBase)
out.save(goodDirectory + imageFile[len(goodDirectory) - 1:])
def dateSorted(path):
mtime = lambda f: os.stat(os.path.join(path, f)).st_mtime
return list(sorted(os.listdir(path), key=mtime))
def cleaner():
sortedPics = dateSorted(goodDirectory)
counter = 0
while (len(sortedPics) > 15):
os.remove(goodDirectory + "/" + sortedPics[counter])
sortedPics.remove(sortedPics[counter])
counter = counter + 1
def setUp():
os.system("mkdir " + goodDirectory[:len(goodDirectory) - len("goodPics/")])
print "mkdir " + goodDirectory[:len(goodDirectory) - len("goodPics/")]
os.system("mkdir " + goodDirectory)
os.system("mkdir " + rawDirectory)
if isWindows:
print "****************************************"
print "I am not perfect, my creators didn't give me the power to change your wallpaper settings automatically, so you have to do it."
print "(Windows 10) Open settings -> Personalization -> Background"
print "\tSet background to 'Slideshow' and choose album ('" + goodDirectory + "') for your slideshow"
print "If you're not on Windows 10 yet then hurry up and upgrade and stop being so paranoid"
print "****************************************"
else:
call(["osascript", workingDirectory + "/SystemPref.scpt"])
# --------- Runner --------- #
def runner():
clear = "cls" if isWindows else "clear"
clearScreen = lambda: os.system(clear)
clearScreen()
if not os.path.exists(goodDirectory):
setUp()
if "y" == str(raw_input("Do you want to clean the folder?(y/n)")):
cleaner()
pulling = True
while pulling:
subInput = str(raw_input("What subreddit do you want?(don't include /r/)"))
sub = r.get_subreddit(subInput)
submissionType = {
"day": sub.get_top_from_day(limit=limit),
"week": sub.get_top_from_week(limit=limit),
"hot": sub.get_hot(limit=limit),
"all": sub.get_top_from_all(limit=limit)
}
if "y" == str(raw_input("Top from day?(y/n)")):
pullFrom__(submissionType["day"], subInput)
if "y" == str(raw_input("Top from week?(y/n)")):
pullFrom__(submissionType["week"], subInput)
if "y" == str(raw_input("Top from all?(y/n)")):
pullFrom__(submissionType["all"], subInput)
if "y" == str(raw_input("From Hot?(y/n)")):
pullFrom__(submissionType["hot"], subInput)
if not "y" == str(raw_input("Do you want to pull from another subreddit?(y/n)")):
pulling = False
runner()
from gui import *
"""
DEBUG:
subreddit=pics, top from all takes forever
subreddit= funny, from hot throws IOError: [Errno 22] invalid mode ('wb')
dditWallpaper/rawPics/CNN\'s "ISI.jpg'
"""
"""
TODO:
Make little algorithim to adjust size depending on font
Possible solutions: use .getsize(text) to find the width and always make the tag
a certain width in proportion to the width of the picture
"""
"""
Make it more entertaining
"""
"""
Place proportional watermark on image that labels the source subreddit
example:
<bottomRight>
"/r/woahdude"
</bottomRight>
Subtasks:
Create cool dynamic graphic for watermark
Place it proportionally on corner of image
Use PIL/Pillow to place watermark on *.jpg
Find font library
"""
"""
Make two versions: stream and favorites.
Stream changes it constantly throughout the month long process
Favorites are categorized pictures that are favorited throughout a stream
"""
"""
have script talk to phone and update phone background
"""
"""
make the script go through a subreddit and find popular
pictures on random topics. Example: Kanye West, and it would find
pictures of kanye as the wallpaper theme for the get_top_from_day
"""
"""
For the watermark in pick a corner for the best placement when i download each picture.
So when each picture is moved to ghe good directory it is displayed and prompts for the
best corner so as not to cover important details of the picture
I've played with this idea but it seems to be a bit annoying. For now I'll keep it on the top left corner untill
I find a problem with this
"""
"""
One thing I've noticed is that all my processing and downloading is taking place in one move
For a more fluid run I should first download every into the rawDirectory and then transfer
the good pictures into the goodDirectory and then once every picture is in I add the tag
all at the end with some kind of exit status
It would be cool to figure out how to download and tag at the same time
lol found it: http://stackoverflow.com/questions/18864859/python-executing-multiple-functions-simultaneously
"""
"""
make a script so that it changes the settings for mac to have the screensaver and/or desktop
"""
"""
Add user artists tag
"""