nightr/server/nightr/strategies/miloStrats.py
2019-04-07 00:56:14 +02:00

73 lines
2.0 KiB
Python

from datetime import datetime
import requests
from pytz import timezone
from ..util import Context, Prediction
def camImgStrat(context : Context) -> Prediction:
"""
The contents of the camera image
"""
img = context.image
average = float(img.mean())
p = Prediction()
p.weight = 1.0
p.probability = 1 - round((average/255),3)
if average < 128:
p.weight = round(1 - (average/255), 3)
p.reasons.append('Camera image was dark, so the sun has probably set.')
else:
p.weight = round(average / 255, 3)
p.reasons.append('Camera image was light, so the sun is still shining.')
return p
def australiaStrat(context : Context) -> Prediction:
"""
Using time in Australia
"""
australia = timezone('Australia/Melbourne')
t = datetime.now().astimezone(australia)
hour = t.hour
p = Prediction()
if hour > 22 or hour < 6:
p.probability = 0.0
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, so it must be night-time here.')
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[:5]]
delta_times = []
for i in range(len(publish_dates)):
if i == 0 : continue
delta_times.append(publish_dates[i-1] - publish_dates[i])
avg_delta = 0
for d in delta_times:
avg_delta += d
avg_timestamp = avg_delta // len(delta_times) // 60
p = Prediction()
if avg_timestamp < 0:
p.weight = 0.0
else:
p.weight = 0.7
print(avg_timestamp)
p.probability = 0.75 if avg_timestamp > 40 else 0.25
p.reasons.append('There were ' + ('few' if avg_timestamp > 40 else 'many') + ' recent articles on TV2 News')
return p