-
Notifications
You must be signed in to change notification settings - Fork 0
/
oddschecker.py
53 lines (47 loc) · 1.79 KB
/
oddschecker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import time
from bs4 import BeautifulSoup, Comment
from flask import Flask, render_template, redirect, url_for
import re
import requests
import json
import io
import numpy as np
app = Flask(__name__)
def fetch(url):
data = []
html = requests.get(url)
parseable = BeautifulSoup(html.content,"html.parser")
odds_table = parseable.find("table", {"id": "odds_table"})
rows = odds_table.findAll('tr',{'data-dt': re.compile(r".*")})
for item in rows:
obj = {}
country = item.find('img')['alt']
obj['country'] = country
all_odds = item.findAll('td',{'data-aff-dt': re.compile(r".*")})
odds = []
for item in all_odds:
odds.append(float(item.contents[0]))
avg = np.mean(odds)
avg = "%.2f" % (1 / avg * 100) +"%"
obj['odds'] = avg
data.append(obj)
return data
#Only fetch new data if data is older than 2 hours
datasets = [['static/data.json','https://eurovisionworld.com/odds/eurovision'],['static/data-top10.json','https://eurovisionworld.com/odds/eurovision-top-10'],['static/data-semi1.json','https://eurovisionworld.com/odds/eurovision-semi-final-1'],['static/data-semi2.json','https://eurovisionworld.com/odds/eurovision-semi-final-2']]
@app.route('/eurovision')
def render_home():
for dataset in datasets:
file_mod_time = os.stat(dataset[0]).st_mtime
last_time = (time.time() - file_mod_time) / 360
if last_time > 2:
data = fetch(dataset[1])
with io.open(dataset[0], 'w', encoding='utf-8') as f:
f.write(json.dumps(data, ensure_ascii=False))
return render_template('index.html')
@app.route('/')
def redirect_home():
#print last_time
return redirect(url_for('render_home'))
if __name__ == '__main__':
app.run(debug=True)