-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_data.py
More file actions
68 lines (59 loc) · 1.63 KB
/
get_data.py
File metadata and controls
68 lines (59 loc) · 1.63 KB
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
from urllib.request import urlopen
from json import loads
from csv import writer
def team_converter(team_id):
'''Converts a team's ID to their actual name'''
team_map = {
1: "Arsenal",
2: "Aston Villa",
3: "Brighton",
4: "Burnley",
5: "Chelsea",
6: "Crystal Palace",
7: "Everton",
8: "Fulham",
9: "Leicester",
10: "Leeds",
11: "Liverpool",
12: "Man City",
13: "Man Utd",
14: "Newcastle",
15: "Sheffield Utd",
16: "Southampton",
17: "Spurs",
18: "West Brom",
19: "West Ham",
20: "Wolves",
None: None
}
return team_map[team_id]
def position_converter(position):
'''Converts a player's element_type to their actual position'''
position_map = {
1: "Goalkeeper",
2: "Defender",
3: "Midfielder",
4: "Forward"
}
return position_map[position]
def main():
all_data = loads(urlopen("https://fantasy.premierleague.com/api/bootstrap-static/").read())
players = all_data["elements"]
important_data = [
[
x["id"],
team_converter(x["team"]),
position_converter(x["element_type"])[0],
x["web_name"],
x["now_cost"] / 10,
x["total_points"]
]
for x in players
]
with open("players_data.csv", "w", encoding="utf-8", newline="") as out:
headers = ["id", "team", "pos", "name", "cost", "points"]
w = writer(out)
w.writerow(headers)
w.writerows(important_data)
if __name__ == "__main__":
main()