-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapod.py
executable file
·62 lines (47 loc) · 1.62 KB
/
apod.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
#!/usr/bin/env python3
from urllib3 import PoolManager, exceptions
from urllib.request import urlretrieve
from json import loads
from subprocess import Popen
# Set the directory you want to save the pictures in
DIRECTORY = '/home/cydonia/Pictures/apod/'
URL = 'https://api.nasa.gov/planetary/apod?api_key=zaVObY9zGhMh20jhIaTwqUkrgdAeftR8MltzY5ye'
def notify(message):
# TODO: Not the notification type I wanted. Do research
p = Popen(['notify-send', ' ', message])
p.wait()
def setWallpaper(saveAs):
args = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://' + saveAs]
q = Popen(args)
q.wait()
print('Wallpaper set.')
def getInfo(url):
try:
http = PoolManager()
response = http.request('GET', url)
print("Information retrieved...")
return loads(response.data.decode('utf-8'))
except exceptions.MaxRetryError:
notify('APOD: No Internet connectivity found.')
exit(0)
def downloadImage(response):
assert response['media_type'] == 'image'
imgUrl = response['hdurl']
imgName = imgUrl.strip().split('/')[-1]
saveAs = DIRECTORY + response['date'] + '_' + imgName
urlretrieve(imgUrl, saveAs)
print("Image retrieved...")
return saveAs, response['explanation']
def run():
response = getInfo(URL)
try:
saveAs, explanation = downloadImage(response)
setWallpaper(saveAs)
notify(explanation)
except AssertionError:
notify('APOD: Media type not supported.')
except KeyError:
notify('APOD: Service not available.')
if __name__ == '__main__':
run()
exit(0)