Skip to content

Instantly share code, notes, and snippets.

@jgrocha
Created June 8, 2015 08:30
Show Gist options
  • Save jgrocha/11259d44e2906bfc8bc0 to your computer and use it in GitHub Desktop.
Save jgrocha/11259d44e2906bfc8bc0 to your computer and use it in GitHub Desktop.
Moon phases based on pyephem
#-*- coding: utf-8 -*-
from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse
import ephem, datetime
import json
# Phases of the Moon, using the datetime.now() at UTC
# http://127.0.0.1:8899/
# Phases of the Moon, using the datetime.datetime(int(year), int(month), int(day))) at UTC
# http://127.0.0.1:8899/?year=2015&month=07&day=25
# Sun rising and setting at location
# http://127.0.0.1:8899/?lon=-8.446&lat=40.575&elevation=34
# Phases of the Moon + Sun rising and setting at location
# http://127.0.0.1:8899/?year=2015&month=07&day=25&lon=-8.446&lat=40.575&elevation=34
# forever start -c python servidor.py
# forever logs
class GetHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
parsed_path = urlparse.urlparse(self.path)
d = { 'client': self.client_address }
p = urlparse.parse_qs(parsed_path.query)
if 'day' in p and 'month' in p and 'year' in p:
day = p['day'].pop()
month = p['month'].pop()
year = p['year'].pop()
today = ephem.Date(datetime.datetime(int(year), int(month), int(day)))
else:
today = ephem.Date(datetime.datetime.now())
print today
d['today'] = today.datetime().isoformat()
if 'lon' in p and 'lat' in p and 'elevation' in p:
lon = p['lon'].pop()
print lon
lat = p['lat'].pop()
elevation = p['elevation'].pop()
obs = ephem.Observer()
obs.lon = lon
print obs.lon
obs.lat = lat
obs.elevation = float(elevation)
obs.date = today
print obs
sol = ephem.Sun(obs)
nascer = obs.next_rising(sol,today)
hora_n = ephem.Date(nascer + 1 * ephem.hour)
print 'Nascer do Sol', hora_n
porsol = obs.next_setting(sol,today)
hora_p = ephem.Date(porsol + 1 * ephem.hour)
print 'Por do Sol ', hora_p
d['next_rising'] = hora_n.datetime().isoformat()
d['next_setting'] = hora_p.datetime().isoformat()
date = today
full_moons = []
while date < today + 366.0:
date = ephem.next_full_moon(date)
full_moons.append(date.datetime().isoformat())
d['next_full_moon'] = full_moons
date = today
last_quarter_moon = []
while date < today + 366.0:
date = ephem.next_last_quarter_moon(date)
last_quarter_moon.append(date.datetime().isoformat())
d['last_quarter_moon'] = last_quarter_moon
date = today
new_moon = []
while date < today + 366.0:
date = ephem.next_new_moon(date)
new_moon.append(date.datetime().isoformat())
d['new_moon'] = new_moon
date = today
first_quarter_moon = []
while date < today + 366.0:
date = ephem.next_first_quarter_moon(date)
first_quarter_moon.append(date.datetime().isoformat())
d['first_quarter_moon'] = first_quarter_moon
d['success'] = 'true'
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(d))
except (ValueError, AttributeError) as e:
error = "ValueError: {0}".format(e)
print error
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
e = { 'success': 'false' }
e['error'] = error
self.wfile.write(json.dumps(e))
return
if __name__ == '__main__':
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
# server = HTTPServer(('localhost', 8899), GetHandler)
server = ThreadedHTTPServer(('', 8899), GetHandler)
print 'Starting server, use <Ctrl-C> to stop'
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment