-
Notifications
You must be signed in to change notification settings - Fork 0
/
earthy.py
executable file
·43 lines (32 loc) · 1.2 KB
/
earthy.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
#!/usr/bin/env python3.7
import urllib.request
import shutil
import json
import os
from datetime import date, timedelta
OUTPUT_PATH = "/tmp/"
IMAGE_LIST_URL_BASE = "http://epic.gsfc.nasa.gov/api/images.php?date="
IMAGE_URL_BASE = "http://epic.gsfc.nasa.gov/epic-archive/"
IMAGE_FORMAT = "png"
def image_list(idate):
return json.loads(urllib.request.urlopen(IMAGE_LIST_URL_BASE + idate.strftime("%Y-%m-%d")).read().decode('utf-8'))
def get_file(url, out_file):
print("Getting {}".format(url))
with urllib.request.urlopen(url) as response, open(out_file, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
print("Wrote {}".format(out_file))
if __name__ == '__main__':
today = date.today()
while True:
print("Checking images for {}".format(today))
if not image_list(today):
today = today - timedelta(1)
continue
img_name = image_list(today)[-1][u'image'] + "." + IMAGE_FORMAT
out_file = OUTPUT_PATH + img_name
if not os.path.isfile(out_file):
url = IMAGE_URL_BASE + IMAGE_FORMAT + "/" + img_name
get_file(url, out_file)
else:
print("{} exists".format(out_file))
break