-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (43 loc) · 1.76 KB
/
main.py
File metadata and controls
57 lines (43 loc) · 1.76 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
import requests
def print_players(players):
for p in players:
print(f"{p['rank']:>3}. GM {p['name']} ({p['country']}) - {p['rating']} Fide id: {p['fide_id']}")
def get_top_players(limit=10, history=False):
url = f"https://api.chesstools.org/fide/top_active/?limit={limit}&history={str(history).lower()}"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Error {response.status_code}: {response.text}")
def search_player_name():
name=input("Enter name of player: ")
url = f"https://api.chesstools.org/ratinglist/search?query={name}&list_type=fide"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
players_sorted = sorted(data, key=lambda p: p['rating'], reverse=True)
for players in players_sorted:
print(f"{players['title']}, {players['name']} ({players['country']}) - {players['rating']} Fide id: {players['fideid']}")
else:
print(f"{response.status_code} {response.text}")
def search_player_fideid():
id=input("enter player fide id: ")
url=f"https://api.chesstools.org/fide/{id}"
response =requests.get(url)
if response.status_code == 200:
data = response.json()
print(f"{data['title']}, {data['name']} ({data['country']}) - {data['rating']}")
else:
print(f"{response.status_code} {response.text}")
def Start():
run=input("1. Get Top Players\n2. Search by Name\n3. Search by fide id\n ")
if run=="1":
players= get_top_players()
print_players(players)
elif run=="2":
players=search_player_name()
elif run=="3":
players=search_player_fideid()
else:
return
Start()