Merge remote-tracking branch 'origin/master'

This commit is contained in:
Alexander Munch-Hansen 2019-04-07 02:38:23 +02:00
commit 4560ecfb63
4 changed files with 28 additions and 25 deletions

View File

@ -11,7 +11,7 @@ def clock(context: Context) -> Prediction:
It's nighttime if Bing says it's daytime. It's nighttime if Bing says it's daytime.
""" """
p = Prediction() p = Prediction()
p.weight = 0.02 p.weight = 0.01
headers = { headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'} 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}

View File

@ -1,15 +1,11 @@
import itertools import operator
from datetime import datetime from datetime import datetime, timezone
from math import pi, sqrt, sin, cos, atan2 from math import pi, sqrt, sin, cos, atan2
import pytz
import requests import requests
from timezonefinder import TimezoneFinder
from ..util import Context, Prediction from ..util import Context, Prediction
tf = TimezoneFinder(in_memory=True)
def night_on_iss(context: Context) -> Prediction: def night_on_iss(context: Context) -> Prediction:
""" """
@ -20,34 +16,43 @@ def night_on_iss(context: Context) -> Prediction:
if not context.flat_earth: if not context.flat_earth:
iss_position = requests.get("http://api.open-notify.org/iss-now.json").json()["iss_position"] iss_position = requests.get("http://api.open-notify.org/iss-now.json").json()["iss_position"]
the_iss = "The ISS" the_iss = "The ISS"
at_iss_location = "at the ISS's location"
iss_position_description = "on board the ISS" iss_position_description = "on board the ISS"
else: 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" the_iss = "Hollywood"
iss_position = {'latitude': 34.092808, 'longitude': -118.328659} # Hollywood iss_position = {'latitude': 34.092808, 'longitude': -118.328659} # Hollywood
at_iss_location = "in Hollywood"
iss_position_description = "in the Hollywood studio" 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 # 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) 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 max_distance = 40075 / 2 # the furthest you can be from any position is half of the earth's circumference
ratio = distance / max_distance ratio = distance / max_distance
# We're in the same "timezone" as the ISS if we're on the same half of the earth # 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 on_iss_time = ratio < 0.5
side = "same" if on_iss_time else "other" 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.") 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 # iss_night on_iss_time night
# 0 0 1 # 0 0 1
@ -56,11 +61,10 @@ def night_on_iss(context: Context) -> Prediction:
# 1 1 1 # 1 1 1
night = iss_night == on_iss_time night = iss_night == on_iss_time
iss_time_description = "nighttime" if iss_night else "daytime"
time_description = "nighttime" if night else "daytime" time_description = "nighttime" if night else "daytime"
p.probability = float(night) op = operator.add if night else operator.sub
p.reasons.append(f"It is {iss_time_description} {iss_position_description}.") p.probability = op(0.5, ratio * 0.5)
p.reasons.append(f"Therefore, it must be {time_description} where we are.") p.reasons.append(f"So it must be {time_description} where we are.")
return p return p

View File

@ -34,7 +34,7 @@ def is_restaurant_open(name, open, close) -> Prediction:
else: else:
p.reasons.append(f"Our favorite pizza place, {name}, is closed.") 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.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 return p

View File

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