124 lines
6.0 KiB
Python
Executable File
124 lines
6.0 KiB
Python
Executable File
#!/usr/bin/python
|
|
import os, urllib2 as url, json
|
|
import datetime as dt
|
|
from optparse import OptionParser
|
|
|
|
# Greenwich Observatory
|
|
lat="51.4826"
|
|
lng="0.000"
|
|
|
|
optparse = OptionParser(usage="Usage: %prog [options]")
|
|
optparse.add_option('-d','--date',dest='date',help='Set date to qurey YYYY-MM-DD [default=today]',default='today')
|
|
optparse.add_option('-t','--timetype',dest='ttype',help='Set sunrise/sunset type (standard,civil,nautical,astronomical) [default=standard] see https://www.timeanddate.com/astronomy/different-types-twilight.html',default='standard')
|
|
optparse.add_option('-i','--interval',dest='interval',help='Minute Interval [default=1, max=60]',default='1')
|
|
optparse.add_option('-x','--lat',dest='lat',help='Latitude defaults to ' + lat,default=lat)
|
|
optparse.add_option('-y','--lng',dest='lng',help='Longitude defaults to ' + lng,default=lng)
|
|
optparse.add_option('-f','--force',action='store_const',const=1,dest='force',help='Force lookup/update',default=0)
|
|
(options, args) = optparse.parse_args()
|
|
|
|
options.ttype = 'civil'
|
|
|
|
def min_round(x,base):
|
|
minute = dt.datetime.strptime(str(int(base * int(float(x)/base))),'%M')
|
|
return minute.strftime("%M")
|
|
|
|
def riseset_info(lat,lng,date,ttype):
|
|
ret = {}
|
|
apiUrl="https://api.sunrise-sunset.org/json?lat=" + str(lat) + "&lng=" + str(lng) + "&date=" + str(date)
|
|
response = url.urlopen(apiUrl).read()
|
|
result = json.loads(response)
|
|
|
|
if ttype == 'standard':
|
|
sunrise = result['results']['sunrise']
|
|
sunset = result['results']['sunset']
|
|
elif ttype == 'civil':
|
|
sunrise = result['results']['civil_twilight_begin']
|
|
sunset = result['results']['civil_twilight_end']
|
|
elif ttype == 'nautical':
|
|
sunrise = result['results']['nautical_twilight_begin']
|
|
sunset = result['results']['nautical_twilight_end']
|
|
elif ttype == 'astronomical':
|
|
sunrise = result['results']['astronomical_twilight_begin']
|
|
sunset = result['results']['astronomical_twilight_end']
|
|
else:
|
|
print
|
|
print "ERROR: Invalid Time Type"
|
|
print
|
|
optparse.print_help()
|
|
print
|
|
os._exit(1)
|
|
|
|
srtime = dt.datetime.strptime(sunrise,"%I:%M:%S %p")
|
|
sstime = dt.datetime.strptime(sunset,"%I:%M:%S %p")
|
|
|
|
sunrise_H = srtime.strftime("%H")
|
|
sunrise_M = srtime.strftime("%M")
|
|
sunrise_MR = min_round(x=sunrise_M,base=int(options.interval))
|
|
srtime = dt.datetime.strptime(sunrise_H + ':' + str(sunrise_MR),"%H:%M")
|
|
ret['sunrise'] = srtime
|
|
ret['sunrise_text'] = srtime.strftime("%H%M")
|
|
|
|
sunset_H = sstime.strftime("%H")
|
|
sunset_M = sstime.strftime("%M")
|
|
sunset_MR = min_round(x=sunset_M,base=int(options.interval))
|
|
if sunset_M != sunset_MR:
|
|
sunset_MR = sunset_MR + int(options.interval)
|
|
if sunset_MR == 60:
|
|
sunset_H = int(sunset_H) + 1
|
|
sunset_MR = 0
|
|
sstime = dt.datetime.strptime(str(sunset_H) + ':' + str(sunset_MR),"%H:%M")
|
|
ret['sunset'] = sstime
|
|
ret['sunset_text'] = sstime.strftime("%H%M")
|
|
return ret
|
|
|
|
|
|
#
|
|
# Generating file list and getting sunrise/sunset data
|
|
#
|
|
years = os.listdir('.')
|
|
for year in years:
|
|
if not os.path.isfile(year):
|
|
months = os.listdir(year+'/.')
|
|
for month in months:
|
|
if not os.path.isfile(year+'/'+month):
|
|
days = os.listdir(year+'/'+month+'/.')
|
|
for day in days:
|
|
ymd=year+'/'+month+'/'+day
|
|
if not os.path.isfile(ymd):
|
|
#print year+'/'+month+'/'+day
|
|
if options.force or not os.path.isfile(ymd+'/sunrise.info') or not os.path.isfile(ymd+'/sunset.info'):
|
|
riseset = riseset_info(lat=options.lat,lng=options.lng,date=year+'-'+month+'-'+day,ttype=options.ttype)
|
|
if options.force or not os.path.isfile(ymd+'/sunrise.info'):
|
|
sunrise_file = open(ymd+'/sunrise.info','w')
|
|
sunrise_file.write(riseset['sunrise_text'])
|
|
sunrise_file.close()
|
|
#print year+'-'+month+'-'+day+' Sunrise: ' + str(riseset['sunrise'])
|
|
if options.force or not os.path.isfile(ymd+'/sunset.info'):
|
|
sunset_file = open(ymd+'/sunset.info','w')
|
|
sunset_file.write(riseset['sunset_text'])
|
|
sunset_file.close()
|
|
#print year+'-'+month+'-'+day+' Sunset: ' + str(riseset['sunset'])
|
|
riseset['date'] = year+'-'+month+'-'+day
|
|
else:
|
|
riseset = {}
|
|
riseset['date'] = year+'-'+month+'-'+day
|
|
sunrise_file = open(ymd+'/sunrise.info','r')
|
|
riseset['sunrise_text'] = sunrise_file.read()
|
|
riseset['sunrise'] = dt.datetime.strptime(riseset['sunrise_text'],"%H%M")
|
|
sunrise_file.close()
|
|
sunset_file = open(ymd+'/sunset.info','r')
|
|
riseset['sunset_text'] = sunset_file.read()
|
|
riseset['sunset'] = dt.datetime.strptime(riseset['sunset_text'],"%H%M")
|
|
sunset_file.close()
|
|
files = os.listdir(ymd+'/.')
|
|
for file in files:
|
|
if file != 'sunrise.info' and file != 'sunset.info':
|
|
filetemp = file.split('_')
|
|
filetime_text = filetemp[2].split('.')
|
|
filetime = dt.datetime.strptime(filetime_text[0],"%H%M")
|
|
if filetime < riseset['sunrise'] or filetime > riseset['sunset']:
|
|
continue
|
|
else:
|
|
filemin = filetime.strftime("%M")
|
|
if str(filemin) == str(min_round(x=filemin,base=int(options.interval))):
|
|
print ymd+'/'+file |