def get_onedays(session=None): """ Get oneday records from ONEDAYS page """ if session is None: session = get_session() return get_page_data(ONEDAYS, GetOnedayInfo(), session=session)
def collect_onedays(year=-1, session=None): """ Year is a keyword argument. Default is collect all. """ if session is None: session = get_session() return get_page_data(ONEDAYS, GetOneDayQuiz(year), session=session)
def get_rundle_comp(season, rundle, fsize, session_id=None, func_parm=calc_hun): """ Perform a calculation between all members of a rundle. Input: season -- LL season number rundle -- rundle name "B_Pacific" for example fsize -- size of the field to be displayed func_parm -- name of the routine to be executed for all pairs in this rundle. Keyword parameter, defaults to calc_hun. Returns: A dictionary indexed by player name (in alphabetical order). Each element is a list of tuples consisting of two fields. The fields in the tuple are the player's opponent and caculated nunber based on func_parm. These tuples are arranged in decending value of this func_parm number. """ if session_id is None: session_id = get_session() folks = get_rundle_members(season, rundle, session=session_id) result = {} for plyr in folks: result[plyr] = [] for indx, plyr in enumerate(folks): for opp in folks[indx+1:]: hun_val = format_float(func_parm(plyr, opp, session_id=session_id), fsize) result[plyr].append((opp, hun_val)) result[opp].append((plyr, hun_val)) oresult = {} for plyr in sorted(folks): nlist = sorted(result[plyr], key=itemgetter(1), reverse=True) oresult[plyr] = nlist return oresult
def get_leagues(season, session=None): """ Get a list of leagues for the season specified. """ if session is None: session = get_session() main_data = LLSTANDINGS + "%d" % season return get_page_data(main_data, GetLeagueNames(season), session=session)
def get_rundles(season, league, session=get_session()): """ Get a list of rundles for the season and league specified. """ main_data = LLSTANDINGS + "%d&%s" % (season, league) return get_page_data(main_data, GetRundles(season, league), session=session)
def get_csv_oneday_data(quiz, session=None): """ Given a quiz name, return the csv file for that quiz. """ if session is None: session = get_session() httpv = "%s/oneday/csv/%s.csv" % (LLHEADER, quiz) return read_csv_data(httpv, session=session)
def get_wlt_patterns(player, session=None): """ Call get_user_data to get w-l-t record prior to calling find_wlt_patterns. """ if session is None: session = get_session() pinfo = get_user_data(player.lower(), session=session)[1] return find_wlt_patterns(pinfo)
def get_csv_oneday_players(quiz, session=get_session()): """ Return a list of Llamas who participated in the indicated one-day quiz. """ results = get_csv_oneday_data(quiz, session=session) retval = [] for data in results: retval.append(data[0]) return retval
def get_qhist(player, session=get_session()): """ Extract player's question history. Returns a dict indexed by categories. Each dict entry consists of a 'correct' list and a 'wrong' list of questions asked. """ pname = player.lower() main_data = QHIST % pname return get_page_data(main_data, GetQhist(pname), session=session)
def get_season(session=get_session()): """ Find the season number Input: session request Returns most recent season number """ return int(get_page_data(LLHEADER, GetSeasonNumber(), session=session))
def get_matchcount(session=get_session()): """ Find matches in current season Input: session request """ return get_page_data(ARUNDLE % (get_season(), 'Pacific'), GetCurrentlyFinishedCount(), session=session)
def get_personal_data(person, session=get_session()): """ Get information on a person Input: person -- LL id. session request Returns: dictionary of user's metadata (Location, Gender, College) """ page = "%s/profiles.php?%s" % (LLHEADER, person.lower()) return get_page_data(page, GetPersonalInfo(), session=session)
def read_csv_data(url, session=get_session()): """ Read a csv file pointed to by a url. Return a list of lines. Each line is a list of fields. """ main_data = session.get(url) flines = main_data.text.strip().split('\n') retval = [] for peep in flines[1:]: newpeep = peep.split(',') retval.append(newpeep[1:]) return retval
def get_rundle_members(season, rundle, session=get_session()): """ Get players in a rundle Input: season -- season number rundle -- rundle name (B_Pacific, for example) session request Returns list of user names of players in the rundle """ page = "%s%d&%s" % (LLSTANDINGS, season, rundle) return get_page_data(page, GetRundleMembers(), session=session)
def parse_oneday_get_date(oneday, session=get_session()): """ Find the date of a oneday event. Returns a date value """ urlv = "%s.php?%s" % (ONEDAYS, oneday) one_day_str = get_page_data(urlv, GetDateFromUrl(), session=session) extract = one_day_str.strip() if not extract: return date.today() + timedelta(days=1) extdate = extract.split(':')[0] return datetime.strptime(extdate, "%B %d, %Y").date()
def calc_wonder(season, rundle, session_id=None): """ Calculate a season's worth of WONDER values for a rundle. WONDER ("Warren's Overtly Narcissistic Defensive Efficiency Rating") is a statistic that measures how much defense affected the standings points of a player. It compares match point scores with total questions scores for a match. If player A and player B tie on total questions but player A beats player B on matchpoints, then player A's WONDER score goes up by 1 and player B's WONDER score goes down by 1. Similarly, a loss in total questions with a tie in matchpoints would also change WONDER values by 1, and a loss in total questions with a win in matchpoints would change WONDER values by 2. Since this value only changes by a low integer, the tracking of this value is only being done for each season. Parameters: season -- season number rundle -- name of rundle (R_Pacific_Div_2, for example) Returns -- a list of two item lists whose values are the name of a person and the corresponding WONDER values accumlated for the entire season """ wonderv = [0, -1, -2, 1, 0, -1, 2, 1, 0] if session_id is None: session_id = get_session() current_season = get_season(session=session_id) if season == current_season: match_count = get_matchcount(session=session_id) else: match_count = TOTAL_MATCHES_PER_SEASON pdata = get_rundle_personal(season, rundle, session=session_id) players = {} for plyr in pdata: players[plyr] = 0 for day in range(0, match_count): for match_res in get_matchresult(season, day+1, rundle, session=session_id): results = match_anal(match_res) players[results[0]] += wonderv[results[2]] players[results[1]] -= wonderv[results[2]] output = [] for oplyr in sorted(players.keys()): output.append([oplyr, players[oplyr]]) output = sorted(output, key=itemgetter(1), reverse=True) for entry in output: entry[1] = str(entry[1]) return output
def get_user_data(player, session=get_session()): """ Return information about a user: Tuple of two dicts: first Dict -- indexed by season, list of scores as 2 x 2 tuples. second Dict -- indexed by season, list of W-L-T records as tuples All values are integers Input: player -- player name session request """ return get_page_data(USER_DATA % player.lower(), GetUserData(), session=session)
def get_rundle_personal(season, rundle, session=get_session()): """ Call get_personal_data on all members of a rundle. Input: season -- season number rundle -- rundle name (B_Pacific, for example) session request Return personal info in a dictionary indexed by person. """ retv = {} plist = get_rundle_members(season, rundle, session=session) for person in plist: retv[person] = get_personal_data(person, session) return retv
def __init__(self, season, match_day, rundle, session=get_session()): self.info = {} self.info['season'] = season self.info['day'] = match_day parts = rundle.split('_') self.info['rundle'] = parts[0] self.info['league'] = parts[1] self.info['division'] = 0 self.result = {} if len(parts) > 2: self.info['division'] = int(parts[-1]) page = '&'.join([str(season), str(match_day), rundle]) self.url = MATCH_DATA % page self.raw_data = get_page_data(self.url, GetMatchDay(), session) if len(self.raw_data) % MatchDay.INFO_PER_USER != 0: raise ValueError('LL Parsing Error') self.num_folks = len(self.raw_data) // MatchDay.INFO_PER_USER
def get_matchresult(season, day, rundle, session=get_session()): """ Extract match day results Input: season -- season number day -- match day number in range 1 to 25 rundle -- name of rundle (R_Pacific_Div_2, for example) Returns: A list of match results. Each entry consists of a dictionary whose values are: players -- list of players (2 in a match) score -- list of corresponding match scores (strings) """ page = '&'.join([str(season), str(day), rundle]) this_url = MATCH_DATA % page return get_page_data(this_url, GetMatchResult(), session=session)
def calc_hun(player1, player2, session_id=None): """ Calculate Hamill/Usui numbers Hamill/Usui numbers (HUN) are a metric to determine how similar a player is to another player. This value is the number of questions on which they got the same result (both got right or bot missed) divided by the total number of questions that they answered in common. This has a maximum value of one and the higher the number is, the similar to two players are. Parmeters: player1 -- first player player2 -- player first player is being compared to. Returns -- a HUN number in the range of 0 to 1. A higher value indicates that the players are more 'like' each other. """ if not session_id: session_id = get_session() plyr1 = get_qhist(player1, session_id) plyr2 = get_qhist(player2, session_id) return comp_hun(plyr1, plyr2)
def ll_oneday_players(oneday, session=get_session()): """ Extract a list of players from the oneday passed in """ dval = parse_oneday_get_date(oneday, session=session) indx = 0 for boundary in DATE_BOUNDARIES: if dval < boundary: break indx += 1 if indx == ERROR_BOUNDARY: return [] if indx == CSV_BOUNDARY: return get_csv_oneday_players(oneday, session=session) ostr = ONEDAYS + FILE_PATTERNS[indx] % oneday odata = get_page_data(ostr, GetOldOnedayData(indx), session=session) retval = [] for entry in odata: if entry == 'LearnedLeague': continue retval.append(entry[0]) return list(set(retval))
def get_matchday(season, day, rundle, session=get_session()): """ Extract match day information Input: season -- season number day -- match day number in range 1 to 25 rundle -- name of rundle (R_Pacific_Div_2, for example) Returns: A list with two entries. The first entry is a dict indexed by player name, whose contents are dicts containing the following information: answers -- list of right ('1'), wrong ('0'), or forfeited answers ratings -- list of the point assignments for the questions opp -- name of this day's opponent. The second entry is a dictionary of values related to the entire match day object (league, rundle, division, day, season). """ matchday = MatchDay(season, day, rundle, session=session) return [matchday.get_results(), matchday.get_info()]
Find first href referring to standings.php """ if self.skipsofar: return if tag == 'a': if attrs[0][0] == 'href': if attrs[0][1].startswith(STANDINGS): tindx = attrs[0][1].find('&') + 1 self.result.append(attrs[0][1][tindx:]) def handle_data(self, data): """ Indicate when we should start checking """ if data == self.seasontag: self.skipsofar = False @handle_conn_err def get_leagues(season, session=get_session()): """ Get a list of leagues for the season specified. """ main_data = LLSTANDINGS + "%d" % season return get_page_data(main_data, GetLeagueNames(season), session=session) if __name__ == "__main__": print(get_leagues(78)) print(get_leagues(66, get_session()))