-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
67 lines (54 loc) · 2.24 KB
/
application.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
import requests
import requests_cache
from dateutil.relativedelta import relativedelta
from flask import Flask, render_template
application = Flask(__name__)
# Create a config.py file with API_TOKEN = '' and CACHE_TTL = int
application.config.from_object('config')
apitoken = application.config["API_TOKEN"]
cache_ttl = application.config["CACHE_TTL"]
requests_cache.install_cache('football_data_cache', backend='sqlite',
expire_after=cache_ttl)
headers = {'X-Auth-Token': apitoken, 'X-Response-Control': 'minified'}
url = 'http://api.football-data.org/v1/'
@application.route('/')
def getCompetitions():
requeststring = "/competitions/"
r = requests.get(url + requeststring, headers=headers)
response = r.json()
output = {}
for competition in response:
output[competition['id']] = {'caption': competition['caption'],
'league': competition['league']}
return render_template('index.html', content=output)
@application.route('/league/<league>')
def getTeams(league):
requeststring = "/competitions/" + str(league) + "/teams"
r = requests.get(url + requeststring, headers=headers)
response = r.json()
output = {}
for team in response['teams']:
output[team['id']] = team['name']
return render_template('teamlist.html', content=output)
@application.route('/team/<team>')
def printNextMatch(team):
today = str(datetime.date.today())
nextMonth = str(datetime.date.today() + relativedelta(months=12))
requeststring = "/teams/" + str(team) + "/fixtures?timeFrameStart=" +\
today + "&timeFrameEnd=" + nextMonth
r = requests.get(url + requeststring, headers=headers)
response = r.json()
output = {}
if response['count'] == 0:
return("No Fixture in the next month")
else:
nextDate = response['fixtures'][0]['date']
nextAwayTeam = response['fixtures'][0]['awayTeamName']
nextHomeTeam = response['fixtures'][0]['homeTeamName']
output['nextDate'] = nextDate
output['nextHomeTeam'] = nextHomeTeam
output['nextAwayTeam'] = nextAwayTeam
return render_template('fixture.html', content=output)
if __name__ == "__main__":
application.run()