-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfb.py
80 lines (70 loc) · 2.6 KB
/
cfb.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
import json, urllib2
APIBASE='https://my.cfbevents.com'
def getCFBJSONData(url):
html = urllib2.urlopen(APIBASE+url)
root = json.loads(html.read())
if not 'status' in root or not 'data' in root:
raise Exception("Requesting CFB show list failed")
if root['status'] != 'success':
raise Exception("Requesting CFB show list returned failure")
return root
class CFBTournament(object):
cachedPairingsData = {}
cachedDecklistData = {}
@classmethod
def getCachedPairingsData(cls, id, url):
if not id in cls.cachedPairingsData:
cls.cachedPairingsData[id] = getCFBJSONData(url) if url else None
return cls.cachedPairingsData[id]
@classmethod
def getCachedDecklistData(cls, id, url):
if not id in cls.cachedDecklistData:
cls.cachedDecklistData[id] = getCFBJSONData(url)['data'] if url else None
return cls.cachedDecklistData[id]
def __init__(self, json):
self.id=json['id']
self.name=json['name']
self.pairingsurl=json.get('pairings_url', None)
self.decklistsurl=json.get('decklist_list_url', None)
def getPairingsURL(self): return self.pairingsurl
def getDecklistsURL(self): return self.decklistsurl
def getName(self): return self.name
def getId(self): return self.id
def getRound(self):
data = CFBTournament.getCachedPairingsData(self.id, self.pairingsurl)
return int(data['current_round'])
def getListURLForPlayer(self, name):
data = CFBTournament.getCachedDecklistData(self.id, self.decklistsurl)
candidates=[]
for i in data:
if i['name'].strip().lower() == name.strip().lower():
candidates.append( (i['created_at'], APIBASE+'/tools/deck/print/%s'%i['id']) )
candidates.sort()
if len(candidates) > 0:
return candidates[-1][1]
return None
def getPlayersWithDecklists(self):
data = CFBTournament.getCachedDecklistData(self.id, self.decklistsurl)
return set([x['name'].strip() for x in data])
def getPairings(self):
data = CFBTournament.getCachedPairingsData(self.id, self.pairingsurl)
deckdata = CFBTournament.getCachedDecklistData(self.id, self.decklistsurl)
roundnum=self.getRound()
return [
(roundnum,
i['table'],
(i['player']['name'], '', i['player']['points'])
) for i in data['data']]
class CFBShow(object):
def __init__(self, json):
self.id=json['id']
self.name=json['name']
self.tournamenturl=json['url_tournament_list']
def getName(self): return self.name
def getID(self): return self.id
def getTournaments(self):
root = getCFBJSONData(self.tournamenturl)
return [CFBTournament(x) for x in root['data'] if x['format'] != 'Package']
def getCFBShows(url='/api/json/'):
root = getCFBJSONData(url)
return [CFBShow(x) for x in root['data']]