-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
executable file
·114 lines (86 loc) · 4.16 KB
/
data.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import requests
import json
import csv
import datetime
import os
os.chdir(os.path.dirname(os.path.abspath(__file__))) # MUST COME BEFORE RELATIVE IMPORTS
class ResponseError(Exception):
def __init__(self, status_code, data):
self.status_code = status_code
self.body = data.text
self.headers = data.headers
self.request_headers = data.request.headers
super().__init__(self.body)
def __str__(self):
return f'--- Response Error ---\nStatus Code: {self.status_code}\n\nHeaders:\nRequest: {self.request_headers}\nResponse: {self.headers}\n\nBody:\n{self.body}'
class MatchData():
df = 'matchdata.csv'
def fetch_data(self):
http_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
csvHeader = "FanFootyDrawID,year,competition,round,gametime,day,homeTeam,awayTeam,ground,timeslot,TVCoverage,homeSupergoals,homeGoals,homeBehinds,homePoints,awaySupergoals,awayGoals,awayBehinds,awayPoints,matchStatus\n"
# Get data from URL
url = 'http://www.fanfooty.com.au/resource/draw.php'
r = requests.get(url, headers=http_headers)
# Write to file
if r.status_code == 200:
f = open(self.df, 'w', newline='')
f.write(csvHeader)
f.write(r.text.strip())
f.close()
else:
raise ResponseError(r.status_code, r)
return
def get_rounds(self, year):
currentDate = datetime.datetime.now()
ha = []
preliminary = []
finals = []
currentRound = 0
currentComp = None
cRound = -1
cRoundBeenSet = False
with open(self.df) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['year'] == year:
if row['competition'][0] == "P" and int(row['round']) == 0 and currentComp != row['competition']:
currentComp = row['competition']
preliminary.append(row['competition'])
if row['competition'] == "HA" and int(row['round']) != currentRound:
currentRound = int(row['round'])
ha.append(row['round'])
if row['competition'][-1] == "F":
final = ""
if currentComp != row['competition'] and currentRound != int(row['round']):
final = row['competition']
if currentComp != row['competition'] and currentRound == int(row['round']):
if finals[-1].find(row['competition']) == -1:
finals[-1] += " and " + row['competition']
if final != "":
finals.append(final)
currentRound = int(row['round'])
currentComp = row['competition']
if datetime.datetime.strptime(row['gametime'], "%Y-%m-%d %H:%M:%S") > currentDate and not cRoundBeenSet:
cRound = str(currentRound)
cRoundBeenSet = True
dump = json.dumps(
{"preliminary": preliminary, "HA": ha, "finals": finals, "currentRound": cRound})
return dump
def get_matches(self, year, round):
matches = []
# Rewrite this peice of shit, possibly an array of rounds (competitions)
if round == "EF and QF" or round == "QF and EF":
with open(self.df) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['year'] == year and (row['round'] == round or row['competition'] == "EF" or row['competition'] == "QF"):
matches.append(row)
dump = json.dumps(matches)
return dump
with open(self.df) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['year'] == year and (row['round'] == round or row['competition'] == round):
matches.append(row)
dump = json.dumps(matches)
return dump