From 319c03067f6212af02ebbf8ef253d4ba85480f0c Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 23:00:08 +0200 Subject: [PATCH] Reasons and battery. --- server/nightr/app.py | 3 ++- server/nightr/strategies/battery.py | 21 +++++++++++++++++++ server/nightr/strategies/bing.py | 4 ++-- server/nightr/strategies/iss.py | 3 +-- server/nightr/strategies/just_eat.py | 8 ++++--- server/nightr/strategies/miloStrats.py | 11 ++++------ server/nightr/strategies/svm_strat.py | 6 +++--- .../nightr/strategies/upstairs_neighbour.py | 2 +- server/nightr/util.py | 4 ++-- 9 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 server/nightr/strategies/battery.py diff --git a/server/nightr/app.py b/server/nightr/app.py index b7b4072..ad3ce8f 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -10,7 +10,7 @@ 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 +from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour, bing, battery from .util import Context app = Flask(__name__) @@ -30,6 +30,7 @@ strategies = { "tide": tide_strat.is_tide, "upstairs_neighbour": upstairs_neighbour.check_games, "bing": bing.clock, + "battery_level": battery.battery_level, } diff --git a/server/nightr/strategies/battery.py b/server/nightr/strategies/battery.py new file mode 100644 index 0000000..b301ee8 --- /dev/null +++ b/server/nightr/strategies/battery.py @@ -0,0 +1,21 @@ +from ..util import Context, Prediction + + +def battery_level(context: Context) -> Prediction: + """ + If the battery is low, it's probably bedtime soon. + """ + p = Prediction() + + if context.battery > 60: + p.reasons.append("Battery level's good, so it's probably still early in the day.") + elif context.battery > 30: + p.reasons.append("Battery level's getting low, so it's probably around dinnertime.") + elif context.battery > 10: + p.reasons.append("Your phone is dying, so it's bedtime soon?") + else: + p.reasons.append("Your phone's practically dead, so it's probably around four in the morning.") + + p.probability = 1 - (context.battery / 100) # night is inverse proportional to battery level + + return p diff --git a/server/nightr/strategies/bing.py b/server/nightr/strategies/bing.py index 13b64fd..6b61db5 100644 --- a/server/nightr/strategies/bing.py +++ b/server/nightr/strategies/bing.py @@ -11,7 +11,7 @@ def clock(context: Context) -> Prediction: It's nighttime if Bing says it's daytime. """ p = Prediction() - p.weight = 0.05 + p.weight = 0.02 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'} @@ -22,7 +22,7 @@ def clock(context: Context) -> Prediction: time = datetime.strptime(time_str, "%H:%M") night = time.hour < 6 or time.hour >= 22 - time_description = "" if night else "daytime" + time_description = "nighttime" if night else "daytime" time_description_oppersite = "daytime" if night else "nighttime" p.reasons.append(f"Bing says its {time_description}.") diff --git a/server/nightr/strategies/iss.py b/server/nightr/strategies/iss.py index 64b308a..bd759bc 100644 --- a/server/nightr/strategies/iss.py +++ b/server/nightr/strategies/iss.py @@ -1,5 +1,4 @@ import itertools -import logging from datetime import datetime from math import pi, sqrt, sin, cos, atan2 @@ -14,7 +13,7 @@ tf = TimezoneFinder(in_memory=True) def night_on_iss(context: Context) -> Prediction: """ - It is night if it is night on the ISS and it is currently orbiting above us. + It is night if it is night on the ISS and it is currently orbiting above us. http://www.isstracker.com/ """ p = Prediction() diff --git a/server/nightr/strategies/just_eat.py b/server/nightr/strategies/just_eat.py index 4376958..3bf29e8 100644 --- a/server/nightr/strategies/just_eat.py +++ b/server/nightr/strategies/just_eat.py @@ -17,7 +17,7 @@ def is_restaurant_open(name, open, close) -> Prediction: soup = BeautifulSoup(r.content, features='html5lib') listing_groups = soup.find_all('div', {'class': 'listing-group'}) - p.reasons.append("Hopefully we are not banned from Just-eat ..") + #p.reasons.append("Hopefully we are not banned from Just-eat ..") nice_group = None for x in listing_groups: @@ -32,10 +32,12 @@ def is_restaurant_open(name, open, close) -> Prediction: all_listings = nice_group.find_all('a', {'class': 'mediaElement'}) if any(name in x['href'] for x in all_listings): - p.reasons.append(f"{name} is currently open. We conclude from this, that there is {1 / 11}% chance of it being night outside!") + p.reasons.append(f"Our favorite pizza place, {name}, is currently open.") + p.reasons.append(f"We conclude from this, that there is {1 / 11}% chance of it being night outside") p.probability = 1 / 11 else: - p.reasons.append(f"{name} is not open. We can conclude from this, that there is {1 - (1/11)}% chance of it currently being night outside! ") + 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) return p diff --git a/server/nightr/strategies/miloStrats.py b/server/nightr/strategies/miloStrats.py index 0b51e43..d061c3d 100644 --- a/server/nightr/strategies/miloStrats.py +++ b/server/nightr/strategies/miloStrats.py @@ -18,11 +18,11 @@ def camImgStrat(context : Context) -> Prediction: p.probability = 1 - round((average/255),3) if average < 128: p.weight = round(1 - (average/255), 3) - p.reasons.append('Image was dark') + p.reasons.append('Camera image was dark, so the sun has probably set.') else: p.weight = round(average / 255, 3) - p.reasons.append('Image was light') + p.reasons.append('Camera image was light, so the sun is still shining.') return p @@ -37,10 +37,10 @@ def australiaStrat(context : Context) -> Prediction: if hour > 22 or hour < 6: p.probability = 0.0 - p.reasons.append('It\'s night-time in Australia') + p.reasons.append('It\'s night-time in Australia, so it must be day-time here.') else: p.probability = 1.0 - p.reasons.append('It\'s day-time in Australia') + p.reasons.append('It\'s day-time in Australia, so it must be night-time here.') return p @@ -48,10 +48,7 @@ def tv2newsStrat(context : Context) -> Prediction: """ The number of articles releases in the last few hours on TV2.dk """ - - print('before') r = requests.get('http://mpx.services.tv2.dk/api/latest') - print('after') data = r.json() publish_dates = [(x['pubDate'])//1000 for x in data][:10] diff --git a/server/nightr/strategies/svm_strat.py b/server/nightr/strategies/svm_strat.py index 58a17f6..0af7835 100644 --- a/server/nightr/strategies/svm_strat.py +++ b/server/nightr/strategies/svm_strat.py @@ -50,9 +50,9 @@ def perform_svm_pred(context: Context) -> Prediction: records = data.json()['result']['records'] X = [house['vehicleCount'] / house['totalSpaces'] for house in records] X = [min(x, 1) for x in X] - p.reasons.append("Since we only have two data points") - p.reasons.append("Since our only two data points have 11 dimensions") - p.reasons.append("Since we are using a SVM") + p.reasons.append("We only have two data points") + p.reasons.append("Our only two data points have 11 dimensions") + p.reasons.append("We are using a SVM") p.probability = predict(X) return p diff --git a/server/nightr/strategies/upstairs_neighbour.py b/server/nightr/strategies/upstairs_neighbour.py index 25ce40f..c0f5f70 100644 --- a/server/nightr/strategies/upstairs_neighbour.py +++ b/server/nightr/strategies/upstairs_neighbour.py @@ -34,7 +34,7 @@ def check_games(context: Context) -> Prediction: last_game_in_hours = (((datetime.now() - last_played_game).seconds)/60/60) if last_game_in_hours < 2: - p.reasons.append("Alexanders upstairs neighbour is currently playing league") + p.reasons.append("Alexander's upstairs neighbour is currently playing league") p.probability = 0.8 else: last_game_in_hours = min(24.0, last_game_in_hours) diff --git a/server/nightr/util.py b/server/nightr/util.py index 6c70643..82725a1 100644 --- a/server/nightr/util.py +++ b/server/nightr/util.py @@ -9,8 +9,8 @@ import numpy as np @dataclass class Context: - battery: int = 100 - position: Dict[str, float] = field(default_factory=lambda: {'latitude': 53.0, 'longitude': 9.0}) + battery: int = 55 + position: Dict[str, float] = field(default_factory=lambda: {'latitude': 53.0, 'longitude': 9.0}) # Denmark somewhere image: np.ndarray = None # App settings