forked from errorundefined/getspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getspace.py
executable file
·270 lines (196 loc) · 6.79 KB
/
getspace.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
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
#!/usr/bin/env python
import os
# import ssl
import json
import urllib
# ENV VARIABLE
home = os.getenv('HOME')
# GET OS VARIABLE
from sys import platform
# CONDITIONALLY SET OS SPECIFIC ENV
if platform == 'linux' or platform == 'linux2':
print 'getspace is NOT YET running as getspace_linux'
quit() # exit until draft is no draft any longer
import getspace_linux as getspace
path = home + '/GetSpace'
elif platform == 'darwin':
print 'getspace is running as getspace_osx'
import getspace_osx as getspace
path = home + '/Pictures/GetSpace'
else:
print 'exiting (no compatible os found)'
quit()
# DEF GET COLOR BASED ON https://gist.github.com/zollinger/1722663
def getcolor(img):
img = img.resize((150, 150))
result = img.convert('P', palette=Image.ADAPTIVE, colors=1)
result.putalpha(255)
colors = result.getcolors(22500)
color = colors[0][1]
return color
# OPEN URL ON SRV - ADD YOUR API KEY HERE
url = 'https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY'
try:
response = urllib.urlopen(url)
# LOAD JSON DATA FROM OBJECT
jsonstuff = json.load(response)
except IOError, e:
print 'Using CURL fallback.'
import subprocess
process = subprocess.Popen(['curl', url], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
response = stdout
# LOAD JSON DATA FROM STRING
jsonstuff = json.loads(response)
# CHECK IF JSON DATA EXISTS
if 'jsonstuff' not in locals():
print 'Failed! APOD info could not be downloaded.'
quit()
else:
print 'APOD info downloaded.'
media_type = (jsonstuff['media_type'])
# CHECK IF AN VIDEO OR IMNAGE IS AVAILABLE
if media_type == 'video':
getspace.notify("Space traveling now..", "There is an APOD video today.", "Watch it at apod.nasa.gov!", "success")
elif media_type == 'image':
# IF 'PICTURES/GETSPACE' DOES NOT EXISTS, CREATE IT
if not os.path.exists(path):
os.makedirs(path)
# EXTRACT VARIABLES FROM JSON DATA
date = (jsonstuff['date'])
title = (jsonstuff['title'])
explanation = (jsonstuff['explanation'])
hdurl = (jsonstuff['hdurl'])
# (maybe there's not always a HQ version? if so, add fallback for 'url')
# DEFINE FILE PATH VARIABLES
imgname = os.path.basename(hdurl)
# imgname = 'testimage2.jpg' # debugging
filename, file_extension = os.path.splitext(imgname)
savein = path + '/' + imgname
saveout = path + '/' + filename + '_info' + file_extension
# CHECK IF IMAGE IS NEW
lastspace = path + '/lastspace.txt'
if os.path.exists(lastspace):
lastimage = open(lastspace, 'r')
print 'lastimg is: ' + lastimage.read()
print 'imgname is: ' + imgname
if lastimage.read() == imgname:
file = open(lastspace, 'w')
file.write(imgname)
file.close()
print 'exiting (there\'s no new image available)'
quit()
else:
file = open(lastspace, 'w')
file.write(imgname)
file.close()
# DOWNLOAD THE HQ IMAGE FROM hdurl TO savein
urllib.urlretrieve(hdurl, savein)
print 'APOD image downloaded.'
# CHECK IF PIL/PILLOW IS INSTALLED
try:
import PIL
except ImportError, e:
getspace.notify("Please read the source code.", "This thing requires a spaceship.", "So, no space info on the desktop for you.", "error")
pass
else:
# GET THE SCREEN SIZE
(vw, vh) = getspace.getscreensize()
# CALCULATE RATIO OF THE SCREEN
ratioscreen = float(vw) / vh
print("Screenratio is %s (%sx%spx)" % (ratioscreen, vw, vh))
# MANIPULATE THE IMAGE!
from PIL import Image
from PIL import ImageOps
from PIL import ImageDraw
# from PIL import ImageFilter
Image.MAX_IMAGE_PIXELS = 1000000000
from math import floor
# OPEN IMG + INVERSION IMAGE
img = Image.open(savein)
inv = ImageOps.invert(img)
# GET IMG + INVERSION COLOR
imgcolor = getcolor(img)
invcolor = getcolor(inv)
# ADDING TRANSPARENCY TO IMGCOLOR
lst = list(imgcolor)
lst[3] = 180
imgcolor = tuple(lst)
# SET INVCOLOR TO EITHER BLACK OR WHITE
lst = list(invcolor)
if lst[0] + lst[1] + lst[2] > 382:
lst[0] = 255
lst[1] = 255
lst[2] = 255
else:
lst[0] = 0
lst[1] = 0
lst[2] = 0
invcolor = tuple(lst)
# OPEN DOWNLOADED IMAGE
img = img.convert('RGBA')
# GET IMAGE SIZE
(width, height) = img.size
# CALCULATE RATIO OF THE IMAGE
ratioimage = float(width) / height
print("Imageratio is %s (%sx%spx)" % (ratioimage, width, height))
# CUT THE IMAGE TO THE SCREEN RATIO
if ratioscreen > ratioimage:
newheight = floor(width / ratioscreen)
img = ImageOps.fit(img, (width,newheight), centering = (0.5,0.5))
(width, height) = img.size
print 'Image cut.'
elif ratioscreen < ratioimage:
newwidth = floor(height * ratioscreen)
img = ImageOps.fit(img, (newwidth,height), centering = (0.5,0.5))
(width, height) = img.size
print 'Image cut.'
# UPSCALE IMAGE TO DISPLAY SIZE
if width < vw:
img = img.resize((int(vw), int(vh)), Image.BICUBIC)
width = vw
height = vh
print 'Image upscaled.'
# BEGIN PILLOW'S DRAWING
overlay = Image.new('RGBA', img.size)
# draw = ImageDraw.Draw(img)
draw = ImageDraw.Draw(overlay)
# GET FONT VARIABLES
(fsizehead, fsizetext, wrapped, headfont, textfont) = getspace.getfontvars(height, explanation)
# CALCULATE THE HEADING SIZE
whead, hhead = draw.textsize(title,font=headfont)
# SET POSITIONG VARS
golden = hhead * 1.618
silver = golden - hhead
# CALCULATE THE TEXT SIZE AND DEDUCE POSITIONING VARS FOR NEXT 2 STEPS
wtext, htext = draw.textsize(wrapped,font=textfont)
x = (2 * golden - silver)
y = (height - htext - golden - silver)
x2 = (x + wtext + silver)
y2 = (y + htext + silver)
# DRAW A RECTANGULAR BACKGROUND SHAPE ONTO THE IMAGE
# draw.rectangle([x - silver, y - silver, x2, y2 ],fill=(255,255,255,188))
draw.rectangle([x - silver, y - silver, x2, y2 ],fill=imgcolor)
# DRAW THE 'EXPLANATION'-TEXT ONTO THE BACKGROUND
draw.text((x + 1, y + 1),wrapped,invcolor,font=textfont,align="left")
# draw.text((x, y),wrapped,(0,0,0),font=textfont,align="left")
# DRAW THE HEADING
draw.text((x, (y - 2 * silver - hhead)),title,invcolor,font=headfont)
# draw.text((golden, golden),title,(255,255,255),font=headfont)
# CHANGE LOGINIMAGE
# (add stuff for changing loginimage)
# SET THE OVERLAY ON TOP OF THE IMG
img = Image.alpha_composite(img, overlay)
print 'Text written.'
# WRITE THE IMAGE TO saveout
img.save(saveout)
print 'Image saved.'
# SET savein TO saveout SO THE MODIFIED IMAGE IS SET AS BG IN THE NEXT STEP
savein = saveout
# SETTING THE DESKTOP BG
getspace.dosetbackground(savein)
print 'Background set.'
# SETTING
getspace.notify("Boldly go where only aliens have gone before", title.replace("'", ""), "Background image has been set.", "success")
else:
getspace.notify("Space, the final frontier!", "Apparently, there is no new image today.", "Try investing in space travel or something.", "error")