-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (59 loc) · 2.35 KB
/
main.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
import json
from urllib.request import urlopen
from bs4 import BeautifulSoup
def getStatusFightsbyFighter(fighter):
fighterResponse = urlopen(
"http://ufc-data-api.ufc.com/api/v1/us/fighters/" + str(fighter["id"])).read()
soup = BeautifulSoup(fighterResponse, 'html.parser')
fightsStatuses = soup.findAll("img", {"class": "fight-result-flag"})
fighter["statuses"] = []
for fightsStatus in fightsStatuses:
fighter["statuses"].append(fightsStatus.get('alt', ''))
def getFighterScore(fighter):
fighter["score"] = fighter["wins"] - fighter["losses"]
wins_count = 0
for status in fighter["statuses"]:
if status == "win":
wins_count += 1
else:
break
fighter["score"] += wins_count
if wins_count > 2 and fighter["title_holder"]:
fighter["score"] += 2
fightersResponse = urlopen(
"http://ufc-data-api.ufc.com/api/v1/us/fighters").read().decode('utf8')
fighters = json.loads(fightersResponse)
eventsResponse = urlopen(
"http://ufc-data-api.ufc.com/api/v1/us/events").read().decode('utf8')
events = json.loads(eventsResponse)
last_event = urlopen(
"http://ufc-data-api.ufc.com/api/v1/us/events/" + str(events[0]["id"])).read()
soup = BeautifulSoup(last_event, 'html.parser')
reds = soup.findAll("h1", {"class": "fighter-name-red"})
blues = soup.findAll("h1", {"class": "fighter-name-blue"})
fights = []
for i in range(0, len(reds)):
fight = {}
red_last_name = reds[i].getText()[1:]
blue_last_name = blues[i].getText()[1:]
for fighter in fighters:
if fighter["last_name"] == red_last_name:
getStatusFightsbyFighter(fighter)
getFighterScore(fighter)
fight["red"] = fighter
elif fighter["last_name"] == blue_last_name:
getStatusFightsbyFighter(fighter)
getFighterScore(fighter)
fight["blue"] = fighter
fights.append(fight)
print (fights[0])
for fight in fights:
try:
red = (fight["red"]["score"] * 100) / \
(fight["red"]["score"] + fight["blue"]["score"])
blue = (fight["blue"]["score"] * 100) / \
(fight["red"]["score"] + fight["blue"]["score"])
print(fight["red"]["last_name"] + ":" + str(round(red, 2)) +
"------" + fight["blue"]["last_name"] + ":" + str(round(blue, 2)))
except:
print(fight, "\n")