예제 #1
0
def get_live_games(seasonID, gamedate, home_team, away_team):

    gameid = ""

    gameVector = []

    #Read in season schedule
    scheduleUrl = "http://stats.swehockey.se/ScheduleAndResults/Live/" + str(
        seasonID)
    response = urllib.urlopen(scheduleUrl)
    page_source = str(response.read())

    #If vectors dont exist then get vectors

    page_source = page_source.replace("\\xc3\\xa5", "å")
    page_source = page_source.replace("\\xc3\\xa4", "ä")
    page_source = page_source.replace("\\xc2\\xa0", " ")
    page_source = page_source.replace("\\xc3\\xa9", "é")
    page_source = page_source.replace("\\xc3\\xb6", "ö")
    page_source = page_source.replace("\\xc3\\x84", "Ä")
    page_source = page_source.replace("\\xc3\\x85", "Å")
    page_source = page_source.replace("\\xc3\\x96", "Ö")
    page_source = page_source.replace("\\r", " ")
    page_source = page_source.replace("\\n", " ")

    for i in range(1, len(page_source) - 10):

        if isnumber(page_source[i:i +
                                4]) and page_source[i + 4] == '-' and isnumber(
                                    page_source[i + 5:i + 7]) and page_source[
                                        i + 7] and isnumber(
                                            page_source[i + 8:i + 10]):
            currdate = page_source[i:i + 10]

        if page_source[i:i + 8] == "/Events/":

            gameID = 0

            for j in range(1, 10):

                if isnumber(page_source[i + 8 + j]) == False:
                    if gameID == 0:
                        gameID = page_source[i + 8:i + 8 + j]

            stats = get_stats(gameID, gamedate)

            if home_team == stats[2] and away_team == stats[3]:
                gameid = gameID

    return gameid
예제 #2
0
def get_official_roster(seasonID, season, serie):

    gameUrl = "http://stats.swehockey.se/Teams/Info/TeamRoster/" + str(seasonID) + ""
    response = urllib.urlopen(gameUrl)
    page_source = str(response.read())
    output = []

    page_source = page_source.replace("\\xc3\\xa5", "å")
    page_source = page_source.replace("\\xc3\\xa4", "ä")
    page_source = page_source.replace("\\xc2\\xa0", " ")
    page_source = page_source.replace("\\xc3\\xa9", "é")
    page_source = page_source.replace("\\xc3\\xb6", "ö")
    page_source = page_source.replace("\\xc3\\x84", "Ä")
    page_source = page_source.replace("\\xc3\\x85", "Å")
    page_source = page_source.replace("\\xc3\\x96", "Ö")
    page_source = page_source.replace("\\xc3\\xa8", "é")

    page_source = page_source.replace("\\r", " ")
    page_source = page_source.replace("\\n", " ")

    content = get_td_content(page_source)

    for i in range(3,len(content)-3):

        player = []

        if "Team Roster" in content[i]:
            team = content[i - 2]

        if "Youth club" in content[i] or (isnumber(content[i+1]) and "," in content[i+2] and "-" in content[i+3]):
            player.append(season)
            player.append(team)
            player.append(serie)
            player.append(content[i + 1])

            n = content[i + 2].find(",")

            name = content[i + 2][0:n]
            surname = content[i+2][n+1:len(content[i+2])]

            if surname[0] == " ":
                surname = surname[1:len(surname)]

            player.append(name)
            player.append(surname)

            player.append(content[i + 3])
            player.append(content[i + 4])
            player.append(content[i + 5])
            player.append(content[i + 6])
            player.append(content[i + 7])
            player.append(content[i + 8][0:3])

            #print(player)

            output.append(player)

    #print(output)

    return output
예제 #3
0
    def test_coords(x_diff):
        im1 = ImageGrab.grab(bbox=(cV[0] + xV[0] + x_diff, cV[1] + yV[0],
                                   cV[0] + xV[1] + x_diff, cV[1] + yV[1]))

        bigimage1 = im1.resize((int(
            (xV[0] - xV[1]) * 2), int((yV[0] - yV[1]) * 2)), Image.NEAREST)
        bigimage2 = im1.resize((int(
            (xV[0] - xV[1]) * 3), int((yV[0] - yV[1]) * 3)), Image.NEAREST)
        bigimage3 = im1.resize((int(
            (xV[0] - xV[1]) * 5), int((yV[0] - yV[1]) * 5)), Image.NEAREST)

        image_string1 = pytesseract.image_to_string(
            bigimage1, config='-psm 7 -c tessedit_char_whitelist=0123456789')
        image_string2 = pytesseract.image_to_string(
            bigimage2, config='-psm 7 -c tessedit_char_whitelist=0123456789')
        image_string3 = pytesseract.image_to_string(
            bigimage3, config='-psm 7 -c tessedit_char_whitelist=0123456789')

        nm = -1

        if isnumber(image_string3):
            nm = int(image_string3)
        elif isnumber(image_string2):
            nm = int(image_string2)
        elif isnumber(image_string1):
            nm = int(image_string1)

        if pc in [2, 3, 4, 5, 6, 7]:
            category = "Attacks"
        elif pc in [8, 9, 10, 11]:
            category = "Shots"
        elif pc in [16, 17]:
            category = "Corners"
        elif pc in [12, 13, 14, 15]:
            category = "Shots"

        if x_diff != 0:
            savestring = "./pics/" + category + "/" + str(matchID) + "-" + str(
                pc) + "-" + str(x_diff) + ".jpg"
            bigimage1 = bigimage1.convert("RGB")
            bigimage1.save(savestring, "JPEG")

        nm = test_number(nm, pc, min, c, matchID)

        return nm
예제 #4
0
def OCRscore(cV):

    h1 = (cV[2] - cV[0]) * 0.715
    h2 = (cV[2] - cV[0]) * 0.74
    h3 = (cV[2] - cV[0]) * 0.75
    h4 = (cV[2] - cV[0]) * 0.775
    v1 = (cV[3] - cV[1]) * 0.21
    v2 = (cV[3] - cV[1]) * 0.295

    im1 = ImageGrab.grab(bbox=(cV[0] + h1, cV[1] + v1, cV[0] + h2, cV[1] + v2))
    im2 = ImageGrab.grab(bbox=(cV[0] + h3, cV[1] + v1, cV[0] + h4, cV[1] + v2))

    bigimage1 = im1.resize((int((h2 - h1) * 10), int((v2 - v1) * 10)),
                           Image.NEAREST)
    bigimage2 = im2.resize((int((h4 - h3) * 10), int((v2 - v1) * 10)),
                           Image.NEAREST)

    bigimage1 = bigimage1.convert("RGB")
    bigimage1.save("pic17n.jpg", "JPEG")

    bigimage2 = bigimage2.convert("RGB")
    bigimage2.save("pic16n.jpg", "JPEG")

    image_string1 = pytesseract.image_to_string(
        bigimage1, config='-psm 10 -c tessedit_char_whitelist=0123456789')
    image_string2 = pytesseract.image_to_string(
        bigimage2, config='-psm 10 -c tessedit_char_whitelist=0123456789')

    score1 = -1
    score2 = -1

    if isnumber(image_string1) == True:
        score1 = int(image_string1)
    if isnumber(image_string2) == True:
        score2 = int(image_string2)

    scoreResult = [score1, score2]
    return scoreResult
예제 #5
0
def check_time(pre_val, pre_sec, start_time, start_min, c, matchID):

    if isnumber(pre_val):

        mins = []

        c.execute("SELECT time as dayTime FROM tradedata where ID = ?", [str(matchID)])
        dayTime = c.fetchall()
        if len(dayTime) > 0:
            currTime = max(dayTime)[0]
            c.execute("SELECT timeMin as times FROM tradedata where ID = ? and time = ?", [str(matchID),currTime])
            mins = c.fetchall()

        if len(mins) > 0:
            start_time = str(max(dayTime)[0])
            start_min = max(mins)[0]

        check_number = 1

        # Check if time numbers are reasonable
        if pre_val < 0 or pre_val > 120:
            check_number = 0

        d1 = datetime.datetime.strptime((start_time)[0:19], '%Y-%m-%d %H:%M:%S')
        d2 = datetime.datetime.strptime(str(datetime.datetime.now())[0:19], '%Y-%m-%d %H:%M:%S')

        diff = (d2 - d1).total_seconds() / 60
        exp_min = start_min + diff

        if len(mins) == 0 and pre_val in [0,1]:
            exp_min = 0

        if pre_val == 45 and pre_sec == 0:
            if start_min > 45:
                pass
        else:
            if abs(exp_min - pre_val) > 2:
                pre_val = -1
            else:
                print("Expected time " + str(round(exp_min, 1)) + " actual " + str(pre_val) + " time accepted")

    return pre_val
예제 #6
0
def add_team_games(seasonID, seasonYear, serie):

    scheduleUrl = "http://stats.swehockey.se/ScheduleAndResults/Schedule/" + str(
        seasonID)

    gameVector = []
    venueVector = []
    audVector = []
    dateVector = []

    response = urllib.urlopen(scheduleUrl)
    page_source = str(response.read())

    import sqlite3
    conn = sqlite3.connect('hockeystats.db')
    c = conn.cursor()

    # Check if vectors exist
    c.execute("SELECT * FROM schedule where SEASONID = ? and SERIE = ?",
              [seasonYear, serie])
    sc = c.fetchall()

    if len(sc) == 0:

        page_source = page_source.replace("\\xc3\\xa5", "å")
        page_source = page_source.replace("\\xc3\\xa4", "ä")
        page_source = page_source.replace("\\xc2\\xa0", " ")
        page_source = page_source.replace("\\xc3\\xa9", "é")
        page_source = page_source.replace("\\xc3\\xb6", "ö")
        page_source = page_source.replace("\\xc3\\x84", "Ä")
        page_source = page_source.replace("\\xc3\\x85", "Å")
        page_source = page_source.replace("\\xc3\\x96", "Ö")
        page_source = page_source.replace("\\r", " ")
        page_source = page_source.replace("\\n", " ")

        for i in range(1, len(page_source) - 10):

            if isnumber(page_source[i:i + 4]) and page_source[i + 4] == '-' and isnumber(page_source[i + 5:i + 7]) and \
                    page_source[i + 7] and isnumber(page_source[i + 8:i + 10]):
                dateVector.append(page_source[i:i + 11])

            if page_source[i:i + 8] == "/Events/":

                gameID = 0

                for j in range(1, 10):
                    if isnumber(page_source[i + 8 + j]) == False:
                        if gameID == 0:
                            gameID = page_source[i + 8:i + 8 + j]
                            gameVector.append(gameID)

                audience = ""

                tds = get_td_content(
                    page_source[i:max(len(page_source) - 10, i + 200)])

                inserted = 0

                for j in range(0, 10):
                    if isnumber(tds[j]) and inserted == 0:
                        inserted = 1
                        audVector.append(int(tds[j]))
                        venueVector.append(tds[j + 1])

        for j in range(0, len(gameVector)):
            c.execute(
                "INSERT INTO schedule (SEASONID, SERIE, GAMEID, GAMEDATE, AUD, VENUE) VALUES (?,?,?,?,?,?)",
                [
                    seasonYear, serie, gameVector[j], dateVector[j + 1],
                    audVector[j], venueVector[j]
                ])

        c.execute(
            "SELECT GAMEID from schedule where SEASONID = ? and SERIE = ?",
            [seasonYear, serie])
        gameVector = c.fetchall()
        c.execute(
            "SELECT GAMEDATE from schedule where SEASONID = ? and SERIE = ?",
            [seasonYear, serie])
        dateVector = c.fetchall()
        c.execute("SELECT AUD from schedule where SEASONID = ? and SERIE = ?",
                  [seasonYear, serie])
        audVector = c.fetchall()
        c.execute(
            "SELECT VENUE from schedule where SEASONID = ? and SERIE = ?",
            [seasonYear, serie])
        venueVector = c.fetchall()

    else:

        c.execute(
            "SELECT GAMEID from schedule where SEASONID = ? and SERIE = ?",
            [seasonYear, serie])
        gameVector = c.fetchall()
        c.execute(
            "SELECT GAMEDATE from schedule where SEASONID = ? and SERIE = ?",
            [seasonYear, serie])
        dateVector = c.fetchall()
        c.execute("SELECT AUD from schedule where SEASONID = ? and SERIE = ?",
                  [seasonYear, serie])
        audVector = c.fetchall()
        c.execute(
            "SELECT VENUE from schedule where SEASONID = ? and SERIE = ?",
            [seasonYear, serie])
        venueVector = c.fetchall()

    for j in range(0, len(gameVector)):
        stats = get_stats(gameVector[j][0], dateVector[j][0])

        c.execute("SELECT GAMEID as GAMEID FROM stats where GAMEID = ?",
                  [stats[0]])

        hits = c.fetchall()

        if len(hits) == 0:

            c.execute(
                """INSERT INTO
                            stats (
                                SEASONID,SERIE,GAMEID,GAMEDATE,HOMETEAM,AWAYTEAM,HOMESCORE,AWAYSCORE,HOMESHOTS,AWAYSHOTS,HOMESAVES,AWAYSAVES,HOMEPENALTY,AWAYPENALTY,HSCORE1,HSCORE2,HSCORE3,HSCORE4,ASCORE1,ASCORE2,ASCORE3,ASCORE4,
                                HSHOTS1,HSHOTS2,HSHOTS3,HSHOTS4,ASHOTS1,ASHOTS2,ASHOTS3,ASHOTS4,HSAVES1,HSAVES2,HSAVES3,HSAVES4,ASAVES1,ASAVES2,ASAVES3,ASAVES4,HPENALTY1,HPENALTY2,HPENALTY3,HPENALTY4,APENALTY1,APENALTY2,APENALTY3,
                                APENALTY4)
                            VALUES
                                (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
                (seasonYear, serie, stats[0], stats[1], stats[2], stats[3],
                 stats[4], stats[5], stats[6], stats[7], stats[8], stats[9],
                 stats[10], stats[11], stats[12], stats[13], stats[14],
                 stats[15], stats[16], stats[17], stats[18], stats[19],
                 stats[20], stats[21], stats[22], stats[23], stats[24],
                 stats[25], stats[26], stats[27], stats[28], stats[29],
                 stats[30], stats[31], stats[32], stats[33], stats[34],
                 stats[35], stats[36], stats[37], stats[38], stats[39],
                 stats[40], stats[41], stats[42], stats[43]))

        else:
            pass

    conn.commit()

    create_teamgames(seasonYear, serie)
예제 #7
0
def import_base_data(pV, input_text, t):
    from functions import contains
    from functions import get_isolated_number
    from functions import isnumber
    import re

    # Variables to get

    # Split up text in lines and delete empty lines
    text_list = input_text[t].splitlines()
    #print(text_list)

    if len(text_list) > 0:
        #print(text_list)

        for k in range(0, len(text_list)):
            if len(text_list[k]) < 4:
                text_list[k] = ""

        for k in range(0, len(text_list)):

            text_list[k] = text_list[k].replace("O", "0")
            text_list[k] = text_list[k].replace("'I", "1")
            text_list[k] = text_list[k].upper()

        #Get time:
        time_row = len(text_list)
        for i in range(1, len(text_list)):
            if ":" in text_list[i]:
                if len(text_list[i]) == 5:
                    pV[0].append(text_list[i])
                    time_row = i
                elif len(text_list[i]) > 5:
                    p = text_list[i].find(":")

                    if isnumber(text_list[i][p - 2:p]):
                        pV[0].append(text_list[i][p - 2:p + 3])
                        time_row = i

        #Get attacks, dangerous attacks, possession
        for i in range(0, len(text_list)):
            if len(text_list[i]) > 6 and contains(text_list[i].upper(),
                                                  "TARGET", 4) == False:

                text_list[i] = text_list[i].replace(">", "")
                text_list[i] = text_list[i].replace(")", "")
                text_list[i] = text_list[i].replace("(", "")
                text_list[i] = text_list[i].replace("C", "")

                #print(text_list[i])

                if len(get_isolated_number(text_list[i])) > 2:

                    list = get_isolated_number(text_list[i])
                    #print(list)

                    for j in range(0, len(list)):
                        list[j] = re.sub("[^0-9]", "", list[j])

                    if len(list) == 4:
                        pV[1].append(list[0])
                        pV[2].append(list[1])
                        pV[3].append(list[2])
                        pV[4].append(list[3])

                        pos1 = float(int(list[0])) * 1.5
                        pos2 = float(int(list[1])) * 1.5
                        pos3 = float(int(list[2]))
                        pos4 = float(int(list[3]))

                        ep1 = 100 * (pos1 + pos3) / (pos1 + pos2 + pos3 + pos4)
                        ep2 = 100 - ep1

                        est_pos1 = str(round(ep1))
                        est_pos2 = str(round(ep2))

                        pV[5].append(est_pos1)
                        pV[6].append(est_pos2)

                    elif len(list) == 6:
                        pV[1].append(list[0])
                        pV[2].append(list[1])
                        pV[3].append(list[2])
                        pV[4].append(list[3])
                        pV[5].append(list[4])
                        pV[6].append(list[5])

                    elif len(list) > 6:
                        pass

        #Get Shots on target and shot outside
        if len(text_list[i]) > 10 and contains(text_list[i], "OFF TARGET",
                                               4) == True:
            for k in range(0, len(text_list[i])):
                text_list[i] = text_list[i].replace("l", "1")
                text_list[i] = text_list[i].replace("'", "")
                text_list[i] = text_list[i].replace("´", "")
                text_list[i] = text_list[i].replace("‘", "")
                text_list[i] = text_list[i].replace(">", "")
                text_list[i] = text_list[i].replace(")", "")
                text_list[i] = text_list[i].replace("(", "")
                text_list[i] = text_list[i].replace("C", "")

                list = get_isolated_number(text_list[-1])
                #print(list)

                if len(list) == 8:
                    pV[9].append(list[3])
                    pV[10].append(list[4])
                    pV[11].append(list[2])
                    pV[12].append(list[5])
                    pV[13].append(list[1])
                    pV[14].append(list[6])
                    pV[15].append(list[0])
                    pV[16].append(list[7])

        for i in range(0, len(text_list)):
            if len(text_list[i]) > 8 and contains(
                    text_list[i], "ON TARGET",
                    4) == True and "OF" not in text_list[
                        i] and "FF" not in text_list[i]:
                #print(text_list[i])
                list = get_isolated_number(text_list[i])

                if len(list) == 2:
                    pV[7].append(list[0])
                    pV[8].append(list[1])

                if len(list) == 3:
                    if list[1] == "0N":
                        pV[7].append(list[0])
                        pV[8].append(list[2])

        #Get score
        for i in range(0, time_row):
            #print(text_list[i])
            if len(get_isolated_number(text_list[i])) == 2:
                pV[17].append(get_isolated_number(text_list[i])[0])
                pV[18].append(get_isolated_number(text_list[i])[1])

            elif len(get_isolated_number(text_list[i])) > 2:

                nums = get_isolated_number(text_list[i])

                for j in range(0, len(nums)):
                    if len(nums) > j:
                        if isnumber(nums[j]):
                            if int(nums[j]) > 9:
                                del nums[j]

                if len(nums) == 2:

                    pV[17].append(nums[0])
                    pV[18].append(nums[1])

    return pV
예제 #8
0
#If vectors dont exist then get vectors

page_source = page_source.replace("\\xc3\\xa5", "å")
page_source = page_source.replace("\\xc3\\xa4", "ä")
page_source = page_source.replace("\\xc2\\xa0", " ")
page_source = page_source.replace("\\xc3\\xa9", "é")
page_source = page_source.replace("\\xc3\\xb6", "ö")
page_source = page_source.replace("\\xc3\\x84", "Ä")
page_source = page_source.replace("\\xc3\\x85", "Å")
page_source = page_source.replace("\\xc3\\x96", "Ö")
page_source = page_source.replace("\\r", " ")
page_source = page_source.replace("\\n", " ")

for i in range(1, len(page_source) - 10):

    if isnumber(
            page_source[i:i + 4]) and page_source[i + 4] == '-' and isnumber(
                page_source[i + 5:i + 7]) and page_source[i + 7] and isnumber(
                    page_source[i + 8:i + 10]):
        currdate = page_source[i:i + 10]

    if page_source[i:i + 8] == "/Events/":

        gameID = 0

        for j in range(1, 10):

            if isnumber(page_source[i + 8 + j]) == False:
                if gameID == 0:
                    gameID = page_source[i + 8:i + 8 + j]
                    gameVector.append(gameID)
                    dateVector.append(currdate)
예제 #9
0
def get_year_statistics(id,seasonYear,serie):

    gameUrl = "http://stats.swehockey.se/Teams/Info/PlayersByTeam/" + str(id)
    response = urllib.urlopen(gameUrl)
    page_source = str(response.read())

    page_source = page_source.replace("\\xc3\\xa5", "å")
    page_source = page_source.replace("\\xc3\\xa4", "ä")
    page_source = page_source.replace("\\xc2\\xa0", " ")
    page_source = page_source.replace("\\xc3\\xa9", "é")
    page_source = page_source.replace("\\xc3\\xb6", "ö")
    page_source = page_source.replace("\\xc3\\x84", "Ä")
    page_source = page_source.replace("\\xc3\\x85", "Å")
    page_source = page_source.replace("\\xc3\\x96", "Ö")
    page_source = page_source.replace("\\r", " ")
    page_source = page_source.replace("\\n", " ")

    tds = get_td_content(page_source)

    import sqlite3
    conn = sqlite3.connect('hockeystats.db')
    c = conn.cursor()

    c.execute("SELECT DISTINCT TEAM FROM rosters where seasonid = ? and serie = ?", [seasonYear, serie])
    teams = c.fetchall()



    for j in range(0,len(teams)):

        last_n = 0
        search_keepers = 0

        team_found = 0

        for i in range(0,len(tds)):
            if teams[j][0] in tds[i]:
                team_found += 1

                if team_found == 2:
                    team = tds[i]

            if "," in tds[i] and tds[i+1] in ["GK","LD","RD","LW","RW","CE"]:

                forname = tds[i][tds[i].find(",")+2:len(tds[i])]
                forname = forname.replace("*","")

                surname = tds[i][0:tds[i].find(",")]

                games = tds[i+2]
                goals = tds[i+3]
                assist = tds[i+4]
                penalty = tds[i+6]
                plus = tds[i+7]
                minus = tds[i+8]
                shots = 0
                saves = 0

                c.execute("UPDATE rosters SET GAMES = ?, GOALS = ?, ASSIST = ?, PENALTY = ?, PLUS = ?, MINUS = ?, SHOTS = ?, SAVES = ? WHERE SEASONID = ? and SERIE = ? and TEAM = ? and FORNAME = ? and SURNAME = ?",[games, goals, assist, penalty, plus, minus, shots, saves, seasonYear, serie, team, forname, surname])
                conn.commit()

            if isnumber(tds[i]) and isnumber(tds[i + 1]) == True and "," in tds[i + 2]:

                if last_n > int(tds[i]):

                    if search_keepers == 0:
                        search_keepers = 1
                    else:
                        search_keepers = 0

                last_n = int(tds[i])

                if search_keepers == 1:
                    if ":" in tds[i + 6]:

                        forname = tds[i+2][tds[i+2].find(",") + 2:len(tds[i+2])]
                        forname = forname.replace("*", "")

                        surname = tds[i+2][0:tds[i+2].find(",")]

                        games = tds[i+5]
                        goals = 0
                        assist = 0
                        penalty = 0
                        plus = 0
                        minus = 0
                        shots = tds[i+9]
                        saves = tds[i+8]

                        c.execute("UPDATE rosters SET GAMES = ?, SHOTS = ?, SAVES = ? WHERE SEASONID = ? and SERIE = ? and TEAM = ? and FORNAME = ? and SURNAME = ?",
                            [games, shots, saves, seasonYear, serie, team, forname,
                             surname])

                        conn.commit()
예제 #10
0
    pV = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
          [], []]
    fV = [
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1
    ]

    for t in range(0, n_iter):
        pV = import_base_data(pV, input_text, t)
        #print(input_text[t])

    ### Create final suggested vector based on the five versions ###

    if pV[0]:
        if isnumber(most_common(pV[0][:])[0:2]) and isnumber(
                most_common(pV[0][:])[3:5]):
            fV[0] = int(most_common(pV[0][:])[0:2])
            fV[1] = int(most_common(pV[0][:])[3:5])

    for i in range(1, 19):
        if len(pV[i][:]) > 1:
            if isnumber(most_common(pV[i][:])):
                fV[i + 1] = int(most_common(pV[i][:]))
            else:
                fV[i + 1] = -1

        elif len(pV[i][:]) == 1:
            if isnumber((pV[i][0])):
                fV[i + 1] = int(pV[i][0])
            else:
예제 #11
0
def get_actions(id, audience, venue, season, team1, team2, c):
    gameUrl = "http://stats.swehockey.se/Game/Events/" + str(id)
    response = urllib.urlopen(gameUrl)
    page_source = str(response.read())

    page_source = page_source.replace("\\xc3\\xa5", "å")
    page_source = page_source.replace("\\xc3\\xa4", "ä")
    page_source = page_source.replace("\\xc2\\xa0", " ")
    page_source = page_source.replace("\\xc3\\xa9", "é")
    page_source = page_source.replace("\\xc3\\xb6", "ö")
    page_source = page_source.replace("\\xc3\\x84", "Ä")
    page_source = page_source.replace("\\xc3\\x85", "Å")
    page_source = page_source.replace("\\xc3\\x96", "Ö")

    page_source = page_source.replace("\\r", " ")
    page_source = page_source.replace("\\n", " ")

    content = get_td_content(page_source)

    period = 0
    events = []

    for i in range(0, len(content)):

        stn = content[i]

        if "Overtime" in stn:
            period = 4
        elif "3rd" in stn:
            period = 3
        elif "2nd" in stn:
            period = 2
        elif "1st" in stn:
            period = 1

        if "%" in content[i] and "(" in content[i + 1] and ")" in content[
                i + 1] and "/" in content[i + 1]:
            #print(content[i+1])

            [saves, shots] = get_all_numbers(content[i + 1])

            event = create_goalie_event(id, period, content[i - 2:i + 1],
                                        shots, saves, audience, venue, season)
            events.append(event)

        if isnumber(content[i][0:2]) and isnumber(
                content[i][3:5]) and content[i][2] == ":":

            event = create_event(id, period, content[i - 1:i + 6], audience,
                                 venue, season)
            events.append(event)

            if event[3] == "Goal":

                extra = event[8]

                for j in range(5, 9):
                    if isnumber(content[i + j][0]) and content[i + j].find(
                            ".") > 0 and content[i + j].find(".") < 5:
                        event = create_assist_event(content[i + j], event,
                                                    audience, venue, season,
                                                    extra)
                        events.append(event)

                    if "Neg." in content[i + j]:
                        numbers = (get_all_numbers(content[i + j]))

                        for k in range(0, len(numbers)):
                            event = create_plus_minus_event(
                                event, -1, numbers[k], audience, venue, season,
                                extra)
                            events.append(event)

                    if "Pos." in content[i + j]:
                        numbers = (get_all_numbers(content[i + j]))

                        for k in range(0, len(numbers)):
                            event = create_plus_minus_event(
                                event, 1, numbers[k], audience, venue, season,
                                extra)
                            events.append(event)

    home_team_short = events[-1][4]

    for i in range(0, len(events)):
        if events[i][3] != -1:
            if events[i][4] == home_team_short:
                events[i][4] = team1
            else:
                events[i][4] = team2
        else:
            if events[i][4] == home_team_short:
                events[i][4] = team2
            else:
                events[i][4] = team1

        if events[i][3] in [-1, 1]:
            c.execute(
                "SELECT FORNAME, SURNAME FROM lineups where GAMEID = ? and TEAM = ? and NUMBER = ?",
                [events[i][0], events[i][4], events[i][5]])
            player_name = c.fetchall()

            events[i][6] = ""
            events[i][7] = ""

            try:
                events[i][6] = player_name[0][1]
            except IndexError:
                pass
            try:
                events[i][7] = player_name[0][0]
            except IndexError:
                pass

    #print(events)
    return events
예제 #12
0
def scrape_sh(seasonID, seasonYear, serie, score_update):
    #Vectors to scrape in first step
    gameVector = []
    venueVector = []
    audVector = []
    dateVector = []
    lineVector = []

    #Read in season schedule
    scheduleUrl = "http://stats.swehockey.se/ScheduleAndResults/Schedule/" + str(
        seasonID)
    response = urllib.urlopen(scheduleUrl)
    page_source = str(response.read())

    #Establish connection to database
    import sqlite3
    conn = sqlite3.connect('hockeystats.db')
    c = conn.cursor()

    #Check if vectors exist
    c.execute("SELECT * FROM schedule where SEASONID = ? and SERIE = ?",
              [seasonYear, serie])
    sc = c.fetchall()

    if len(sc) > 0:
        c.execute("DELETE FROM SCHEDULE")

    #If vectors dont exist then get vectors

    page_source = page_source.replace("\\xc3\\xa5", "å")
    page_source = page_source.replace("\\xc3\\xa4", "ä")
    page_source = page_source.replace("\\xc2\\xa0", " ")
    page_source = page_source.replace("\\xc3\\xa9", "é")
    page_source = page_source.replace("\\xc3\\xb6", "ö")
    page_source = page_source.replace("\\xc3\\x84", "Ä")
    page_source = page_source.replace("\\xc3\\x85", "Å")
    page_source = page_source.replace("\\xc3\\x96", "Ö")
    page_source = page_source.replace("\\r", " ")
    page_source = page_source.replace("\\n", " ")

    #Update rosters

    update_rosters(seasonID, seasonYear, serie, c, conn)

    for i in range(1, len(page_source) - 10):

        if isnumber(page_source[i:i +
                                4]) and page_source[i + 4] == '-' and isnumber(
                                    page_source[i + 5:i + 7]) and page_source[
                                        i + 7] and isnumber(
                                            page_source[i + 8:i + 10]):
            currdate = page_source[i:i + 10]

        if page_source[i:i + 8] == "/Events/":

            gameID = 0

            for j in range(1, 10):

                if isnumber(page_source[i + 8 + j]) == False:
                    if gameID == 0:
                        gameID = page_source[i + 8:i + 8 + j]
                        gameVector.append(gameID)
                        dateVector.append(currdate)

            audience = ""

            tds = get_td_content(
                page_source[i:max(len(page_source) - 10, i + 200)])

            inserted = 0

            for j in range(0, 10):
                if isnumber(tds[j]) and inserted == 0:
                    inserted = 1
                    audVector.append(int(tds[j]))
                    venueVector.append(tds[j + 1])

    for j in range(0, len(gameVector)):
        c.execute(
            "INSERT INTO schedule (SEASONID, SERIE, GAMEID, GAMEDATE, AUD, VENUE) VALUES (?,?,?,?,?,?)",
            [
                seasonYear, serie, gameVector[j], dateVector[j], audVector[j],
                venueVector[j]
            ])

    c.execute("SELECT GAMEID from schedule where SEASONID = ? and SERIE = ?",
              [seasonYear, serie])
    gameVector = c.fetchall()
    c.execute("SELECT GAMEDATE from schedule where SEASONID = ? and SERIE = ?",
              [seasonYear, serie])
    dateVector = c.fetchall()
    c.execute("SELECT AUD from schedule where SEASONID = ? and SERIE = ?",
              [seasonYear, serie])
    audVector = c.fetchall()
    c.execute("SELECT VENUE from schedule where SEASONID = ? and SERIE = ?",
              [seasonYear, serie])
    venueVector = c.fetchall()

    conn.commit()

    ########################################################################################################################
    ################################    Get game specific statistics (Lineups)    ##########################################
    ########################################################################################################################

    #Loop through games
    for j in range(0, len(gameVector)):

        #Check if game already has been updated, then skip update

        c.execute("SELECT * FROM lineups where GAMEID = ?", [gameVector[j][0]])
        check = c.fetchall()

        if len(check) == 0:

            stats = get_stats(gameVector[j][0], dateVector[j][0])
            lineups = get_lineups(gameVector[j][0], audVector[j][0],
                                  venueVector[j][0], seasonYear, stats[2],
                                  stats[3])
            [refs, lines] = get_refs(gameVector[j][0], audVector[j][0],
                                     venueVector[j][0], seasonYear)

            # Create stats table
            c.execute("SELECT GAMEID as GAMEID FROM stats where GAMEID = ?",
                      [stats[0]])

            hits = c.fetchall()

            if len(hits) == 0:

                c.execute(
                    """INSERT INTO
                                stats (
                                    SEASONID,SERIE,GAMEID,GAMEDATE,HOMETEAM,AWAYTEAM,HOMESCORE,AWAYSCORE,HOMESHOTS,AWAYSHOTS,HOMESAVES,AWAYSAVES,HOMEPENALTY,AWAYPENALTY,HSCORE1,HSCORE2,HSCORE3,HSCORE4,ASCORE1,ASCORE2,ASCORE3,ASCORE4,
                                    HSHOTS1,HSHOTS2,HSHOTS3,HSHOTS4,ASHOTS1,ASHOTS2,ASHOTS3,ASHOTS4,HSAVES1,HSAVES2,HSAVES3,HSAVES4,ASAVES1,ASAVES2,ASAVES3,ASAVES4,HPENALTY1,HPENALTY2,HPENALTY3,HPENALTY4,APENALTY1,APENALTY2,APENALTY3,
                                    APENALTY4, HOMEPP, AWAYPP)
                                VALUES
                                    (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
                    (seasonYear, serie, stats[0], stats[1][0:10], stats[2],
                     stats[3], stats[4], stats[5], stats[6], stats[7],
                     stats[8], stats[9], stats[10], stats[11], stats[12],
                     stats[13], stats[14], stats[15], stats[16], stats[17],
                     stats[18], stats[19], stats[20], stats[21], stats[22],
                     stats[23], stats[24], stats[25], stats[26], stats[27],
                     stats[28], stats[29], stats[30], stats[31], stats[32],
                     stats[33], stats[34], stats[35], stats[36], stats[37],
                     stats[38], stats[39], stats[40], stats[41], stats[42],
                     stats[43], stats[44], stats[45]))

            else:
                pass

            conn.commit()

            #Create lineup table
            for i in range(0, len(lineups)):
                c.execute(
                    "SELECT ID as ID FROM lineups where GAMEID = ? and TEAM = ? and NUMBER = ? and FORNAME = ? and SURNAME = ?",
                    [
                        lineups[i][0], lineups[i][1], lineups[i][2],
                        lineups[i][3], lineups[i][4]
                    ])
                hits = c.fetchall()
                c.execute("SELECT ID as ID FROM lineups")
                ids = c.fetchall()

                if len(ids) > 0:
                    id = max(ids)[0] + 1
                else:
                    id = 1

                if len(hits) == 0:

                    c.execute(
                        """INSERT INTO
                                lineups (
                                    ID,GAMEID,SEASONID,SERIE,AUDIENCE,VENUE,HOMETEAM,AWAYTEAM,TEAM,GAMEDATE,NUMBER,FORNAME,SURNAME,POSITION,START_PLAYER,
                                    GOALS, PPGOALS, SHGOALS, ASSISTS, PLUS, MINUS, PENALTY, INPOWERPLAY, INBOXPLAY, SHOTSAT, SAVES, SCORE, FINALSCORE, SCORE5, GOALS5, ASSIST5, GAMES5, SCORE_CURRENT, GOALS_CURRENT, ASSIST_CURRENT, GAMES_CURRENT)
                                VALUES
                                    (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)""",
                        (id, lineups[i][0], lineups[i][9], serie,
                         lineups[i][7], lineups[i][8], stats[2], stats[3],
                         lineups[i][1], stats[1][0:10], lineups[i][2],
                         lineups[i][3], lineups[i][4], lineups[i][5],
                         lineups[i][6]))

                else:
                    pass

                conn.commit()

                c.execute(
                    "SELECT PERSONNR from rosters where SEASONID = ? and TEAM = ? and NUMBER = ?",
                    [seasonYear, lineups[i][1], lineups[i][2]])
                personnr = c.fetchall()

                persnr = ''

                if len(personnr) > 0:
                    persnr = personnr[0][0]

                c.execute(
                    "UPDATE lineups SET PERSONNR = ? WHERE SEASONID = ? and TEAM = ? and NUMBER = ? and FORNAME = ? and SURNAME = ?",
                    [
                        persnr, seasonYear, lineups[i][1], lineups[i][2],
                        lineups[i][3], lineups[i][4]
                    ])

            conn.commit()

            # Get events data from each game
            events = get_actions(gameVector[j][0], audVector[j][0],
                                 venueVector[j][0], seasonYear, stats[2],
                                 stats[3], c)

            # Create event table
            for i in range(0, len(events)):
                c.execute(
                    "SELECT ID FROM events where GAMEID = ? and TIME = ? and EVENT = ? and TEAM = ? and NUMBER = ? and FORNAME = ? and SURNAME = ?",
                    [
                        events[i][0], events[i][2], events[i][3], events[i][4],
                        events[i][5], events[i][7], events[i][6]
                    ])
                hits = c.fetchall()
                c.execute("SELECT ID as ID FROM events")
                ids = c.fetchall()

                if len(ids) > 0:
                    id = max(ids)[0] + 1
                else:
                    id = 1

                if len(hits) == 0:

                    c.execute(
                        """SELECT PERSONNR FROM rosters WHERE SEASONID = ? and TEAM = ? and NUMBER = ? and FORNAME = ? AND SURNAME = ? """,
                        (seasonYear, events[i][4], events[i][5], events[i][7],
                         events[i][6]))
                    personnr = c.fetchall()

                    if personnr == []:
                        pnr = ""
                    else:
                        pnr = personnr[0][0]

                    c.execute(
                        """INSERT INTO
                                    events (
                                        ID,GAMEID,SEASONID,AUDIENCE,VENUE,PERIOD,TIME,EVENT,TEAM,NUMBER,PERSONNR,FORNAME,SURNAME,EXTRA1,EXTRA2)
                                    VALUES
                                        (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
                        (id, events[i][0], events[i][12], events[i][10],
                         events[i][11], events[i][1], events[i][2],
                         events[i][3], events[i][4], events[i][5], pnr,
                         events[i][7], events[i][6], events[i][8],
                         events[i][9]))

                else:
                    pass

            conn.commit()

            # Create ref table
            if serie == "SHL":
                for i in range(0, len(events)):
                    c.execute(
                        "SELECT GAMEID as GAMEID FROM refs where GAMEID = ?",
                        [stats[0]])
                    hits = c.fetchall()

                    if len(hits) == 0:

                        c.execute(
                            """INSERT INTO
                                        refs (
                                            GAMEID,SEASONID,HOMETEAM,AWAYTEAM,REF1,REF2,LINE1,LINE2)
                                        VALUES
                                            (?,?,?,?,?,?,?,?)""",
                            (stats[0], seasonYear, refs[0], refs[1], stats[2],
                             stats[3], lines[0], lines[1]))

                    else:
                        pass

                conn.commit()

            print(str(stats[0]) + " lineups loaded")

            #Update lineups with stats
            c.execute(
                "SELECT TEAM, NUMBER, FORNAME, SURNAME, GAMEDATE FROM lineups where GAMEID = ?",
                [gameVector[j][0]])
            lineups = c.fetchall()

            for i in range(0, len(lineups)):
                c.execute(
                    "SELECT SUM(CASE WHEN EVENT = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    ['Goal', gameVector[j][0], lineups[i][0], lineups[i][1]])
                goals = c.fetchall()[0][0]

                if goals == None:
                    goals = 0

                c.execute(
                    "SELECT SUM(CASE WHEN EVENT = ? and EXTRA1 = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    [
                        'Goal', 'PP', gameVector[j][0], lineups[i][0],
                        lineups[i][1]
                    ])
                PP = c.fetchall()[0][0]

                if PP == None:
                    PP = 0

                c.execute(
                    "SELECT SUM(CASE WHEN EVENT = ? and EXTRA1 = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    [
                        'Goal', 'SH', gameVector[j][0], lineups[i][0],
                        lineups[i][1]
                    ])
                SH = c.fetchall()[0][0]

                if SH == None:
                    SH = 0

                c.execute(
                    "SELECT SUM(CASE WHEN EVENT = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    ['Assist', gameVector[j][0], lineups[i][0], lineups[i][1]])
                assist = c.fetchall()[0][0]

                if assist == None:
                    assist = 0

                c.execute(
                    "SELECT SUM(CASE WHEN EVENT = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    ['1', gameVector[j][0], lineups[i][0], lineups[i][1]])
                plus = c.fetchall()[0][0]

                if plus == None:
                    plus = 0

                c.execute(
                    "SELECT SUM(CASE WHEN EVENT = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ? and (extra1 = ? or extra1 = ?)",
                    [
                        '-1', gameVector[j][0], lineups[i][0], lineups[i][1],
                        '', 'PP'
                    ])
                minus = c.fetchall()[0][0]

                if minus == None:
                    minus = 0

                c.execute(
                    "SELECT SUM(CASE WHEN EVENT = ? then CAST(EXTRA2 as INT) else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    [
                        'Penalty', gameVector[j][0], lineups[i][0],
                        lineups[i][1]
                    ])
                penalty = c.fetchall()[0][0]

                if penalty == None:
                    penalty = 0

                c.execute(
                    "SELECT SUM(CASE WHEN EXTRA1 = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    ['PP', gameVector[j][0], lineups[i][0], lineups[i][1]])
                activePP = c.fetchall()[0][0]

                if activePP == None:
                    activePP = 0
                elif activePP > 1:
                    activePP = 1

                c.execute(
                    "SELECT SUM(CASE WHEN EXTRA1 = ? then 1 else 0 end) as X FROM events where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    ['SH', gameVector[j][0], lineups[i][0], lineups[i][1]])
                activeBP = c.fetchall()[0][0]

                if activeBP == None:
                    activeBP = 0
                elif activeBP > 1:
                    activeBP = 1

                #Addera kod för shots/saves

                c.execute(
                    "SELECT EXTRA1, EXTRA2 from events where EVENT = ? and GAMEID = ? and TEAM = ? and NUMBER = ?",
                    [
                        'Keeper stat', gameVector[j][0], lineups[i][0],
                        lineups[i][1]
                    ])
                golieStats = c.fetchall()

                if golieStats == []:
                    shotsAt = 0
                    saves = 0
                else:
                    shotsAt = golieStats[0][0]
                    saves = golieStats[0][1]

                c.execute(
                    "UPDATE lineups SET GOALS = ?, PPGOALS = ?, SHGOALS = ?, ASSISTS = ?, PLUS = ?, MINUS = ?, PENALTY = ?, INPOWERPLAY = ?, INBOXPLAY = ?, SHOTSAT = ?, SAVES = ? WHERE GAMEID = ? and TEAM = ? and NUMBER = ?",
                    [
                        goals, PP, SH, assist, plus, minus, penalty, activePP,
                        activeBP, shotsAt, saves, gameVector[j][0],
                        lineups[i][0], lineups[i][1]
                    ])

                conn.commit()

                # Update lineups with old stats

                # Last five games

                c.execute(
                    "SELECT SCORE, GOALS, ASSISTS, GAMEDATE, TEAM, FORNAME, SURNAME FROM lineups WHERE GAMEDATE < ? and GAMEDATE > ? and FORNAME = ? and SURNAME = ? and PERSONNR = ? ORDER BY GAMEDATE DESC",
                    [
                        lineups[i][4],
                        transform_date(lineups[i][4], 20), lineups[i][2],
                        lineups[i][3], persnr
                    ])
                output = np.array(c.fetchall())

                games5 = min(len(output), 5)

                score5 = 0
                goals5 = 0
                assist5 = 0

                for k in range(0, games5):
                    score5 += float(output[k][0]) / games5
                    goals5 += float(output[k][1]) / games5
                    assist5 += float(output[k][2]) / games5

                # Current season

                c.execute(
                    "SELECT SCORE, GOALS, ASSISTS, GAMEDATE, TEAM, FORNAME, SURNAME FROM lineups WHERE SEASONID = ? and FORNAME = ? and SURNAME = ? and PERSONNR = ? ORDER BY GAMEDATE DESC",
                    [seasonYear, lineups[i][2], lineups[i][3], persnr])
                output = np.array(c.fetchall())

                gamescurr = len(output)

                scorecurr = 0
                goalscurr = 0
                assistcurr = 0

                for k in range(0, gamescurr):
                    scorecurr += float(output[k][0]) / gamescurr
                    goalscurr += float(output[k][1]) / gamescurr
                    assistcurr += float(output[k][2]) / gamescurr

                c.execute(
                    """UPDATE lineups SET SCORE5 = ?, GOALS5 = ?, ASSIST5 = ?, SCORE_CURRENT = ?, GOALS_CURRENT = ?, ASSIST_CURRENT = ?, GAMES5 = ?, GAMES_CURRENT = ?
                           WHERE GAMEID = ? and PERSONNR = ? and FORNAME = ? and SURNAME = ?""",
                    [
                        score5, goals5, assist5, scorecurr, goalscurr,
                        assistcurr, games5, gamescurr, gameVector[j][0],
                        persnr, lineups[i][2], lineups[i][3]
                    ])

                conn.commit()

                #Create teamgames table

                create_teamgames(seasonYear, serie, c)

            print(str(stats[0]) + " stats, events loaded")
        else:
            print("Game already loaded")

        if t_count == 1:
            t += 1
            print(str(t) + "/" + str(len(gameVector)) + " done")

    #Update score for games

    for j in range(0, len(gameVector)):

        c.execute("SELECT GAMEID FROM TEAMSCORE WHERE GAMEID = ?",
                  [gameVector[j][0]])
        check = c.fetchall()

        if score_update == "Full" or len(check) == 0:

            # Add score to lineups

            c.execute(
                "SELECT TEAM, NUMBER, FORNAME, SURNAME, GAMEDATE, HOMETEAM, AWAYTEAM FROM lineups where GAMEID = ?",
                [gameVector[j][0]])
            lineups = c.fetchall()

            for i in range(0, len(lineups)):

                c.execute(
                    "SELECT * from lineups where GAMEID = ? and TEAM = ? and NUMBER = ?",
                    [gameVector[j][0], lineups[i][0], lineups[i][1]])
                lineup = c.fetchall()

                score = create_game_rating(lineup, lineups[i][0], c, conn)

                if len(score) < 4:
                    score = ['0', '0', '0', '0']

                c.execute(
                    "UPDATE lineups SET SCORE = ?, FINALSCORE = ?, OFFSCORE = ?, DEFSCORE = ? WHERE GAMEID = ? and TEAM = ? and NUMBER = ?",
                    [
                        score[0], score[1], score[2], score[3],
                        gameVector[j][0], lineups[i][0], lineups[i][1]
                    ])

            #Calculate team strenght

            if len(lineups) > 0:

                # Check score home team

                [team_strenght, form_score, last_seasons_score, player_score
                 ] = calculate_team_strength(lineups[0][5], lineups[0][4], "",
                                             c)

                c.execute(
                    "SELECT * FROM TEAMSCORE WHERE GAMEDATE = ? AND TEAM = ?",
                    [lineups[0][4], lineups[0][5]])
                chk = c.fetchall()

                if len(chk) == 0:
                    c.execute(
                        "INSERT INTO TEAMSCORE (SEASONID, SERIE, GAMEID, GAMEDATE, TEAM, SCORE, FORM_SCORE, LAST_SEASONS_SCORE, PLAYER_SCORE) VALUES (?,?,?,?,?,?,?,?,?)",
                        [
                            seasonYear, serie, gameVector[j][0], lineups[0][4],
                            lineups[0][5], team_strenght, form_score,
                            last_seasons_score, player_score
                        ])
                else:
                    c.execute(
                        "UPDATE TEAMSCORE SET SCORE = ?, FORM_SCORE = ?, LAST_SEASONS_SCORE = ?, PLAYER_SCORE = ? WHERE SEASONID = ? AND SERIE = ? AND GAMEID = ? AND TEAM = ?",
                        [
                            team_strenght, form_score, last_seasons_score,
                            player_score, seasonYear, serie, gameVector[j][0],
                            lineups[0][5]
                        ])

                #Check score away team

                [team_strenght, form_score, last_seasons_score, player_score
                 ] = calculate_team_strength(lineups[0][6], lineups[0][4], "",
                                             c)

                c.execute(
                    "SELECT * FROM TEAMSCORE WHERE GAMEDATE = ? AND TEAM = ?",
                    [lineups[0][4], lineups[0][6]])
                chk = c.fetchall()

                if len(chk) == 0:
                    c.execute(
                        "INSERT INTO TEAMSCORE (SEASONID, SERIE, GAMEID, GAMEDATE, TEAM, SCORE, FORM_SCORE, LAST_SEASONS_SCORE, PLAYER_SCORE) VALUES (?,?,?,?,?,?,?,?,?)",
                        [
                            seasonYear, serie, gameVector[j][0], lineups[0][4],
                            lineups[0][6], team_strenght, form_score,
                            last_seasons_score, player_score
                        ])
                else:
                    c.execute(
                        "UPDATE TEAMSCORE SET SCORE = ?, FORM_SCORE = ?, LAST_SEASONS_SCORE = ?, PLAYER_SCORE = ? WHERE SEASONID = ? AND SERIE = ? AND GAMEID = ? AND TEAM = ?",
                        [
                            team_strenght, form_score, last_seasons_score,
                            player_score, seasonYear, serie, gameVector[j][0],
                            lineups[0][6]
                        ])

            print("Score updated")

            conn.commit()

    # Update pre-game info

    for j in range(0, len(gameVector)):

        c.execute("SELECT GAMEID FROM EXP_SHOTS_TABLE WHERE GAMEID = ?",
                  [gameVector[j][0]])
        check = c.fetchall()

        if len(check) == 0:

            c.execute("SELECT HOMETEAM, AWAYTEAM FROM STATS WHERE GAMEID = ?",
                      [gameVector[j][0]])
            teams = c.fetchall()

            create_pre_match_analysis(dateVector[j][0], seasonID, serie,
                                      teams[0][0], teams[0][1],
                                      gameVector[j][0], c, conn)
예제 #13
0
def get_stats(id, gamedate):

    gameUrl = "http://stats.swehockey.se/Game/LineUps/" + str(id)
    response = urllib.urlopen(gameUrl)
    page_source = str(response.read())

    page_source = page_source.replace("\\xc3\\xa5", "å")
    page_source = page_source.replace("\\xc3\\xa4", "ä")
    page_source = page_source.replace("\\xc3\\xa9", "é")
    page_source = page_source.replace("\\xc3\\xb6", "ö")
    page_source = page_source.replace("\\xc3\\x84", "Ä")
    page_source = page_source.replace("\\xc3\\x85", "Å")
    page_source = page_source.replace("\\xc3\\x96", "Ö")

    team_string = ""
    num_vect = []
    pct_vect = []
    period_list = []
    output = []

    for j in range(1, len(page_source) - 10):

        if page_source[j] == ">":
            exNum = get_isolated_number(page_source, j)
            if exNum == 0 or exNum >= 1:
                num_vect.append(exNum)

            exPct = get_isolated_percent(page_source, j)
            if exPct >= 0:
                pct_vect.append(exPct)

        if page_source[j] == "(":
            period_vector = get_period_stats(page_source, j)
            if period_vector != []:
                period_list.append(period_vector)

        if page_source[j:j + 17] == "\\xc2\\xa0-\\xc2\\xa0":

            isf = 0
            s1 = 0
            s2 = 0

            for k in range(1, 35):
                if isf == 0 and j - k > 0:
                    if page_source[j - k] == ">":
                        isf = 1
                        s1 = j - k

            isf = 0

            for k in range(1, 60):
                if isf == 0 and j + k < len(page_source):
                    if page_source[j + k] == "<":
                        isf = 1
                        s2 = j + k

            if isnumber(page_source[s1 + 1:j]) and isnumber(page_source[j + 17:s2]):
                home_score = page_source[s1 + 1:j]
                away_score = page_source[j + 17:s2]
            else:
                home_team = page_source[s1 + 1:j]
                away_team = page_source[j + 17:s2]

    home_shots = num_vect[0]
    away_shots = num_vect[1]
    home_saves = num_vect[2]
    away_saves = num_vect[3]
    home_penalty = num_vect[4]
    away_penalty = num_vect[5]

    hp1score = period_list[1][0]
    hp2score = period_list[1][2]
    hp3score = period_list[1][4]
    ap1score = period_list[1][1]
    ap2score = period_list[1][3]
    ap3score = period_list[1][5]

    if len(period_list[1]) > 6:
        hp4score = period_list[1][6]
    else:
        hp4score = 0

    if len(period_list[1]) > 7:
        ap4score = period_list[1][7]
    else:
        ap4score = 0

    hp1shots = 0
    hp2shots = 0
    hp3shots = 0
    ap1shots = 0
    ap2shots = 0
    ap3shots = 0


    try:
        hp1shots = period_list[0][0]
    except IndexError:
        pass
    try:
        hp2shots = period_list[0][1]
    except IndexError:
        pass
    try:
        hp3shots = period_list[0][2]
    except IndexError:
        pass
    try:
        ap1shots = period_list[2][0]
    except IndexError:
        pass
    try:
        ap2shots = period_list[2][1]
    except IndexError:
        pass
    try:
        ap3shots = period_list[2][2]
    except IndexError:
        pass

    if len(period_list[0]) > 3:
        hp4shots = period_list[0][3]
    else:
        hp4shots = 0

    if len(period_list[2]) > 3:
        ap4shots = period_list[2][3]
    else:
        ap4shots = 0

    ap1saves = hp1shots - hp1score
    ap2saves = hp2shots - hp2score
    ap3saves = hp3shots - hp3score
    ap4saves = hp4shots - hp4score
    hp1saves = ap1shots - ap1score
    hp2saves = ap2shots - ap2score
    hp3saves = ap3shots - ap3score
    hp4saves = ap4shots - ap4score

    hp1penalty = period_list[5][0]
    hp2penalty = period_list[5][1]
    hp3penalty = period_list[5][2]
    ap1penalty = period_list[6][0]
    ap2penalty = period_list[6][1]
    ap3penalty = period_list[6][2]

    if len(period_list[5]) > 3:
        hp4penalty = period_list[5][3]
    else:
        hp4penalty = 0

    if len(period_list[6]) > 3:
        ap4penalty = period_list[6][3]
    else:
        ap4penalty = 0

    output.append(id)
    output.append(gamedate)
    output.append(home_team)
    output.append(away_team)

    output.append(home_score)
    output.append(away_score)
    output.append(home_shots)
    output.append(away_shots)
    output.append(home_saves)
    output.append(away_saves)
    output.append(home_penalty)
    output.append(away_penalty)

    output.append(hp1score)
    output.append(hp2score)
    output.append(hp3score)
    output.append(hp4score)
    output.append(ap1score)
    output.append(ap2score)
    output.append(ap3score)
    output.append(ap4score)

    output.append(hp1shots)
    output.append(hp2shots)
    output.append(hp3shots)
    output.append(hp4shots)
    output.append(ap1shots)
    output.append(ap2shots)
    output.append(ap3shots)
    output.append(ap4shots)

    output.append(hp1saves)
    output.append(hp2saves)
    output.append(hp3saves)
    output.append(hp4saves)
    output.append(ap1saves)
    output.append(ap2saves)
    output.append(ap3saves)
    output.append(ap4saves)

    output.append(hp1penalty)
    output.append(hp2penalty)
    output.append(hp3penalty)
    output.append(hp4penalty)
    output.append(ap1penalty)
    output.append(ap2penalty)
    output.append(ap3penalty)
    output.append(ap4penalty)

    if len(pct_vect) > 4:
        home_pp = pct_vect[4]
    else:
        home_pp = 0
    if len(pct_vect) > 5:
        away_pp = pct_vect[5]
    else:
        away_pp = 0

    output.append(home_pp)
    output.append(away_pp)

    return output
예제 #14
0
def OCRattacks(cV, min, c, matchID):

    h1 = (cV[2] - cV[0]) * 0.52
    h2 = (cV[2] - cV[0]) * 0.99

    v1 = (cV[3] - cV[1]) * 0.50
    v2 = (cV[3] - cV[1]) * 0.59

    im1 = ImageGrab.grab(bbox=(cV[0] + h1, cV[1] + v1, cV[0] + h2, cV[1] + v2))

    bigimage1 = im1.resize((int((h2 - h1) * 5), int((v2 - v1) * 5)),
                           Image.NEAREST)

    image_string1 = pytesseract.image_to_string(
        bigimage1, config='-psm 7 -c tessedit_char_whitelist=0123456789')

    savestring = "./pics/Attacks/attack_string.jpg"
    bigimage1 = bigimage1.convert("RGB")
    bigimage1.save(savestring, "JPEG")

    nums = get_all_numbers(image_string1)

    for i in range(0, len(nums)):
        if isnumber(nums[i]) == True:
            nums[i] = int(nums[i])
        else:
            nums[i] = -1

    ap = [-1, -1, -1, -1, -1, -1]

    if len(nums) == 6:
        for i in range(0, 6):
            ap[i] = nums[i]

    elif len(nums) == 9:
        ap[0] = nums[0]
        ap[1] = nums[2]
        ap[2] = nums[3]
        ap[3] = nums[5]
        ap[4] = nums[6]
        ap[5] = nums[8]

    elif len(nums) == 7 or len(nums) == 8:

        ap[0] = nums[0]

        for i in range(1, 2):
            if test_number(nums[i], 3, min, c, matchID) > -1:
                ap[1] = nums[i]
        for i in range(2, 3):
            if test_number(nums[i], 4, min, c, matchID) > -1:
                ap[2] = nums[i]
        for i in range(3, 5):
            if test_number(nums[i], 5, min, c, matchID) > -1:
                ap[3] = nums[i]
        for i in range(4, 7):
            if len(nums) > i:
                if test_number(nums[i], 6, min, c, matchID) > -1:
                    ap[4] = nums[i]
        for i in range(5, 7):
            if len(nums) > i:
                if test_number(nums[i], 7, min, c, matchID) > -1:
                    ap[5] = nums[i]

    ap[0] = test_number(ap[0], 2, min, c, matchID)
    ap[1] = test_number(ap[1], 3, min, c, matchID)
    ap[2] = test_number(ap[2], 4, min, c, matchID)
    ap[3] = test_number(ap[3], 5, min, c, matchID)
    ap[4] = test_number(ap[4], 6, min, c, matchID)
    ap[5] = test_number(ap[5], 7, min, c, matchID)

    if ap[4] + ap[5] != 100:
        ap[4] = -1
        ap[5] = -1

    return ap
예제 #15
0
def get_time(cV):

    xV = [(cV[2] - cV[0]) * 0.27, (cV[2] - cV[0]) * 0.22]
    yV = [(cV[3] - cV[1]) * 0.30, (cV[3] - cV[1]) * 0.245]

    im1 = ImageGrab.grab(bbox=(cV[0] + xV[0], cV[1] + yV[0], cV[0] + xV[1],
                               cV[1] + yV[1]))

    bigimage1 = im1.resize((int((xV[0] - xV[1]) * 2), int(
        (yV[0] - yV[1]) * 2)), Image.NEAREST)
    bigimage2 = im1.resize((int((xV[0] - xV[1]) * 3), int(
        (yV[0] - yV[1]) * 3)), Image.NEAREST)
    bigimage3 = im1.resize((int((xV[0] - xV[1]) * 5), int(
        (yV[0] - yV[1]) * 5)), Image.NEAREST)

    image_string1 = pytesseract.image_to_string(bigimage1, config='-psm 7')
    image_string2 = pytesseract.image_to_string(bigimage2, config='-psm 7')
    image_string3 = pytesseract.image_to_string(bigimage3, config='-psm 7')

    bigimage3 = bigimage3.convert("RGB")
    bigimage3.save("pic1n.jpg", "JPEG")

    [min1, min2, min3] = ["-1", "-1", "-1"]
    [sec1, sec2, sec3] = ["-1", "-1", "-1"]

    if ":" in image_string1:
        i = image_string1.index(':')

        min1 = image_string1[0:i]
        sec1 = image_string1[i + 1:len(image_string1) + 1]

    elif len(image_string1) == 5:
        min1 = image_string1[0:2]
        sec1 = image_string1[3:5]

    if ":" in image_string2:
        i = image_string2.index(':')

        min2 = image_string2[0:i]
        sec2 = image_string2[i + 1:len(image_string2) + 1]

    elif len(image_string2) == 5:
        min2 = image_string2[0:2]
        sec2 = image_string2[3:5]

    if ":" in image_string3:
        i = image_string3.index(':')

        min3 = image_string3[0:i]
        sec3 = image_string3[i + 1:len(image_string3) + 1]

    elif len(image_string3) == 5:
        min3 = image_string3[0:2]
        sec3 = image_string3[3:5]

    min = -1
    sec = -1

    if min1 == min2 and min2 == min3:
        if isnumber(min1):
            min = int(min1)
    elif min1 == min2:
        if isnumber(min1):
            min = int(min1)
    elif min1 == min3:
        if isnumber(min1):
            min = int(min1)
    elif min2 == min3:
        if isnumber(min2):
            min = int(min2)

    if min > -1:
        if sec1 == sec2 and sec2 == sec3:
            if isnumber(sec1):
                sec = int(sec1)
        elif sec1 == sec2:
            if isnumber(sec1):
                sec = int(sec1)
        elif sec1 == sec3:
            if isnumber(sec1):
                sec = int(sec1)
        elif sec2 == sec3:
            if isnumber(sec2):
                sec = int(sec2)
        else:
            if isnumber(sec1):
                sec = int(sec1)
            elif isnumber(sec2):
                sec = int(sec2)
            elif isnumber(sec3):
                sec = int(sec3)

    return [min, sec]