forked from fatiherikli/worldcup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worldcup.py
executable file
·180 lines (144 loc) · 4.22 KB
/
worldcup.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import sys
import json
import datetime
import colorama
import humanize
import dateutil.parser
import dateutil.tz
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
FUTURE = "future"
NOW = "now"
PAST = "past"
SCREEN_WIDTH = 68
def progress_bar(percentage, separator="o", character="-"):
"""
Creates a progress bar by given percentage value
"""
filled = colorama.Fore.GREEN + colorama.Style.BRIGHT
empty = colorama.Fore.WHITE + colorama.Style.BRIGHT
if percentage == 100:
return filled + character * SCREEN_WIDTH
if percentage == 0:
return empty + character * SCREEN_WIDTH
completed = int((SCREEN_WIDTH / 100.0) * percentage)
return (filled + (character * (completed - 1)) +
separator +
empty + (character * (SCREEN_WIDTH - completed)))
def prettify(match):
"""
Prettifies given match object
"""
diff = (datetime.datetime.now(tz=dateutil.tz.tzlocal()) -
dateutil.parser.parse(match['datetime']))
seconds = diff.total_seconds()
if seconds > 0:
if seconds > 60 * 90:
status = PAST
else:
status = NOW
else:
status = FUTURE
if status in [PAST, NOW]:
color = colorama.Style.BRIGHT + colorama.Fore.GREEN
else:
color = colorama.Style.NORMAL + colorama.Fore.WHITE
home = match['home_team']
away = match['away_team']
if status == NOW:
minute = int(seconds / 60)
match_status = "Being played now: %s minutes gone" % minute
elif status == PAST:
if match['winner'] == 'Draw':
result = 'Draw'
else:
result = "%s won" % (match['winner'])
match_status = "Played %s. %s" % (humanize.naturaltime(diff),
result)
else:
match_status = "Will be played %s" % humanize.naturaltime(diff)
if status == NOW:
match_percentage = int(seconds / 60 / 90 * 100)
elif status == FUTURE:
match_percentage = 0
else:
match_percentage = 100
return u"""
{} {:<30} {} - {} {:>30}
{}
\u26BD {}
""".format(
color,
home['country'],
home['goals'],
away['goals'],
away['country'],
progress_bar(match_percentage),
colorama.Fore.WHITE + match_status
)
def group_list(country):
"""
Lists a group member
"""
return """
{:<5} \t\t| wins: {} | losses: {} | goals for: {} | goals against: {} | out? {}
{}
""".format(
country['country'],
country['wins'],
country['losses'],
country['goals_for'],
country['goals_against'],
country['knocked_out'],
"-" * SCREEN_WIDTH
)
def is_valid(match):
"""
Validates the given match object
"""
return (
isinstance(match, dict) and
isinstance(match.get('home_team'), dict) or
isinstance(match.get('away_team'), dict) or
isinstance(match.get('group_id'), int)
)
def fetch(endpoint):
"""
Fetches match results by given endpoint
"""
url = "http://worldcup.sfg.io/%(endpoint)s" % {
"endpoint": endpoint
}
data = urlopen(url).read().decode('utf-8')
#print data
matches = json.loads(data)
#print matches
for match in matches:
if is_valid(match):
yield match
def main():
colorama.init()
endpoint = 'matches/'+''.join(sys.argv[1:])
# todo: use argument parser
#if len(sys.argv)==1:
#endpoint = 'matches'
if len(sys.argv) > 1:
if (sys.argv[1].lower() == 'country'):
endpoint = 'matches/country?fifa_code=%(country)s' % {
"country": sys.argv[2].upper()
}
elif (sys.argv[1].lower() == 'group'):
endpoint = 'group_results'
group_id = int(sys.argv[2])
for match in fetch(endpoint):
if (match.get('group_id') == group_id):
print group_list(match)
return
for match in fetch(endpoint):
print(prettify(match).encode('utf-8'))
if __name__ == "__main__":
main()