This commit is contained in:
Casper 2019-04-07 02:35:26 +02:00
parent b53f8472d4
commit fc6baa68ed
No known key found for this signature in database
GPG Key ID: 289CA03790535054
3 changed files with 27 additions and 24 deletions

View File

@ -1,15 +1,11 @@
import itertools
from datetime import datetime
import operator
from datetime import datetime, timezone
from math import pi, sqrt, sin, cos, atan2
import pytz
import requests
from timezonefinder import TimezoneFinder
from ..util import Context, Prediction
tf = TimezoneFinder(in_memory=True)
def night_on_iss(context: Context) -> Prediction:
"""
@ -20,34 +16,43 @@ def night_on_iss(context: Context) -> Prediction:
if not context.flat_earth:
iss_position = requests.get("http://api.open-notify.org/iss-now.json").json()["iss_position"]
the_iss = "The ISS"
at_iss_location = "at the ISS's location"
iss_position_description = "on board the ISS"
else:
p.reasons.append("The ISS is (obviously) located in Hollywood")
p.reasons.append('The "ISS" is obviously a studio in Hollywood')
the_iss = "Hollywood"
iss_position = {'latitude': 34.092808, 'longitude': -118.328659} # Hollywood
at_iss_location = "in Hollywood"
iss_position_description = "in the Hollywood studio"
phone_position = context.position
sun_times = requests.get(f"https://api.sunrise-sunset.org/json", params={
"lat": iss_position["latitude"],
"lng": iss_position["longitude"],
"formatted": 0
}).json()["results"]
iss_position_sunrise = datetime.strptime(sun_times["sunrise"], "%Y-%m-%dT%H:%M:%S%z")
iss_position_sunset = datetime.strptime(sun_times["sunset"], "%Y-%m-%dT%H:%M:%S%z")
p.reasons.append(f"The sun rises and sets at UTC {iss_position_sunrise.strftime('%H:%M')} and {iss_position_sunset.strftime('%H:%M')}, respectively, {at_iss_location}.")
utcnow = datetime.now(timezone.utc)
p.reasons.append(f"It is currently {utcnow.strftime('%H:%M')} UTC")
iss_night = utcnow < iss_position_sunrise or iss_position_sunset < utcnow
iss_time_description = "nighttime" if iss_night else "daytime"
p.reasons.append(f"Therefore, it is {iss_time_description} {iss_position_description}.")
# Calculate ratio: a number between 0 and 1 saying how close we are to the ISS
phone_position = context.position
distance = haversine(iss_position, phone_position)
max_distance = 40075 / 2 # the furthest you can be from any position is half of the earth's circumference
ratio = distance / max_distance
# We're in the same "timezone" as the ISS if we're on the same half of the earth
on_iss_time = ratio < 0.5
side = "same" if on_iss_time else "other"
p.reasons.append(f"{the_iss} is {int(distance)} km away, so we are on the {side} side of the earth.")
for i in itertools.count(1):
iss_tz = tf.closest_timezone_at(lng=float(iss_position["longitude"]), lat=float(iss_position["latitude"]),
delta_degree=i)
if iss_tz is not None:
break
iss_time = datetime.now(pytz.timezone(iss_tz))
iss_night = iss_time.hour < 6 or iss_time.hour >= 22
# iss_night on_iss_time night
# 0 0 1
@ -56,11 +61,10 @@ def night_on_iss(context: Context) -> Prediction:
# 1 1 1
night = iss_night == on_iss_time
iss_time_description = "nighttime" if iss_night else "daytime"
time_description = "nighttime" if night else "daytime"
p.probability = float(night)
p.reasons.append(f"It is {iss_time_description} {iss_position_description}.")
p.reasons.append(f"Therefore, it must be {time_description} where we are.")
op = operator.add if night else operator.sub
p.probability = op(0.5, ratio * 0.5)
p.reasons.append(f"So it must be {time_description} where we are.")
return p

View File

@ -34,7 +34,7 @@ def is_restaurant_open(name, open, close) -> Prediction:
else:
p.reasons.append(f"Our favorite pizza place, {name}, is closed.")
p.reasons.append(f"We can conclude from this, that there is {1 - (1/11)}% chance of it currently being night outside!")
p.probability = 1 - (1 / 11)
p.probability = round(1 - (1 / 11), 2)
return p

View File

@ -5,7 +5,6 @@ pytz
beautifulsoup4
pandas
opencv-python
timezonefinder
scikit-learn
html5lib
xlrd