Exemplo n.º 1
0
def test(gameid):
    allplayers = {}
    events = set()
    worthtimestamps = {}
    timestamps = json.loads(api_calls.get_game_timestamps(gameid))
    cont = True
    game = pbpmodels.Game.objects.values("dateTime", "endDateTime").get(gamePk=gameid)
    startDateTime = datetime.strftime(game["dateTime"], "%Y%m%d_%H%M%S")
    endDateTime = datetime.strftime(game["endDateTime"], "%Y%m%d_%H%M%S")
    eventIds = pbpmodels.PlayByPlay.objects.values_list("eventId", flat=True).filter(gamePk_id=gameid)
    for x in xrange(1, len(timestamps)):
        start = timestamps[x - 1]
        end = timestamps[x]
        if start <= startDateTime and end <= endDateTime:
            print "huzzah"
            j = api_calls.get_game_diff(gameid, start, end)
            if "eventId" in j:
                j = json.loads(j)
                for diff in j:
                    if "diff" in diff:
                        paths = diff["diff"]
                        for path in paths:
                            if "eventId" in path["path"] and "eventIdx" not in path["path"]:
                                if path["value"] in eventIds:
                                    worthtimestamps[path["value"]] = end
    worhttimestamps = worthtimestamps.values()
    """for pbp in pbpmodels.PlayByPlay.objects.filter(gamePk_id=gameid):
        if pbpmodels.PlayByPlayPlayers.objects.filter(play=pbp).exists():
            print "already exists..."
            cont = False
            break
        dateTime = pbp.dateTime
        sdateTime = datetime.strftime(dateTime, "%Y%m%d_%H%M%S")
        found = False
        for ts in xrange(1, len(timestamps)):
            start = timestamps[ts-1]
            end = timestamps[ts]
            print start, sdateTime, end
            if start < sdateTime and end > sdateTime:
                worthtimestamps.append(start)
                found = True
                break
            elif start == sdateTime:
                worthtimestamps.append(start)
                found = True
                break
            elif end == sdateTime:
                worthtimestamps.append(end)
                found = True
                break"""
    byevents = {}
    # jsonload = None
    # print len(worthtimestamps), len(timestamps)
    if cont is True:
        print len(worthtimestamps)
        for ts in worthtimestamps:
            # forstart = datetime.now()
            j = json.loads(api_calls.get_game_at(gameid, ts))
            # forjsonload = datetime.now() - forstart
            # if jsonload is None:
            #    jsonload = forjsonload
            # else:
            #    jsonload += forjsonload
            if "currentPlay" in j["liveData"]["plays"]:
                awayplayers = j["liveData"]["boxscore"]["teams"]["away"]["onIce"]
                homeplayers = j["liveData"]["boxscore"]["teams"]["home"]["onIce"]
                eventId = j["liveData"]["plays"]["currentPlay"]["about"]["eventId"]
                results = {
                    "away": awayplayers,
                    "home": homeplayers,
                    "homeScore": j["liveData"]["plays"]["currentPlay"]["about"]["goals"]["home"],
                    "awayScore": j["liveData"]["plays"]["currentPlay"]["about"]["goals"]["away"],
                    "eventIdx": j["liveData"]["plays"]["currentPlay"]["about"]["eventIdx"],
                }
                if len(homeplayers) > 0 and len(awayplayers) > 0:
                    byevents[eventId] = results
        getPbp = None
        for eventId in byevents:
            try:
                # forstart = datetime.now()
                pbp = pbpmodels.PlayByPlay.objects.get(gamePk_id=gameid, eventId=eventId)
                pbp.homeScore = byevents[eventId]["homeScore"]
                pbp.awayScore = byevents[eventId]["awayScore"]
                # pbp.eventId = eventId
                # pbp.eventIdx = byevents[eventId]["eventIdx"]
                pbp.save()
                pbpid = pbp.id
                # forgetpbp = datetime.now() - forstart
                # if getPbp is None:
                #    getPbp = forgetpbp
                # else:
                #    getPbp += forgetpbp
                players = {}
                for h in xrange(len(homeplayers)):
                    players["h" + str(h + 1) + "_id"] = homeplayers[h]
                for a in xrange(len(awayplayers)):
                    players["a" + str(a + 1) + "_id"] = awayplayers[a]
                players["play_id"] = pbpid
                pbpp = pbpmodels.PlayByPlayPlayers(**players)
                allplayers[eventId] = pbpp
            except Exception as e:
                print gameid, eventId, e
        # create = datetime.now()
        print len(allplayers.values())
        pbpmodels.PlayByPlayPlayers.objects.bulk_create(allplayers.values())
Exemplo n.º 2
0
def test(gameid):
    allplayers = {}
    events = set()
    worthtimestamps = {}
    timestamps = json.loads(api_calls.get_game_timestamps(gameid))
    cont = True
    game = pbpmodels.Game.objects.values("dateTime",
                                         "endDateTime").get(gamePk=gameid)
    startDateTime = datetime.strftime(game["dateTime"], "%Y%m%d_%H%M%S")
    endDateTime = datetime.strftime(game["endDateTime"], "%Y%m%d_%H%M%S")
    eventIds = pbpmodels.PlayByPlay.objects.values_list(
        "eventId", flat=True).filter(gamePk_id=gameid)
    for x in xrange(1, len(timestamps)):
        start = timestamps[x - 1]
        end = timestamps[x]
        if start <= startDateTime and end <= endDateTime:
            print "huzzah"
            j = api_calls.get_game_diff(gameid, start, end)
            if "eventId" in j:
                j = json.loads(j)
                for diff in j:
                    if "diff" in diff:
                        paths = diff["diff"]
                        for path in paths:
                            if "eventId" in path[
                                    "path"] and "eventIdx" not in path["path"]:
                                if path["value"] in eventIds:
                                    worthtimestamps[path["value"]] = end
    worhttimestamps = worthtimestamps.values()
    """for pbp in pbpmodels.PlayByPlay.objects.filter(gamePk_id=gameid):
        if pbpmodels.PlayByPlayPlayers.objects.filter(play=pbp).exists():
            print "already exists..."
            cont = False
            break
        dateTime = pbp.dateTime
        sdateTime = datetime.strftime(dateTime, "%Y%m%d_%H%M%S")
        found = False
        for ts in xrange(1, len(timestamps)):
            start = timestamps[ts-1]
            end = timestamps[ts]
            print start, sdateTime, end
            if start < sdateTime and end > sdateTime:
                worthtimestamps.append(start)
                found = True
                break
            elif start == sdateTime:
                worthtimestamps.append(start)
                found = True
                break
            elif end == sdateTime:
                worthtimestamps.append(end)
                found = True
                break"""
    byevents = {}
    #jsonload = None
    #print len(worthtimestamps), len(timestamps)
    if cont is True:
        print len(worthtimestamps)
        for ts in worthtimestamps:
            #forstart = datetime.now()
            j = json.loads(api_calls.get_game_at(gameid, ts))
            #forjsonload = datetime.now() - forstart
            #if jsonload is None:
            #    jsonload = forjsonload
            #else:
            #    jsonload += forjsonload
            if "currentPlay" in j["liveData"]["plays"]:
                awayplayers = j["liveData"]["boxscore"]['teams']['away'][
                    'onIce']
                homeplayers = j["liveData"]["boxscore"]['teams']['home'][
                    'onIce']
                eventId = j["liveData"]["plays"]["currentPlay"]["about"][
                    "eventId"]
                results = {
                    "away":
                    awayplayers,
                    "home":
                    homeplayers,
                    "homeScore":
                    j["liveData"]["plays"]["currentPlay"]["about"]["goals"]
                    ["home"],
                    "awayScore":
                    j["liveData"]["plays"]["currentPlay"]["about"]["goals"]
                    ["away"],
                    "eventIdx":
                    j["liveData"]["plays"]["currentPlay"]["about"]["eventIdx"]
                }
                if len(homeplayers) > 0 and len(awayplayers) > 0:
                    byevents[eventId] = results
        getPbp = None
        for eventId in byevents:
            try:
                #forstart = datetime.now()
                pbp = pbpmodels.PlayByPlay.objects.get(gamePk_id=gameid,
                                                       eventId=eventId)
                pbp.homeScore = byevents[eventId]["homeScore"]
                pbp.awayScore = byevents[eventId]["awayScore"]
                #pbp.eventId = eventId
                #pbp.eventIdx = byevents[eventId]["eventIdx"]
                pbp.save()
                pbpid = pbp.id
                #forgetpbp = datetime.now() - forstart
                #if getPbp is None:
                #    getPbp = forgetpbp
                #else:
                #    getPbp += forgetpbp
                players = {}
                for h in xrange(len(homeplayers)):
                    players["h" + str(h + 1) + "_id"] = homeplayers[h]
                for a in xrange(len(awayplayers)):
                    players["a" + str(a + 1) + "_id"] = awayplayers[a]
                players["play_id"] = pbpid
                pbpp = pbpmodels.PlayByPlayPlayers(**players)
                allplayers[eventId] = pbpp
            except Exception as e:
                print gameid, eventId, e
        #create = datetime.now()
        print len(allplayers.values())
        pbpmodels.PlayByPlayPlayers.objects.bulk_create(allplayers.values())
Exemplo n.º 3
0
def check_game(gameid):
    j = json.loads(api_calls.get_game_timestamps(gameid))
    timestamps = j
    fp = open("../../../data/pbp/timestamps.json", "w")
    fp.write(str(timestamps))
    fp.close()
Exemplo n.º 4
0
def check_game(gameid):
    j = json.loads(api_calls.get_game_timestamps(gameid))
    timestamps = j
    fp = open("../../../data/pbp/timestamps.json", "w")
    fp.write(str(timestamps))
    fp.close()