From de299f61ead009a8f87c2989150332bb062706e7 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 20:21:52 +0200 Subject: [PATCH] Bing --- server/nightr/app.py | 3 ++- server/nightr/strategies/bing.py | 34 ++++++++++++++++++++++++++ server/nightr/strategies/iss.py | 2 +- server/nightr/strategies/miloStrats.py | 5 +++- 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 server/nightr/strategies/bing.py diff --git a/server/nightr/app.py b/server/nightr/app.py index 3eecf5e..b8f788b 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 +from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour, bing from .util import Context app = Flask(__name__) @@ -29,6 +29,7 @@ strategies = { "cars_in_traffic": cars_in_traffic.cars_in_traffic, "tide": tide_strat.is_tide, "upstairs_neighbour": upstairs_neighbour.check_games, + "bing": bing.clock, } diff --git a/server/nightr/strategies/bing.py b/server/nightr/strategies/bing.py new file mode 100644 index 0000000..95d7064 --- /dev/null +++ b/server/nightr/strategies/bing.py @@ -0,0 +1,34 @@ +from datetime import datetime + +import requests +from bs4 import BeautifulSoup + +from ..util import Context, Prediction + + +def clock(context: Context) -> Prediction: + """ + It's nighttime if Bing says it's daytime. + """ + p = Prediction() + p.weight = 0.5 + + 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'} + r = requests.get("https://www.bing.com/search?q=time+in+denmark", headers=headers) + soup = BeautifulSoup(r.content, features='html5lib') + time_str = soup.find("div", {"id": "digit_time"}).text + + time = datetime.strptime(time_str, "%H:%M") + night = time.hour < 6 or time.hour >= 22 + + time_description = "" if night else "daytime" + time_description_oppersite = "daytime" if night else "nighttime" + + p.reasons.append(f"Bing says its {time_description}.") + p.reasons.append(f"We don't really trust it.") + p.reasons.append(f"Let's guess its {time_description_oppersite}.") + + p.probability = 1 - p.probability + + return p diff --git a/server/nightr/strategies/iss.py b/server/nightr/strategies/iss.py index e4ca5de..64b308a 100644 --- a/server/nightr/strategies/iss.py +++ b/server/nightr/strategies/iss.py @@ -48,7 +48,7 @@ def night_on_iss(context: Context) -> Prediction: break iss_time = datetime.now(pytz.timezone(iss_tz)) - iss_night = 6 < iss_time.hour > 22 + iss_night = iss_time.hour < 6 or iss_time.hour >= 22 # iss_night on_iss_time night # 0 0 1 diff --git a/server/nightr/strategies/miloStrats.py b/server/nightr/strategies/miloStrats.py index a6aee42..d0418f7 100644 --- a/server/nightr/strategies/miloStrats.py +++ b/server/nightr/strategies/miloStrats.py @@ -40,7 +40,11 @@ def australiaStrat(context : Context) -> Prediction: p.reasons.append('It\'s day-time in Australia') return p + def tv2newsStrat(context : Context) -> Prediction: + """ + The number of articles releases in the last few hours on TV2.dk + """ r = requests.get('http://mpx.services.tv2.dk/api/latest') data = r.json() publish_dates = [(x['pubDate'])//1000 for x in data][:10] @@ -61,4 +65,3 @@ def tv2newsStrat(context : Context) -> Prediction: p.probability = 1.0 if avg_timestamp > 50 else 0.0 p.reasons.append('There were ' + ('few' if avg_timestamp > 50 else 'many') + ' recent articles on TV2 News') return p -