forked from randomdrake/nasa-apod-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nasa_apod_desktop.py
executable file
·547 lines (460 loc) · 19 KB
/
nasa_apod_desktop.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env python3
#
# Copyright (c) 2012 David Drake
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# nasa_apod_desktop.py
# https://github.com/randomdrake/nasa-apod-desktop
#
# Written/Modified by David Drake
# http://randomdrake.com
# http://twitter.com/randomdrake
#
#
# Tested on Ubuntu 12.04
#
#
# DESCRIPTION
# 1) Grabs your current download path
# 2) Downloads the latest image of the day from NASA (http://apod.nasa.gov/apod/)
# 3) Determines your desktop resolution, or uses the set default.
# 4) Resizes the image to the given resolution.
# 5) Sets the image as your desktop.
# 6) Adds image to XML file used to scroll through desktop background images.
#
# It's not very exciting to scroll through a single image, so it will attempt to download
# additional images (default: 10) to seed your list of images.
#
#
# INSTALLATION
# Place the file wherever you like and chmod +x it to make it executable
# Ensure you have Python installed (default for Ubuntu) and the PIL and lxml packages:
# pip install -f requirements.txt or sudo apt-get install python-imaging python-lxml
#
#
# RUN AT STARTUP
# To have this run whenever you startup your computer, perform the following steps:
# 1) Click on the settings button (cog in top right)
# 2) Select "Startup Applications..."
# 3) Click the "Add" button
# 4) Enter whatever Name and Comment you like with the following Command:
# python /path/to/nasa_apod_desktop.py
# 5) Click on the "Add" button
#
# DEFAULTS
# While the script will detect as much as possible and has safe defaults, you may want to set your own.
#
# DOWNLOAD_PATH - where you want the file to be downloaded. Will be auto-detected if not set.
# CUSTOM_FOLDER - if we detect your download folder, this will be the target folder in there.
# RESOUTION_TYPE -
# 'stretch': single monitor or the combined resolution of your available monitors
# 'largest': largest resolution of your available monitors
# 'default': use the default resolution that is set
# RESOLUTION_X - horizontal resolution if RESOLUTION_TYPE is not default or cannot be
# automatically determined
# RESOLUTION_Y - vertical resolution if RESOLUTION_TYPE is not default or cannot be
# automatically determined
# RESIZE_TYPE -
# 'none': don't resize the image
# 'stretch': stretch and scale image to resolution ignoring the aspect ratio
# 'scale': scale image maintaining aspect ratio
# PICTURE_OPTIONS - set the gnome picture-options setting
# 'reset': reset the options
# 'centered': center and fit the full image
# 'zoom': zoom image to fit the full screen
# https://askubuntu.com/a/914760 for a full list
# NASA_APOD_SITE - location of the current picture of the day
# IMAGE_SCROLL - if true, will write an XML file to make the images scroll
# IMAGE_DURATION - if IMAGE_SCROLL is enabled, this is the duration each will stay in seconds
# SEED_IMAGES - if > 0, it will download previous images as well to seed the list of images
# SHOW_DEBUG - print useful debugging information or statuses
from datetime import datetime, timedelta
from lxml import etree
from sys import exit
from sys import stdout
from PIL import Image
from PIL import ImageFile
from PIL import ImageOps
import argparse
import glob
import random
import logging
import os
import pathlib
import re
import urllib.request
import subprocess
from gi.repository import GLib
NASA_APOD_SITE = 'http://apod.nasa.gov/apod/'
DOWNLOAD_PATH = '/tmp/backgrounds/'
CUSTOM_FOLDER = 'nasa-apod-backgrounds'
RESOLUTION_TYPE = 'default'
RESOLUTION_X = 1920
RESOLUTION_Y = 1080
RESIZE_TYPE = 'scaled'
PICTURE_OPTIONS = 'centered'
IMAGE_SCROLL = True
IMAGE_DURATION = 1200
SEED_IMAGES = 10
SHOW_DEBUG = True
# Use XRandR to grab the desktop resolution. If the scaling method is set to 'largest',
# we will attempt to grab it from the largest connected device. If the scaling method
# is set to 'stretch' we will grab it from the current value. Default will simply use
# what was set for the default resolutions.
def find_resolution():
logging.info("RESOLUTION_TYPE:" + RESOLUTION_TYPE)
if RESOLUTION_TYPE == 'default':
logging.info(
"Using default resolution of {}x{}".format(
RESOLUTION_X, RESOLUTION_Y))
return RESOLUTION_X, RESOLUTION_Y
res_x = 0
res_y = 0
logging.info("Attempting to determine the current resolution.")
if RESOLUTION_TYPE == 'largest':
regex_search = 'connected'
else:
regex_search = 'current'
p1 = subprocess.Popen(["xrandr"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["grep", regex_search],
stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output = str(p2.communicate()[0])
if RESOLUTION_TYPE == 'largest':
# We are going to go through the connected devices and get the X/Y from
# the largest
matches = re.finditer(" connected ([0-9]+)x([0-9]+)+", output)
if matches:
largest = 0
for match in matches:
if int(match.group(1)) * int(match.group(2)) > largest:
res_x = match.group(1)
res_y = match.group(2)
else:
logging.warning("Could not determine largest screen resolution.")
else:
reg = re.search(".* current (.*?) x (.*?),.*", output)
if reg:
res_x = reg.group(1)
res_y = reg.group(2)
else:
logging.warning("Could not determine current screen resolution.")
# If we couldn't find anything automatically use what was set for the
# defaults
if res_x == 0 or res_y == 0:
res_x = RESOLUTION_X
res_y = RESOLUTION_Y
logging.warning(
"Could not determine resolution automatically. Using defaults.")
logging.warning("Using detected resolution of {}x{}".format(res_x, res_y))
return int(res_x), int(res_y)
# Uses GLib to find the localized "Downloads" folder
# See:
# http://askubuntu.com/questions/137896/how-to-get-the-user-downloads-folder-location-with-python
def set_download_folder():
if DOWNLOAD_PATH:
logging.info("Using default path for downloads:" + DOWNLOAD_PATH)
return DOWNLOAD_PATH
downloads_dir = GLib.get_user_special_dir(
GLib.UserDirectory.DIRECTORY_DOWNLOAD)
if downloads_dir:
# Add any custom folder
new_path = os.path.join(downloads_dir, CUSTOM_FOLDER)
logging.info("Using automatically detected path:" + new_path)
return new_path
raise RuntimeError("Unable to determine download folder")
# Download HTML of the site
def download_site(url):
logging.info("Downloading contents of the site to find the image name")
req = urllib.request.Request(url)
try:
reply = urllib.request.urlopen(req).read()
except urllib.error.HTTPError as error:
logging.warning("Error downloading" + url + "-" + str(error.code))
reply = "Error: " + str(error.code)
return reply
# Finds the image URL and saves it
def get_image(text):
logging.info("Grabbing the image URL")
file_url, filename, file_size = get_image_info('a href', text)
# If file_url is None, the today's picture might be a video
if file_url is None:
return None
logging.info("Found name of image:" + filename)
save_to = os.path.join(
DOWNLOAD_PATH,
os.path.splitext(filename)[0] +
'.png')
if not os.path.isfile(save_to):
# If the response body is less than 500 bytes, something went wrong
if file_size < 500:
logging.warning(
"Response less than 500 bytes, probably an error\nAttempting to just grab image source")
file_url, filename, file_size = get_image_info('img src', text)
# If file_url is None, the today's picture might be a video
if file_url is None:
return None
logging.info("Found name of image:" + filename)
if file_size < 500:
# Give up
logging.warning("Could not find image to download")
exit()
logging.info("Retrieving image")
urllib.request.urlretrieve(
file_url, save_to, print_download_status)
logging.info("\nDone downloading " + human_readable_size(file_size))
else:
logging.info("File exists, moving on")
return save_to
# Resizes the image to the provided dimensions
def resize_image(filename):
logging.info("RESIZE_TYPE:" + RESIZE_TYPE)
if RESIZE_TYPE == 'none':
logging.info("Image resize skipped")
return
logging.info("Opening local image:" + filename)
image = Image.open(filename)
current_x, current_y = image.size
if (current_x, current_y) == (RESOLUTION_X, RESOLUTION_Y):
logging.info("Images are currently equal in size. No need to scale.")
else:
if RESIZE_TYPE == 'stretch':
logging.info(
"stretch: resizing the image from %sx%s to %sx%s",
image.size[0],
image.size[1],
RESOLUTION_X,
RESOLUTION_Y)
image = image.resize((RESOLUTION_X, RESOLUTION_Y), Image.ANTIALIAS)
else:
logging.info(
"scale: resizing the image from %sx%s to fit inside %sx%s maintaining aspect ratio",
image.size[0],
image.size[1],
RESOLUTION_X,
RESOLUTION_Y)
image = ImageOps.contain(image, (RESOLUTION_X, RESOLUTION_Y))
logging.info("Saving the image to" + filename)
fhandle = open(filename, 'wb')
image.save(fhandle, 'PNG')
# Sets the new image as the wallpaper
def set_gnome_wallpaper(file_path):
logging.info("PICTURE_OPTIONS:" + PICTURE_OPTIONS)
if PICTURE_OPTIONS == 'reset':
command = "gsettings reset org.gnome.desktop.background picture-options"
else:
command = "gsettings set org.gnome.desktop.background picture-options " + PICTURE_OPTIONS
status, output = subprocess.getstatusoutput(command)
logging.info(command + ":" + str(status) + ":" + output)
logging.info("Setting the image" + file_path)
command = "gsettings set org.gnome.desktop.background picture-uri file://" + file_path
status, output = subprocess.getstatusoutput(command)
logging.info(command + ":" + str(status) + ":" + output)
return status
def print_download_status(block_count, block_size, total_size):
written_size = human_readable_size(block_count * block_size)
total_size = human_readable_size(total_size)
# Adding space padding at the end to ensure we overwrite the whole line
stdout.write("\r%s bytes of %s " % (written_size, total_size))
stdout.flush()
def human_readable_size(number_bytes):
for x in ['bytes', 'KB', 'MB']:
if number_bytes < 1024.0:
return "%3.2f%s" % (number_bytes, x)
number_bytes /= 1024.0
# Creates the necessary XML so background images will scroll through
def create_desktop_background_scoll(filename):
if not IMAGE_SCROLL:
return filename
logging.info("Creating XML file for desktop background switching.")
filename = DOWNLOAD_PATH + '/nasa_apod_desktop_backgrounds.xml'
# Create our base, background element
background = etree.Element("background")
# Grab our PNGs we have downloaded
images = glob.glob(DOWNLOAD_PATH + "/*.png")
num_images = len(images)
if num_images < SEED_IMAGES:
# Let's seed some images
# Start with yesterday and continue going back until we have enough
logging.info("Downloading some seed images as well")
days_back = 0
seed_images_left = SEED_IMAGES
while seed_images_left > 0:
days_back += 1
logging.info(
"Downloading seed image (" +
str(seed_images_left) +
" left):")
day_to_try = datetime.now() - timedelta(days=days_back)
# Filenames look like /apYYMMDD.html
seed_filename = NASA_APOD_SITE + "ap" + \
day_to_try.strftime("%y%m%d") + ".html"
seed_site_contents = download_site(seed_filename)
# Make sure we didn't encounter an error for some reason
if seed_site_contents == "error":
continue
seed_filename = get_image(seed_site_contents)
# If the content was an video or some other error occurred, skip the
# rest.
if seed_filename is None:
continue
resize_image(seed_filename)
# Add this to our list of images
images.append(seed_filename)
seed_images_left -= 1
logging.info("Done downloading seed images")
# Get our images in a random order so we get a new order every time we get
# a new file
random.shuffle(images)
# Recalculate the number of pictures
num_images = len(images)
for i, image in enumerate(images):
# Create a static entry for keeping this image here for IMAGE_DURATION
static = etree.SubElement(background, "static")
# Length of time the background stays
duration = etree.SubElement(static, "duration")
duration.text = str(IMAGE_DURATION)
# Assign the name of the file for our static entry
static_file = etree.SubElement(static, "file")
static_file.text = images[i]
# Create a transition for the animation with a from and to
transition = etree.SubElement(background, "transition")
# Length of time for the switch animation
transition_duration = etree.SubElement(transition, "duration")
transition_duration.text = "5"
# We are always transitioning from the current file
transition_from = etree.SubElement(transition, "from")
transition_from.text = images[i]
# Create our tranition to element
transition_to = etree.SubElement(transition, "to")
# Check to see if we're at the end, if we are use the first image as
# the image to
if i + 1 == num_images:
transition_to.text = images[0]
else:
transition_to.text = images[i + 1]
xml_tree = etree.ElementTree(background)
xml_tree.write(filename, pretty_print=True)
return filename
def get_image_info(element, text):
# Grabs information about the image
regex = '<' + element + '="(image.*?)"'
reg = re.search(regex, str(text), re.IGNORECASE)
if reg:
if 'http' in reg.group(1):
# Actual URL
file_url = reg.group(1)
else:
# Relative path, handle it
file_url = NASA_APOD_SITE + reg.group(1)
else:
logging.info("Could not find an image. May be a video today.")
return None, None, None
# Create our handle for our remote file
logging.info("Opening remote URL")
remote_file = urllib.request.urlopen(file_url)
filename = os.path.basename(file_url)
file_size = float(remote_file.headers.get("content-length"))
return file_url, filename, file_size
def parse_args():
global DOWNLOAD_PATH
global CUSTOM_FOLDER
global RESOLUTION_TYPE
global RESOLUTION_X
global RESOLUTION_Y
global RESIZE_TYPE
global PICTURE_OPTIONS
global IMAGE_SCROLL
global IMAGE_DURATION
global SEED_IMAGES
global SHOW_DEBUG
parser = argparse.ArgumentParser()
parser.add_argument('--download_path', type=pathlib.Path,
default=DOWNLOAD_PATH,
help='Where you want the file to be downloaded')
parser.add_argument(
'--custom_folder',
type=pathlib.Path,
default=CUSTOM_FOLDER,
help='Target folder if download path auto-detected')
parser.add_argument('--resolution_type', type=str, default=RESOLUTION_TYPE,
help='Resolution type')
parser.add_argument('--resolution_x', type=int, default=RESOLUTION_X,
help='Horizontal resolution of screen')
parser.add_argument('--resolution_y', type=int, default=RESOLUTION_Y,
help='Vertical resolution of screen')
parser.add_argument('--resize_type', type=str, default=RESIZE_TYPE,
help='Resize type')
parser.add_argument('--picture_options', type=str, default=PICTURE_OPTIONS,
help='gnome picture-options setting')
parser.add_argument(
'--image_scroll',
default=IMAGE_SCROLL,
action=argparse.BooleanOptionalAction,
help='If true, will write an XML file to make the images scroll')
parser.add_argument('--image_duration', type=int, default=IMAGE_DURATION,
help='Duration in seconds between image scrolls')
parser.add_argument('--seed_images', type=int, default=SEED_IMAGES,
help='Number of seed images')
parser.add_argument(
'--debug',
default=SHOW_DEBUG,
action=argparse.BooleanOptionalAction,
help='Print useful debugging information or statuses')
args = parser.parse_args()
DOWNLOAD_PATH = str(args.download_path)
CUSTOM_FOLDER = str(args.custom_folder)
RESOLUTION_TYPE = args.resolution_type
RESOLUTION_X = args.resolution_x
RESOLUTION_Y = args.resolution_y
RESIZE_TYPE = args.resize_type
PICTURE_OPTIONS = args.picture_options
IMAGE_SCROLL = args.image_scroll
IMAGE_DURATION = args.image_duration
SEED_IMAGES = args.seed_images
SHOW_DEBUG = args.debug
if __name__ == '__main__':
parse_args()
# Our program
if SHOW_DEBUG:
logging.basicConfig(level=logging.INFO)
logging.info("Starting")
# Find desktop resolution
RESOLUTION_X, RESOLUTION_Y = find_resolution()
# Set a localized download folder
DOWNLOAD_PATH = set_download_folder()
# Create the download path if it doesn't exist
if not os.path.exists(os.path.expanduser(DOWNLOAD_PATH)):
os.makedirs(os.path.expanduser(DOWNLOAD_PATH))
# Grab the HTML contents of the file
site_contents = download_site(NASA_APOD_SITE)
if site_contents == "error":
logging.error("Could not contact site.")
exit()
# Download the image
filename = get_image(site_contents)
if filename is not None:
# Resize the image
resize_image(filename)
# Create the desktop switching xml
filename = create_desktop_background_scoll(filename)
# If the script was unable todays image and IMAGE_SCROLL is set to False,
# the script exits
if filename is None:
logging.warning("Today's image could not be downloaded.")
exit()
# Set the wallpaper
status = set_gnome_wallpaper(filename)
logging.info("Finished!")