Created
February 10, 2015 15:30
-
-
Save samisalkosuo/2933e03d39cc44be9097 to your computer and use it in GitHub Desktop.
Random Astronomy Picture of the Day from http://apod.nasa.gov/.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#retrieve random Astronomy Picture of the Day | |
#from http://apod.nasa.gov/ | |
import sys | |
import datetime | |
import urllib | |
import urllib2 | |
import random | |
import re | |
from PIL import Image | |
import os.path | |
import json | |
apodUrl="http://apod.nasa.gov/apod" | |
def main(): | |
#TODO: add cli argument to get specifid day | |
try: | |
today=datetime.date.today() | |
earliestDay=datetime.date(1995,6,20) | |
day=today | |
days=[] | |
i=1 | |
while day>=earliestDay: | |
_d=day.strftime("%Y-%m-%d") | |
days.append(_d) | |
day=today-datetime.timedelta(days=i) | |
i=i+1 | |
chosenDay= random.choice(days) | |
#chosenDay="2005-10-01" | |
descfname="/tmp/apod%s%s" % (chosenDay,".txt") | |
if os.path.isfile(descfname): | |
#already exists | |
descf=open(descfname,"r") | |
ext="" | |
for line in descf: | |
if line.find("ext=")==-1: | |
print line.strip() | |
else: | |
ext=line.replace("ext=","").strip() | |
descf.close() | |
fname="/tmp/apod%s%s" % (chosenDay,ext) | |
img = Image.open(fname) | |
img.show() | |
sys.exit(0) | |
apodDay=chosenDay[2:].replace("-","") | |
url="%s/ap%s.html" % (apodUrl,apodDay) | |
descf=open(descfname,"w") | |
out="Chosen day: %s" % chosenDay | |
print out | |
descf.write(out+"\n") | |
out= "URL : %s" % url | |
print out | |
descf.write(out+"\n") | |
apodHtml=urllib2.urlopen(url).read() | |
imageUrl=getImageUrl(apodHtml) | |
nameandcredits=getImageNameAndCredits(apodHtml) | |
rx=re.compile(r'<[^>]+>') | |
nameandcredits = re.sub(rx,'', nameandcredits).strip() | |
nameandcredits=nameandcredits.split("\n") | |
nc=[] | |
for item in nameandcredits: | |
item=item.strip() | |
if item!="": | |
nc.append(item) | |
out= "Image URL : %s" % imageUrl | |
print out | |
descf.write(out+"\n") | |
out=json.dumps(nc,ensure_ascii=False) | |
print out | |
descf.write(out+"\n") | |
ext=imageUrl.rfind(".") | |
ext=imageUrl[ext:] | |
descf.write("ext="+ext+"\n") | |
descf.close() | |
fname="/tmp/apod%s%s" % (chosenDay,ext) | |
urllib.urlretrieve (imageUrl, fname) | |
img = Image.open(fname) | |
img.show() | |
except Exception as e: | |
print "ERROR: ",e | |
sys.exit(1) | |
def getImageUrl(html): | |
htmlL=html.lower() | |
index=htmlL.find("<img") | |
if index==-1: | |
raise Exception("Image not found") | |
endIndex=htmlL.find(">",index) | |
imgSrc=html[index:endIndex] | |
imgStart=imgSrc.find('"')+1 | |
imgEnd=imgSrc.find('"',imgStart) | |
imgSrc=imgSrc[imgStart:imgEnd] | |
return "%s/%s" % (apodUrl,imgSrc) | |
def getImageNameAndCredits(html): | |
#print html | |
htmlL=html.lower() | |
index=htmlL.find("<img") | |
endIndex=htmlL.find("</a>",index) | |
endIndex=htmlL.find("explanation",endIndex) | |
nameandcredits=html[index:endIndex] | |
return nameandcredits | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment