year-progress/year-progress.py

185 lines
5.8 KiB
Python

import datetime
import math
from flask import Flask, render_template, url_for, request
app = Flask(__name__)
def num2day(num):
match num:
case 1:
return "Monday"
case 2:
return "Tuesday"
case 3:
return "Wednesday"
case 4:
return "Thursday"
case 5:
return "Friday"
def latLongToCartesian(lat,long):
lat = lat*math.pi/180
long = long*math.pi/180
x = math.cos(lat)*math.cos(long)
y = math.cos(lat)*math.sin(long)
z = math.sin(lat)
return x,y,z
def cartesianToLatLong(x,y,z):
long = math.atan2(y,x)
hyp = math.sqrt(x*x + y*y)
lat = math.atan2(z,hyp)
lat = lat*180/math.pi
long = long*180/math.pi
return lat, long
def getPercentageLatLong(yearPercent,lat1,long1,lat2,long2):
# http://www.geomidpoint.com/calculation.html
x1, y1, z1 = latLongToCartesian(lat1,long1)
x2, y2, z2 = latLongToCartesian(lat2,long2)
xmid = (x1*(1-yearPercent)+x2*yearPercent)
ymid = (y1*(1-yearPercent)+y2*yearPercent)
zmid = (z1*(1-yearPercent)+z2*yearPercent)
latmid, longmid = cartesianToLatLong(xmid,ymid,zmid)
return latmid, longmid
def workWeek(yearPercent):
totalNumHours = 40*yearPercent
numDays = totalNumHours/8
hoursIntoDay = (numDays - math.floor(numDays))*8
minutesIntoDay = round((hoursIntoDay - math.floor(hoursIntoDay))*60)
if minutesIntoDay > 59:
minutesIntoDay = 0
hoursIntoDay += 1
workDatetime = datetime.datetime(year=1970,month=1,day=1,hour=math.floor(hoursIntoDay)+9,minute=minutesIntoDay)
workTime = workDatetime.strftime("%-I:%M%p")
return num2day(math.floor(numDays+1)), workTime
def abbeyRoad(yearPercent):
totalSeconds = 47*60+29 # 47 minutes 29 seconds
numSeconds = math.floor(totalSeconds*yearPercent);
# 4:19
if numSeconds <= 4*60+19:
return "Come Together"
# 3:02
elif numSeconds <= 259+(3*60+2):
return "Something"
# 3:27
elif numSeconds <= 441+(3*60+27):
return "Maxwell's Silver Hammer"
# 3:27
elif numSeconds <= 648+(3*60+27):
return "Oh! Darling"
# 2:50
elif numSeconds <= 855+(2*60+50):
return "Octopus's Garden"
# 7:47
elif numSeconds <= 1025+(7*60+47):
return "I Want You (She's So Heavy)"
# 3:05
elif numSeconds <= 1492+(3*60+5):
return "Here Comes The Sun"
# 2:45
elif numSeconds <= 1677+(2*60+45):
return "Because"
# 4:02
elif numSeconds <= 1842+(4*60+2):
return "You Never Give Me Your Money"
# 2:26
elif numSeconds <= 2084+(2*60+26):
return "Sun King"
# 1:06
elif numSeconds <= 2230+(1*60+6):
return "Mean Mr. Mustard"
# 1:12
elif numSeconds <= 2296+(1*60+12):
return "Polythene Pam"
# 1:58
elif numSeconds <= 2368+(1*60+58):
return "She Came In Through The Bathroom Window"
# 1:31
elif numSeconds <= 2486+(1*60+31):
return "Golden Slumbers"
# 1:36
elif numSeconds <= 2577+(1*60+36):
return "Carry That Weight"
# 2:21
elif numSeconds <= 2673+(2*60+21):
return "The End"
# 0:25
else:
return "Her Majesty"
def theHobbit():
pass
def transatlanticFlight(yearPercent):
lat1 = 51.470020
long1 = -0.454295
lat2 = 40.641766
long2 = -73.780968
lat, long = getPercentageLatLong(yearPercent,lat1,long1,lat2,long2)
return round(lat,2), round(long,2)
def getHomoSapiens(yearPercent):
totalYears = 4.3e9
yearsUntilHumans = totalYears*(1-yearPercent) - 300e3
if yearsUntilHumans >= 0:
return f"Homo Sapiens would evolve in ~{math.floor(yearsUntilHumans)} years."
else:
return f"Homo Sapiens have been around for ~{-math.floor(yearsUntilHumans)} years."
def getTemplate(timestamp):
now = datetime.datetime.fromtimestamp(timestamp)
nowTime = now.strftime("%-I:%M%p")
nowDate = now.strftime("%A") + " " + now.strftime("%-d") + " " + now.strftime("%B")
delta = now - datetime.datetime(day=1,month=1,year=now.year)
totalDays = datetime.datetime(day=1,month=1,year=now.year+1) - datetime.datetime(day=1,month=1,year=now.year)
yearPercent = delta/totalDays
footballMinute = math.floor(yearPercent*90)
workDay, workTime = workWeek(yearPercent)
marathonMile = round(26.2 - math.floor(yearPercent*26.2),2)
abbeyRoadSong = abbeyRoad(yearPercent)
hobbitChapter = "having an unexpected party"
lat, long = transatlanticFlight(yearPercent)
homoSapiens = getHomoSapiens(yearPercent)
return now.year, nowTime, nowDate, round(yearPercent*100,2), footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens
def index(timestamp):
year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens = getTemplate(timestamp)
cssPath = url_for("static", filename="style.css")
return render_template("index.html",\
cssPath=cssPath,\
year=year,\
nowTime=nowTime,\
nowDate=nowDate,\
yearPercent=yearPercent,\
footballMinute=footballMinute,\
workDay=workDay,\
workTime=workTime,\
marathonMile=marathonMile,\
abbeyRoadSong=abbeyRoadSong,\
hobbitChapter=hobbitChapter,\
lat=lat,\
long=long,\
homoSapiens=homoSapiens)
@app.route("/", methods=["GET","POST"])
def init():
if request.method == "POST":
timestamp = int(request.values.get('timestamp'))
return index(timestamp)
else:
return render_template("init.html")