From 561b022121f6493d5ec0295958aaa5fda2b9a399 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sun, 7 Apr 2019 01:29:09 +0200 Subject: [PATCH] Update names, update ISS. --- server/nightr/app.py | 28 ++++++++++++++++------------ server/nightr/strategies/iss.py | 7 +++++-- server/nightr/strategies/just_eat.py | 4 ---- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/server/nightr/app.py b/server/nightr/app.py index e0f5d75..23bd1ae 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -10,28 +10,29 @@ from typing import List import requests_cache from flask import Flask, jsonify, logging, request -from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour, bing, svm_strat, battery +from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour, bing, svm_strat, battery, just_eat from .util import Context app = Flask(__name__) logger = logging.create_logger(app) logger.setLevel(DEBUG) -requests_cache.install_cache("requests_cache", expire_after=timedelta(minutes=10)) +requests_cache.install_cache("requests_cache", expire_after=timedelta(minutes=2)) strategies = { # name: (weight, probability function) - "tv2news": miloStrats.tv2newsStrat, - "australia": miloStrats.australiaStrat, - "camera": miloStrats.camImgStrat, - "iss": iss.night_on_iss, - "cars_in_traffic": cars_in_traffic.cars_in_traffic, - "tide": tide_strat.is_tide, - "upstairs_neighbour": upstairs_neighbour.check_games, - "bing": bing.clock, - "svm_parking": svm_strat.perform_svm_pred, - "battery_level": battery.battery_level, + "TV2 News": miloStrats.tv2newsStrat, + "Australia": miloStrats.australiaStrat, + "Camera Image": miloStrats.camImgStrat, + "The International Space Station": iss.night_on_iss, + "Nearby Traffic situation": cars_in_traffic.cars_in_traffic, + "Tidal Measurements": tide_strat.is_tide, + "Legends of Nighttime": upstairs_neighbour.check_games, + "Bing AI": bing.clock, + "ML Parking": svm_strat.perform_svm_pred, + "Phone Battery Level": battery.battery_level, + "Pizza Availability": just_eat.do_just_eat_strat, } @@ -87,6 +88,9 @@ def probabilities(): # If this prediction disagrees with the consensus it contributed negatively if prediction["night"] != night: prediction["contribution"] *= -1 + + predictions.sort(key=lambda p: (p["contribution"], p["probability"]), reverse=True) + return jsonify({ "predictions": predictions, "weighted_probabilities_mean": mean, diff --git a/server/nightr/strategies/iss.py b/server/nightr/strategies/iss.py index bd759bc..4972a43 100644 --- a/server/nightr/strategies/iss.py +++ b/server/nightr/strategies/iss.py @@ -40,11 +40,11 @@ def night_on_iss(context: Context) -> Prediction: 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"]), + 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 @@ -75,6 +75,9 @@ def haversine(pos1, pos2): lat2 = float(pos2["latitude"]) long2 = float(pos2["longitude"]) + lat1 = 0 # we're only interested in the distance in the longitude for the timezone calculation + lat2 = 0 + degree_to_rad = float(pi / 180.0) d_lat = (lat2 - lat1) * degree_to_rad diff --git a/server/nightr/strategies/just_eat.py b/server/nightr/strategies/just_eat.py index 9a314cd..634c9a1 100644 --- a/server/nightr/strategies/just_eat.py +++ b/server/nightr/strategies/just_eat.py @@ -1,12 +1,8 @@ import requests from bs4 import BeautifulSoup -from datetime import datetime, timedelta -import requests_cache from ..util import Context, Prediction -requests_cache.install_cache("requests_cache", expire_after=timedelta(minutes=10)) - def is_restaurant_open(name, open, close) -> Prediction: p = Prediction()