Example #1
0
def run():
    response = requests.get(URL)
    soup = BeautifulSoup(response.text, "html5lib")
    rows = []
    player = date = status = comment = None
    for tr in soup.find_all("tbody")[0].children:
        row = [td for td in tr if hasattr(tr, "children")]
        if player and date and status:
            if len(row) == 1 and "Comment:" in row[0].contents[0].string:
                Injury.objects.update_or_create(
                    player=player,
                    date=date,
                    comment=unicode(row[0].contents[-1]),
                    defaults={"status": status},
                )
            else:
                Injury.objects.update_or_create(player=player,
                                                date=date,
                                                defaults={"status": status})
            player = date = status = comment = None
        if len(row) == 3:
            name, status, datestr = [r.string for r in row]
            if name != "NAME" and status != "STATUS" and datestr != "DATE":
                date = get_date_yearless(datestr)
                try:
                    player = Player.get_by_name(name)
                except Player.DoesNotExist:
                    print(f"Couldn't find player {name}")
Example #2
0
def run():
    response = requests.get(URL)
    soup = BeautifulSoup(response.text, 'html5lib')
    rows = []
    player = date = status = comment = None
    for tr in soup.find_all('tbody')[0].children:
        row = [td for td in tr if hasattr(tr, 'children')]
        if player and date and status:
            if len(row) == 1 and 'Comment:' in row[0].contents[0].string:
                Injury.objects.update_or_create(
                    player=player,
                    date=date,
                    comment=unicode(row[0].contents[-1]),
                    defaults={ 'status': status }
                )
            else:
                Injury.objects.update_or_create(
                    player=player,
                    date=date,
                    defaults={ 'status': status }
                )
            player = date = status = comment = None
        if len(row) == 3:
            name, status, datestr = [r.string for r in row]
            if name != 'NAME' and status != 'STATUS' and datestr != 'DATE':
                date = get_date_yearless(datestr)
                try:
                    player = Player.get_by_name(name)
                except Player.DoesNotExist:
                    print 'Couldn\'t find player %s' % name
Example #3
0
def write_salaries_to_db(input_rows, date=datetime.date.today()):
    return_rows = []
    csvreader = reader(input_rows, delimiter=",", quotechar='"')
    try:
        for i, row in enumerate(csvreader):
            if i != 0 and len(row) == 9:  # Ignore possible empty rows
                pos, name_and_pid, name, pid, rpos, salary, gameinfo, team, ppg = row
                player = Player.get_by_name(name)
                dksalary, _ = DKSalary.objects.get_or_create(
                    player=player, date=date, defaults={"salary": int(salary)})
                player.dk_position = pos
                player.save()
                if dksalary.salary != int(salary):
                    print("Warning: trying to overwrite salary for %s."
                          " Ignoring - did not overwrite" % player)
                return_rows.append(row)
    except UnicodeEncodeError as e:
        print(e)
        return []
    return return_rows
Example #4
0
def write_salaries_to_db(input_rows, date=datetime.date.today()):
    return_rows = []
    csvreader = reader(input_rows, delimiter=',', quotechar='"')
    try:
        for i, row in enumerate(csvreader):
            if i != 0 and len(row) == 6: # Ignore possible empty rows
                pos, name, salary, game, ppg, team = row
                player = Player.get_by_name(name)
                dksalary, _ = DKSalary.objects.get_or_create(
                    player=player,
                    date=date,
                    defaults={ 'salary': int(salary) }
                )
                player.dk_position = pos
                player.save()
                if dksalary.salary != int(salary):
                    print ('Warning: trying to overwrite salary for %s.'
                           ' Ignoring - did not overwrite' % player)
                return_rows.append(row)
    except UnicodeEncodeError as e:
        print e
        return []
    return return_rows
Example #5
0
 def get_player_cached(name, player_cache):
     if name in player_cache:
         result = player_cache[name]
         return result
     else:
         return Player.get_by_name(name)
Example #6
0
 def get_player_cached(name, player_cache):
     if name in player_cache:
         result = player_cache[name]
         return result
     else:
         return Player.get_by_name(name)
Example #7
0
def print_player_ownerships_timeseries(player_name, percentile=20):
    """
    Used to see the difference between player ownerships in lineups in the top
    @percentile and all other lineups for a single player over time.

    Prints the following table:
    Date | # Entries | Top Own % (#) | Bottom Own % (#) | Total Own % | Diff
    FPTS | Minutes
    """

    def calculate_ownerships(contest):
        query = '''
SELECT c.dk_id, c.entries, rank, pg_id, sg_id, sf_id, pf_id, c_id, g_id, f_id,
    util_id
FROM nba_dkcontest AS c JOIN nba_dkresult AS r ON c.id=r.contest_id
WHERE c.dk_id='%s'
''' % contest.dk_id

        # Executing SQL
        cursor = connection.cursor()
        cursor.execute(query)
        rows = [row for row in cursor.fetchall()]
        if len(rows) == 0:
            print 'No entries found for contest %s' % contest.dk_id
            return
        total_entries = rows[0][1]

        # Collecting data
        topct = botct = toptotal = bottotal = 0
        for row in rows:
            contest_id, num_entries, rank = (row[0], row[1], row[2])
            player_ids = row[3:]
            is_top = float(rank) * 100 <= percentile * num_entries
            if is_top:
                toptotal += 1
            else:
                bottotal += 1
            for player_id in player_ids:
                if player_id == player.id:
                    if is_top:
                        topct += 1
                    else:
                        botct += 1
        toppct = float(topct) / toptotal * 100
        botpct = float(botct) / bottotal * 100
        totalpct = float(topct + botct) / (toptotal + bottotal) * 100

        # The player didn't play
        if topct == botct and topct == 0:
            return

        # Printing results
        stats = player.get_stats(contest.date, 'min', 'dk_points')
        print ('%s\t%d\t%.2f%% (%d)\t%.2f%% (%d)\t%.2f%%\t%.2f%%\t%.2f\t%d'
               % (contest.date, toptotal + bottotal, toppct, topct, botpct,
                  botct, totalpct, toppct - botpct, stats['dk_points'],
                  stats['min']))

    player = Player.get_by_name(player_name)
    print ('Contest date\tEntries\tTop % (Entries)\tBot % (Entries)\tTotal %'
           '\tDiff %\tFPTS\tMIN')
    for date in sorted(set([d.date for d in DKContest.objects.all()])):
        contest = DKContest.objects.filter(date=date).order_by('entries').last()
        calculate_ownerships(contest)
Example #8
0
def print_player_ownerships_timeseries(player_name, percentile=20):
    """
    Used to see the difference between player ownerships in lineups in the top
    @percentile and all other lineups for a single player over time.

    Prints the following table:
    Date | # Entries | Top Own % (#) | Bottom Own % (#) | Total Own % | Diff
    FPTS | Minutes
    """
    def calculate_ownerships(contest):
        query = (
            f"SELECT c.dk_id, c.entries, rank, pg_id, sg_id, sf_id, pf_id, c_id, g_id, f_id, util_id"
            "FROM nba_dkcontest AS c JOIN nba_dkresult AS r ON c.id=r.contest_id"
            "WHERE c.dk_id='{contest.dk_id}'")

        # Executing SQL
        cursor = connection.cursor()
        cursor.execute(query)
        rows = [row for row in cursor.fetchall()]
        if len(rows) == 0:
            print("No entries found for contest {contest.dk_id}")
            return
        total_entries = rows[0][1]

        # Collecting data
        topct = botct = toptotal = bottotal = 0
        for row in rows:
            contest_id, num_entries, rank = (row[0], row[1], row[2])
            player_ids = row[3:]
            is_top = float(rank) * 100 <= percentile * num_entries
            if is_top:
                toptotal += 1
            else:
                bottotal += 1
            for player_id in player_ids:
                if player_id == player.id:
                    if is_top:
                        topct += 1
                    else:
                        botct += 1
        toppct = float(topct) / toptotal * 100
        botpct = float(botct) / bottotal * 100
        totalpct = float(topct + botct) / (toptotal + bottotal) * 100

        # The player didn't play
        if topct == botct and topct == 0:
            return

        # Printing results
        stats = player.get_stats(contest.date, "min", "dk_points")
        print(
            "{}\t{}\t{:.2f}%% ({})\t{:.2f}%% ({})\t{:.2f}%%\t{:.2f}%%\t{:.2f}\t{}"
            .format(
                contest.date,
                toptotal + bottotal,
                toppct,
                topct,
                botpct,
                botct,
                totalpct,
                toppct - botpct,
                stats["dk_points"],
                stats["min"],
            ))

    player = Player.get_by_name(player_name)
    print("Contest date\tEntries\tTop % (Entries)\tBot % (Entries)\tTotal %"
          "\tDiff %\tFPTS\tMIN")
    for date in sorted(set([d.date for d in DKContest.objects.all()])):
        contest = DKContest.objects.filter(
            date=date).order_by("entries").last()
        calculate_ownerships(contest)