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 syncTeams(self,league=DEFAULT_LEAGUE,season=getCurrentSeason()): ''' @league: string containing shortcut of league (e.g. 'bl1') @season: int representing the season request (e.g. 2011) @returns: None This method is responsible for querying openligadb.de for data for all teams in the requested league and requested season. This data is the stored in the local database. This method relies on the syncLeagues method being run beforehand as this method is responsible for creating the team->league relationship ''' session=Session() team = oldb.GetTeamsByLeagueSaison(league,season) league = localService.getLeagueByShortcutSeason(league, season,ret_dict=False) teams = team.Team for team in teams: if teamShortcuts.has_key(int(team.teamID)): shortName = teamShortcuts[int(team.teamID)] else: # mail admin - should add the shortcut to teamShortcuts shortName = None t = session.merge(Team(int(team.teamID),team.teamName.encode('utf-8'), shortName=shortName,iconURL=team.teamIconURL)) session.commit() session.close()
def syncSeasonMatches(self,league=DEFAULT_LEAGUE,season=getCurrentSeason()): ''' ''' matchdata = oldb.GetMatchdataByLeagueSaison(league,season) matches = matchdata.Matchdata for match in matches: self.syncMatch(match)
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 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 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 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}