def getMatchesByMatchday(self,matchday=getCurrentMatchDay(), league=getDefaultLeague(),season=getCurrentSeason(),ret_dict=True): ''' @matchday: int representing bundesliga matchday (e.g. max 34) @league: string representing League shortcut (e.g. 'bl1') @season: int representing season year (e.g. 2011) @returns: list of dictionaries representing Match objects or list oft Match objects if caller uses ret_dict=False ''' session = Session() try: matches = session.query(Match).join(League).filter(and_(Match.matchDay==matchday, League.shortcut==league, League.season==season)).all() except: raise else: if ret_dict: matchlist = [] for match in matches: matchlist.append(self.dictifier.dictifyMatch(match)) return matchlist else: return matches finally: session.close()
def GET(self): web.header('Content-Type','application/json') season = web.input(season=None) league = web.input(league=None) matchday = web.input(matchday=None) season = season.season league = league.league matchday = matchday.matchday pointslist = [] if not league: league = getDefaultLeague() if not season: season = getCurrentSeason() if not matchday: matchday = getCurrentMatchDay() try: teams = api.getTeams(league=league,season=season,ret_dict=False) except: return json.dumps({'error':'could not return team data'}) else: for team in teams: ppt = api.getPointsPerTeam(team.id) pointslist.append(ppt) # TODO this is not ideal as it doesn't take goal diff into account plist = sorted(pointslist, key=itemgetter('points')) plist.reverse() return json.dumps(plist)
def getMatchesInProgressAsOf(self,tstamp=datetime.now(), league=getDefaultLeague(),ret_dict=True): ''' @tstamp: datetime representing tstamp as of which requestor wants to know if a match is in progress. This method takes a datetime and checks if any Match has a startTime from datetime-100 minutes to datetime. The 100 minutes is a randomly chosen time which generally will address all bundesliga games. ''' from datetime import timedelta matchdelta = timedelta(minutes=-100) earlylimit = tstamp+matchdelta session = Session() try: mip = session.query(Match).join(League).filter(\ and_(Match.startTime>=earlylimit,League.shortcut==league, Match.startTime<=tstamp)).all() except: raise else: if ret_dict: matchlist = [] for m in mip: matchlist.append(self.dictifier.dictifyMatch(m)) return matchlist else: return mip finally: session.close()
def getLastUpstreamChange(self,league=getDefaultLeague(), season=getCurrentSeason()): ''' @league: string representing shortname of league (e.g. 'bl1') @season: int representing year (e.g. 2011) @returns: datetime of last change of upstream database ''' lastchange = oldb.GetLastChangeDateByLeagueSaison(league,season) return lastchange
def getGoalsSinceID(self,goal_id,league=getDefaultLeague(),ret_dict=True): session = Session() goals = session.query(Goal).join(Match).join(League).filter(and_(\ Goal.id>goal_id,League.shortcut == league)).all() if ret_dict: goallist = [] for g in goals: goallist.append(self.dictifier.dictifyGoal(g)) return goallist else: return goals session.close()
def getMaxGoalID(self,league=getDefaultLeague()): ''' @league: string representing shortcut of League object (e.g. 'bl1') @returns: int of maxid in Goal table for league ''' session = Session() maxgoal = session.query(Goal).join(Match).\ join(League).filter(League.shortcut == league).order_by(\ Goal.id.desc()).all() session.close() if not len(maxgoal): raise StandardError, 'No goals found' else: return int(maxgoal[0].id)
def GET(self): web.header('Content-Type','application/json') league = web.input(league=None) league = league.league if not league: league = getDefaultLeague() try: max_goal_id = api.getMaxGoalID(league) except: return json.dumps({'error':'could not retrieve max\ goal id for %s'%league}) else: return json.dumps({'max_goal_id':max_goal_id})
def getGoalsPerMatchWithFlags(self,max_goal_id,league=getDefaultLeague()): mip = self.getMatchesInProgressNow(league=league) newgoals = self.getGoalsSinceID(max_goal_id,league,ret_dict=False) matches = {} for m in mip: if not matches.has_key(m['id']): matches[m['id']] = [] for goaldict in m['goals']: if goaldict['id'] in [x.id for x in newgoals]: goaldict['isNew'] = True else: goaldict['isNew'] = False matches[m['id']].append(goaldict) return matches
def GET(self): web.header('Content-Type','application/json') league = web.input(league=None) league = league.league if not league: league = getDefaultLeague() clientmaxgoal = web.input(max_goal_id=None) clientmaxgoal = clientmaxgoal.max_goal_id if not clientmaxgoal: return json.dumps({'error':'missing parameter max_goal_id'}) else: matches = api.getGoalsPerMatchWithFlags(clientmaxgoal,league=league) matches['max_goal_id'] = api.getMaxGoalID(league=league) return json.dumps(matches)
def GET(self,tstamp=None): web.header('Content-Type','application/json') league = web.input(league=None) league = league.league if not league: league = getDefaultLeague() if not tstamp: mip = api.getMatchesInProgressNow() return json.dumps(mip) else: try: tstamp = datetime.strptime(tstamp,"%Y-%m-%d-%H-%M") except: return json.dumps({ 'error':'%s is an invalid tstamp.'%tstamp}) else: mip = api.getMatchesInProgressAsOf(tstamp) return json.dumps(mip)
def getMatchesSinceGoalID(self,maxid,league=getDefaultLeague(),ret_dict=True): ''' @maxid: int representing the highest goal id that the requestor currently has. @league: filter only goals from a certain League @returns: list of dictionaries representing Goals entered into local database since maxid ''' session = Session() matches = session.query(Match).join(League).join(Goal).filter(and_( Goal.id > maxid,League.shortcut==league)).all() if ret_dict: matchlist = [] for match in matches: matchlist.append(self.dictifier.dictifyMatch(match)) return matchlist else: return matches session.close()
def getLeagueByShortcutSeason(self,league=getDefaultLeague(), season=getCurrentSeason(),ret_dict=True): ''' @league: string representing shortcut of League (e.g. 'bl1') @season: int representing season year (e.g. 2011) @returns: dictionary representing League object, or the League object itself if caller uses ret_dict = False ''' session = Session() try: league = session.query(League).filter(and_(League.shortcut==league, League.season==season)).one() except: raise else: if ret_dict: return self.dictifier.dictifyLeague(league) else: return league finally: session.close()
def test(self,matchday,league=getDefaultLeague(),season=getCurrentSeason()): start=time.time() session=Session() if season > getCurrentSeason(): raise StandardError, "not possible to query future season tables" elif season == getCurrentSeason() and matchday > getCurrentMatchDay(): raise StandardError, "not possible to query future match days" else: teams = {} matches = session.query(Match).join(League).filter(\ and_(League.season==season, League.shortcut==league,Match.matchDay<=matchday)).all() for match in matches: t1,t2 = match.team1.id,match.team2.id if not teams.has_key(t1): teams[t1] = {'gf':0,'ga':0,'points':0,'w':0,'l':0,'d':0,} if not teams.has_key(t2): teams[t2] = {'gf':0,'ga':0,'points':0,'w':0,'l':0,'d':0,} if len(match.team1goals) > len(match.team2goals): teams[t1]['points']+=3 teams[t1]['w']+=1 teams[t2]['l']+=1 elif len(match.team1goals) < len(match.team2goals): teams[t2]['points']+=3 teams[t2]['w']+=1 teams[t1]['l']+=1 else: teams[t1]['points']+=1 teams[t2]['points']+=1 teams[t1]['d']+=1 teams[t2]['d']+=1 teams[t1]['gf']+=len(match.team1goals) teams[t2]['gf']+=len(match.team2goals) teams[t1]['ga']+=len(match.team2goals) teams[t2]['ga']+=len(match.team1goals) end=time.time() took = end-start print "%.3f seconds"%took return teams session.close()
def getPointsPerTeam(self,team_id,league=getDefaultLeague(), season=getCurrentSeason(),matchday=getCurrentMatchDay()): session=Session() matches = session.query(Match).join(League).filter(\ and_(League.shortcut==league, League.season==season,Match.matchDay<=matchday, or_(Match.matchTeam1==team_id, Match.matchTeam2==team_id))).all() goals_for,goals_against,points,won,drew,lost = 0,0,0,0,0,0 for match in matches: if match.matchTeam1 == team_id: goals_for += len(match.team1goals) goals_against += len(match.team2goals) if len(match.team1goals) > len(match.team2goals): points+=3 won+=1 elif len(match.team1goals) == len(match.team2goals): points+=1 drew+=1 else: lost+=1 elif match.matchTeam2 == team_id: goals_for += len(match.team2goals) goals_against += len(match.team1goals) if len(match.team2goals) > len(match.team1goals): points+=3 won+=1 elif len(match.team2goals) == len(match.team1goals): points+=1 drew+=1 else: lost+=1 else: raise StandardError, "wtf?! Cannot be..." session.close() return {'goals_for':goals_for,'goals_against':goals_against, 'points':points,'won':won,'drew':drew,'lost':lost, 'played':len(matches), 'difference':goals_for-goals_against,'team_id':team_id}
def getMatchesInProgressNow(self,league=getDefaultLeague(),ret_dict=True): ''' A simple method to find out from the local database which matches are in progress at the moment (if any) @returns: List of Match dictionaries or list of Match objects if the caller uses ret_dict = False ''' session=Session() try: mip = session.query(Match).join(League).filter(\ and_(Match.isFinished!=True,Match.startTime<=datetime.now(), League.shortcut==league)).all() except: raise else: if ret_dict: matchlist = [] for m in mip: matchlist.append(self.dictifier.dictifyMatch(m)) return matchlist else: return mip finally: session.close()