def get_next_3_day_games(): now = datetime.now() today = "%d_%02d_%02d" % (int(now.year), int(now.month), int(now.day)) three_day_after = now + timedelta(7) today_plus_3_day = "%d_%02d_%02d" % (int( three_day_after.year), int( three_day_after.month), int(three_day_after.day)) games = [ g for g in find_games({"date": { "$gte": today, "$lt": today_plus_3_day }}, connection=client) ] for i, game in enumerate(games): games[i]["team_H"] = find_team_by_id(game["team_H"], client) games[i]["team_A"] = find_team_by_id(game["team_A"], client) competitions_name = [(find_competition_by_id(div, client), div) for div in set([g["division"] for g in games])] games_by_competitions = { str(div): { "name": k["name"], "games": [] } for (k, div) in competitions_name if "name" in k } for game in games: games_by_competitions[str(game["division"])]["games"].append(game) return games_by_competitions
def export_season(division, years, fields=["_id", "team_H", "team_A", "date", "score.final.home", "score.final.away", "shots.home", "shots.away", "possession.home", "possession.away"]): season = [flatten_game(x) for x in find_games({"division": division, "season": years}, connection=client)] # print season[0] with open("data/seasons/" + "_".join([str(division), str(years[0]), str(years[1])]) + ".csv", "w") as f: for i, game in enumerate(season): if i == 0: f.write(";".join([unicode(field).encode('utf-8') for field in fields]) + "\n" ) f.write(";".join([(unicode(game[field]).encode('utf-8') if field in game else str(None)) for field in fields]) + "\n" ) f.close()
def load_game_id_features(game_id, features, mongolab=False): client = get_db("prono", mongolab=mongolab) game = find_game({"$query": {"_id": ObjectId(game_id)}}, connection=client) constructor_funct = { "last_seven_results": get_last_seven_results, "ranking": get_rankings, "last_seven_results_home_pov": get_last_seven_results_home_pov, "day": get_day, "last_seven_home_or_away": get_last_games_home_or_away, "last_seven_goals": last_seven_goals, "last_seven_shots": get_last_games_shots, "last_seven_goals_per_shots": get_last_games_goals_per_shots, "last_seven_rankings": get_last_seven_ranking, "last_seven_opponents_rankings": get_last_seven_opponent_ranking, "points": get_points } line = list() last_games = find_games( { "$query": { "division": ObjectId(game["division"]), "season": [str(game["season"][0]), str(game["season"][1])], "date": { "$lt": game["date"] } } }, connection=client) print game["date"] last_games = [l_g for l_g in last_games] for i, g in enumerate(last_games): if "final" not in g["score"]: last_games[i]["score"] = {"final": {"home": "2", "away": "2"}} for feature in features: f = constructor_funct[feature](game, last_games) if f is None: return None line += f # print line return line
clientMDB = get_mongolab_db("prono") def get_season_from_date(date): year = date[0:4] return [year, str(int(year) + 1)] # compet = add_competition({"name": "primera division", "region": "argentina"}) compet = find_competition_by_name("primera division") # compet = find_competition_by_name("brasileirao a") # brazil_games = [g for g in find_games({"link":{"$regex": "/brazil/serie-a"}})] brazil_games = [ g for g in find_games({"link": { "$regex": "/argentina/primera-division" }}) ] games_nb = len(brazil_games) print games_nb for game in brazil_games: new_game = { "division": compet["_id"], "season": get_season_from_date(game["date"]) } update_game({"link": game["link"]}, new_game, clientMDB) # print game["division"]
from betlib.mongo import get_db from betlib.models.game import find_games, update_game client = get_db("prono", mongolab=True) for game in find_games({}, client): if "link" in game: a = game["link"].split("?ICID=") if len(a) > 1: print game["link"] update_game(game, {"link": a[0]}, connection=client) # db.games.ensureIndex({link: 1}, {unique:true, dropDups: true})
def buildDataset(srcs, features, with_odds, mongolab=False): client = get_db("prono", mongolab=mongolab) game_issues = {-1: [0, 0, 1], 0: [0, 1, 0], 1: [1, 0, 0]} # print srcs, features # print "=" * 30 # sys.exit(-1) data_X_all = list() data_Y_all = list() odds_all = list() for competition_range in srcs: competition = competition_range[0] begin = competition_range[1] end = competition_range[2] for season in range(int(begin[0:4]), int(end[0:4])): if len(begin) == 4 and len(end) == 4: games = [ g for g in find_games( { "$query": { "division": ObjectId(competition), "season": [str(season), str(season + 1)] }, "$orderby": { "date": 1 } }, connection=client) ] else: games = [ g for g in find_games( { "$query": { "division": ObjectId(competition), "season": [str(season), str(season + 1)] }, "date": { "$gte": begin, "$lt": end }, "$orderby": { "date": 1 } }, connection=client) ] for current_game in games: sub_games = [ g for g in games if g["date"] < current_game["date"] ] new_row = construct_row(current_game, sub_games, features) if new_row is None: continue data_X_all.append(new_row[0:-1]) data_Y_all.append(game_issues[new_row[-1]]) if (with_odds is True): odds_all.append([ float(current_game["bets"]["bet365"]["home"]), float(current_game["bets"]["bet365"]["draw"]), float(current_game["bets"]["bet365"]["away"]) ]) else: odds_all.append(None) return data_X_all, data_Y_all, odds_all
from betlib.models.game import find_games import os if len(os.sys.argv) != 4: print "Usage : python 5.... division year1 year2" print "Exemple : python 5.... \"Premier League\" 2010 2011" os.sys.exit(-1) division = os.sys.argv[1] years = [os.sys.argv[2], os.sys.argv[3]] season = [game for game in find_games({"$query": {"season": years, "division": division}, "$orderby": {"date": 1}})] ret = list() ranking = {} for game in season: if game["team_H"] not in ranking: ranking[game["team_H"]] = 0 if game["team_A"] not in ranking: ranking[game["team_A"]] = 0 if int(game["score"]["final"]["home"]) > int(game["score"]["final"]["away"]): ranking[game["team_H"]] += 3 elif int(game["score"]["final"]["home"]) < int(game["score"]["final"]["away"]): ranking[game["team_A"]] += 3 else: ranking[game["team_H"]] += 1 ranking[game["team_A"]] += 1
from betlib.models.team import find_team_by_link, add_team_alias, add_link_alias, remove_team links_to_merge = { # FROM : TO "/teams/brazil/sao-paulo-futebol-clube/302/": "/teams/brazil/sao-paulo/33109/" #Sao Paulo } for link in links_to_merge: team = find_team_by_link(link) main_team = find_team_by_link(links_to_merge[link]) if team is None or team["_id"] == main_team["_id"]: continue add_team_alias(main_team["name"], team["name"]) add_link_alias(main_team["link"], team["link"]) for g in find_games({"team_H": team["_id"]}): update_game({"_id": g["_id"]}, {"team_H": main_team["_id"]}) for g in find_games({"team_A": team["_id"]}): update_game({"_id": g["_id"]}, {"team_A": main_team["_id"]}) remove_team({"_id": team["_id"]}) # mettre a jour le main team [OK] # mettre a jour tous les matchs avec link_to_merge [OK] # supprimer la team a merger [OK] # updater find_team_by_link [OK]
def update_game_in_db_from_soccerway(): client = get_db("prono", mongolab=True) now = datetime.now() + timedelta(5) today = "%d_%02d_%02d" % (int(now.year), int(now.month), int(now.day)) three_day_ago = datetime.now() - timedelta(5) today_minus_3_day = "%d_%02d_%02d" % (int( three_day_ago.year), int(three_day_ago.month), int(three_day_ago.day)) print today_minus_3_day print today games = [ g for g in find_games( {"date": { "$lte": today, "$gte": today_minus_3_day }}, connection=client) ] # games = [g for g in find_games({}, connection=client)] # all games print len(games) for game in games: if "link" in game: # print "http://fr.soccerway.com" + game["link"] if game["link"].startswith("http"): updated = game_page.get_page(game["link"]) else: updated = game_page.get_page("http://fr.soccerway.com" + game["link"]) # print updated.get("date", "") if updated is not None: if updated["scores"] is not None: if "half-time" in updated["scores"]: half_time = updated["scores"]["half-time"].split(" - ") scores = { "half_time": { "home": half_time[0], "away": half_time[1] } } else: half_time = None scores = dict() if "final" in updated["scores"]: final = updated["scores"]["final"].split(" - ") scores["final"] = {"home": final[0], "away": final[1]} else: final = None scores = None if updated["years"] is not None: updated["years"] = updated["years"].split("/") ret = update_game( {"link": game["link"]}, { "score": scores, "stadium": updated["stadium"], "region": updated["country"], "start_time": updated["start_time"], "season": updated["years"], "date": updated.get("date", None), "possession": updated.get("possession", None), "corners": updated.get("corners", None), "shots": updated.get("tirs", None), "shots_on_target": updated.get("tirs_cadres", None), "fouls": updated.get("fautes", None) }, verbose=True, connection=client) if ret is None: print game["link"], "is up to date in db." else: print "[success]", game["_id"]