-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_parser.py
148 lines (122 loc) · 5.84 KB
/
json_parser.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from datetime import date
import urllib, urllib2, json
import unicodedata
TEAMS_ABREVIATIONS = {'Palmeiras-SP':'PAL','Internacional-RS':'INT','Gremio-RS':'GRE','Corinthians-SP':'COR',\
'Sao Paulo-SP':'SAO','Flamengo-RJ':'FLA','Chapecoense-SC':'CHA','Santos-SP':'SAN','Atletico-PR':'CAP',\
'Ponte Preta-SP':'PON','Fluminense-RJ':'FLU','Figueirense-SC':'FIG','Santa Cruz-PE':'STA','Atletico-MG':'CAM',\
'Vitoria-BA':'VIT','Coritiba-PR':'CFC','Sport-PE':'SPO','Botafogo-RJ':'BOT','Cruzeiro-MG':'CRU','America-MG':'AME',\
'Atletico-MG-MG':'CAM'}
MATCH_STATUS = {'encerrados':'encerrado(s)', 'em_andamento':'em andamento', 'agendados':'agendado(s)'}
LINE_SEPARATOR = "\n---------------------------------------\n"
LINE_SEPARATOR_CLASSIFICATION = "\n-----------------------\n"
NUMBER_OF_STRIKERS = 5
def getClassification():
urlClassification = "http://www.futebolinterior.com.br/json/Classificacao/getClassificacao?id_ano=585&id_fase=&rodada="
response = urllib.urlopen(urlClassification)
data = json.loads(response.read())
position = 1
strClassification = "|" + '{:^3}'.format('POS') + "|" + '{:^5}'.format('TIME') + "|" + '{:^3}'.format('P') + "|"\
+ '{:^3}'.format('V') + "|" + '{:^3}'.format('SG') + "|" + LINE_SEPARATOR_CLASSIFICATION
for team in data['classificacao']:
strClassification += "|" + '{:>3}'.format(str(position)) + "|" +\
'{:^5}'.format(TEAMS_ABREVIATIONS[unicodedata.normalize('NFKD', team['clube']).encode('ascii','ignore')]) +\
"|" + '{:>3}'.format(team['pg']) + "|" + '{:>3}'.format(team['vi']) + "|" + '{:>3}'.format(team['sg']) +\
"|" + LINE_SEPARATOR_CLASSIFICATION
position += 1
return strClassification
def getFixture(fixture=""):
urlFixture = "http://www.futebolinterior.com.br/json/Agenda/getJogos?id_ano=585&id_fase=2168&rodada=" + fixture + "&por_grupo=1&id_grupo=-1&hash=fase%3DUnica%26rodada%3D6"
response = urllib.urlopen(urlFixture)
data = json.loads(response.read())
if (fixture == ""):
fixture = data["rodada"]
strFixture = "Rodada " + fixture + ":" + LINE_SEPARATOR
for match in data['jogos']:
if (match['status'] == 'Agendado'):
strFixture += match['mandante'] + " x " + match['visitante'] + "\n"
strFixture += match['datahora'] + "\n"
strFixture += "Agendado"
elif (match['status'] == 'Encerrado'):
strFixture += match['mandante'] + " " + match['placar'] + " " + match['visitante'] + "\n"
strFixture += match['datahora'] + "\n"
strFixture += "Encerrado"
else:
strFixture += match['mandante'] + " " + match['placar'] + " " + match['visitante'] + "\n"
strFixture += match['datahora'] + "\n"
strFixture += "Em andamento"
strFixture += LINE_SEPARATOR
return strFixture
def getTopScorers():
urlTopScorers = "http://www.futebolinterior.com.br/futebol/Brasileiro/Serie-A/2016/artilharia"
opener = urllib2.build_opener()
opener.addheaders = [('Accept-Charset', 'utf-8')]
url_response = opener.open(urlTopScorers)
content = url_response.read().decode('utf-8')
soup = BeautifulSoup(content, 'html.parser')
divStrikers = soup.find_all("div", {"class": "col-md-8"})[2]
strikersList = divStrikers.find_all('p')
finalStrikers = []
for i in range (1, NUMBER_OF_STRIKERS):
strikersList[i] = str(strikersList[i]).replace('<p>','')
strikersList[i] = strikersList[i].replace('</p>','')
strikersList[i] = strikersList[i].replace('<strong>','')
strikersList[i] = strikersList[i].replace('</strong>','')
strikersList[i] = strikersList[i].replace('<br/>','\n')
strikersList[i] = strikersList[i].strip()
if strikersList[i].endswith('\n'):
strikersList[i] = strikersList[i][:-2]
if strikersList[i] != '':
finalStrikers.append(strikersList[i])
strStrikers = "Artilheiros do Brasileirão 2016"
for i in finalStrikers:
strStrikers += LINE_SEPARATOR
strStrikers += i
return strStrikers
def getMatchesByStatus(status):
urlFixture = "http://www.futebolinterior.com.br/json/Agenda/getJogos?id_ano=585&id_fase=2168&rodada=&por_grupo=1&id_grupo=-1&hash=fase%3DUnica%26rodada%3D6"
response = urllib.urlopen(urlFixture)
data = json.loads(response.read())
fixture = data["rodada"]
strFixture = "Jogo(s) " + MATCH_STATUS[status] + " da rodada " + data["rodada"] + ":" + LINE_SEPARATOR
strInitial = strFixture
if (status == 'encerrados'):
for match in data['jogos']:
if (match['status'] == 'Encerrado'):
strFixture += match['mandante'] + " " + match['placar'] + " " + match['visitante'] + "\n"
strFixture += match['datahora']
strFixture += LINE_SEPARATOR
elif (status == 'agendados'):
for match in data['jogos']:
if (match['status'] == 'Agendado'):
strFixture += match['mandante'] + " x " + match['visitante'] + "\n"
strFixture += match['datahora']
strFixture += LINE_SEPARATOR
else:
for match in data['jogos']:
if (match['status'] != 'Encerrado') and (match['status'] != 'Agendado'):
strFixture += match['mandante'] + " " + match['placar'] + " " + match['visitante'] + "\n"
strFixture += match['datahora']
strFixture += LINE_SEPARATOR
if strFixture == strInitial:
return "Sem jogo(s) " + MATCH_STATUS[status] + " na rodada " + fixture
else:
return strFixture
def getMatchesOfTheDay():
today = str(date.today().day).zfill(2) + "/" + str(date.today().month).zfill(2) + "/" + str(date.today().year)
urlFixture = "http://www.futebolinterior.com.br/json/Agenda/getJogos?id_ano=585&id_fase=2168&rodada=&por_grupo=1&id_grupo=-1&hash=fase%3DUnica%26rodada%3D6"
response = urllib.urlopen(urlFixture)
data = json.loads(response.read())
strMatches = "Jogo(s) de hoje" + LINE_SEPARATOR
strInitial = strMatches
for match in data['jogos']:
if (match['datahora'].split(" ")[0] == today):
strMatches += match['mandante'] + " x " + match['visitante'] + "\n"
strMatches += match['datahora']
strMatches += LINE_SEPARATOR
if strMatches == strInitial:
return "Sem jogo(s) hoje"
else:
return strMatches