def str_w_t(rows_lst): res = {} for k, v in _zip_top_bot(_rem(rows_lst)): ct, _ = v.split('/') if '/' in v else ('0-0', '0') w, t = ct.split('-') if '-' in v else ('0', '0') res[k.lower()] = {'won': to_int(w, 0), 'total': to_int(t, 0)} return res
def __get_by_per_summ(self, per_summ): summ = { } while True: cl = per_summ.get('class') if cl is not None and cl.strip() in ['oddColor', 'evenColor']: txt = ex_junk(per_summ.xpath('.//text()'), ['\r', '\n'], ['']) if txt: per = to_int(txt[0]) per = per if per > 0 else 4 if txt[0] == 'OT' else 0 ps = { 'shifts': to_int(txt[1]), 'avg': self.__get_time(txt[2]), 'toi': self.__get_time(txt[3]), 'ev_toi': self.__get_time(txt[4]), 'pp_toi': self.__get_time(txt[5]), 'sh_toi': self.__get_time(txt[6]) } summ[per] = ps per_summ = per_summ.xpath('following-sibling::tr') if per_summ: per_summ = per_summ[0] else: break return summ, per_summ
def __get_by_per_summ(self, per_summ): summ = {} while True: cl = per_summ.get('class') if cl is not None and cl.strip() in ['oddColor', 'evenColor']: txt = ex_junk(per_summ.xpath('.//text()'), ['\r', '\n'], ['']) if txt: per = to_int(txt[0]) per = per if per > 0 else 4 if txt[0] == 'OT' else 0 ps = { 'shifts': to_int(txt[1]), 'avg': self.__get_time(txt[2]), 'toi': self.__get_time(txt[3]), 'ev_toi': self.__get_time(txt[4]), 'pp_toi': self.__get_time(txt[5]), 'sh_toi': self.__get_time(txt[6]) } summ[per] = ps per_summ = per_summ.xpath('following-sibling::tr') if per_summ: per_summ = per_summ[0] else: break return summ, per_summ
def str_w_t(rows_lst): res = { } for k, v in _zip_top_bot(_rem(rows_lst)): ct, _ = v.split('/') if '/' in v else ('0-0','0') w, t = ct.split('-') if '-' in v else ('0', '0') res[k.lower()] = { 'won': to_int(w, 0), 'total': to_int(t, 0) } return res
def __read_team_doc(lx_doc): fo = {} re_opp = re_comp_num_pos_name() took_draw = lx_doc.xpath(".//td[contains(@class,'playerHeading')]/..") for cent in took_draw: # extract info of center taking the draw rec = ex_junk(cent.xpath('.//text()'), containing=['\n', '\r']) num, pos, name = to_int(rec[0]), rec[1], ' '.join( ri.strip() for ri in reversed(rec[2].split(','))) fo[num] = FaceOffRep.__player_fo_rec(name, pos, rec[3:7]) fo[num]['opps'] = {} for vs in cent.xpath('following-sibling::tr'): if vs.xpath(".//td[contains(@class,'space')]"): break else: opp_rec = ex_junk(vs.xpath('.//text()'), containing=['\n', '\r']) reg_res = re_opp.findall(opp_rec[2]) opp_num, opp_pos, opp_last, opp_first = reg_res[ 0] if reg_res else ('-1', '', '', '') opp_name = ' '.join(oi.strip() for oi in [opp_first, opp_last]) fo[num]['opps'][to_int( opp_num)] = FaceOffRep.__player_fo_rec( opp_name, opp_pos, opp_rec[3:7]) return fo
def build_play(self, pbp_row): """ Parses table row from RTSS. These are the rows tagged with ``<tr class='evenColor' ... >``. Result set contains :py:class:`nhlscrapi.games.playbyplay.Strength` and :py:class:`nhlscrapi.games.events.EventType` objects. Returned play data is in the form .. code:: python { 'play_num': num_of_play 'period': curr_period 'strength': strength_enum 'time': { 'min': min, 'sec': sec } 'vis_on_ice': { 'player_num': player } 'home_on_ice': { 'player_num': player } 'event': event_object } :param pbp_row: table row from RTSS :returns: play data :rtype: dict """ d = pbp_row.findall('./td') c = PlayParser.ColMap(self.season) p = {} to_dig = lambda t: int(t) if t.isdigit() else 0 p['play_num'] = to_int(d[c["play_num"]].text, 0) p['period'] = to_int(d[c["per"]].text, 0) p['strength'] = self.__strength(d[c["str"]].text) #if d[4].text == "PENL": time = d[c["time"]].text.split(":") p['time'] = {"min": int(time[0]), "sec": int(time[1])} #print "period: " + str(p['period']) #print "strength: " + str(p['strength']) #print "period: " + str(p['time']) skater_tab = d[c["vis"]].xpath("./table") p['vis_on_ice'] = self.__skaters( skater_tab[0][0]) if len(skater_tab) else {} skater_tab = d[c["home"]].xpath("./table") p['home_on_ice'] = self.__skaters( skater_tab[0][0]) if len(skater_tab) else {} p['event'] = event_type_mapper(d[c["event"]].text, period=p['period'], skater_ct=len(p['vis_on_ice']) + len(p['home_on_ice']), game_type=self.game_type) p['event'].desc = " ".join([ str(t.encode('ascii', 'replace')) for t in d[c["desc"]].xpath("text()") ]) parse_event_desc(p['event'], season=self.season) return p
def build_play(self, pbp_row): """ Parses table row from RTSS. These are the rows tagged with ``<tr class='evenColor' ... >``. Result set contains :py:class:`nhlscrapi.games.playbyplay.Strength` and :py:class:`nhlscrapi.games.events.EventType` objects. Returned play data is in the form .. code:: python { 'play_num': num_of_play 'period': curr_period 'strength': strength_enum 'time': { 'min': min, 'sec': sec } 'vis_on_ice': { 'player_num': player } 'home_on_ice': { 'player_num': player } 'event': event_object } :param pbp_row: table row from RTSS :returns: play data :rtype: dict """ d = pbp_row.findall('./td') c = PlayParser.ColMap(self.season) p = { } to_dig = lambda t: int(t) if t.isdigit() else 0 p['play_num'] = to_int(d[c["play_num"]].text, 0) p['period'] = to_int(d[c["per"]].text, 0) p['strength'] = self.__strength(d[c["str"]].text) time = d[c["time"]].text.split(":") p['time'] = { "min": int(time[0]), "sec": int(time[1]) } skater_tab = d[c["vis"]].xpath("./table") p['vis_on_ice'] = self.__skaters(skater_tab[0][0]) if len(skater_tab) else { } skater_tab = d[c["home"]].xpath("./table") p['home_on_ice'] = self.__skaters(skater_tab[0][0]) if len(skater_tab) else { } p['event'] = event_type_mapper( d[c["event"]].text, period=p['period'], skater_ct=len(p['vis_on_ice']) + len(p['home_on_ice']), game_type=self.game_type ) p['event'].desc = " ".join([str(t.encode('ascii', 'replace')) for t in d[c["desc"]].xpath("text()")]) parse_event_desc(p['event'], season=self.season) return p
def __build_shift(self, shift): shift = [s.strip() for s in shift] ev = None if shift[-1] == 'G': ev = ET.Goal elif shift[-1] == 'P': ev = ET.Penalty return { 'shift_num': to_int(shift[0]), 'period': 4 if shift[1] == 'OT' else to_int(shift[1]), 'start': self.__get_time(shift[2].split(' / ')[0]), 'end': self.__get_time(shift[3].split(' / ')[0]), 'dur': self.__get_time(shift[4]), 'event': ev }
def __read_team_doc(lx_doc): fo = {} re_opp = re_comp_num_pos_name() took_draw = lx_doc.xpath(".//td[contains(@class,'playerHeading')]/..") for cent in took_draw: # extract info of center taking the draw rec = ex_junk(cent.xpath(".//text()"), containing=["\n", "\r"]) num, pos, name = to_int(rec[0]), rec[1], " ".join(ri.strip() for ri in reversed(rec[2].split(","))) fo[num] = FaceOffRep.__player_fo_rec(name, pos, rec[3:7]) fo[num]["opps"] = {} for vs in cent.xpath("following-sibling::tr"): if vs.xpath(".//td[contains(@class,'space')]"): break else: opp_rec = ex_junk(vs.xpath(".//text()"), containing=["\n", "\r"]) reg_res = re_opp.findall(opp_rec[2]) opp_num, opp_pos, opp_last, opp_first = reg_res[0] if reg_res else ("-1", "", "", "") opp_name = " ".join(oi.strip() for oi in [opp_first, opp_last]) fo[num]["opps"][to_int(opp_num)] = FaceOffRep.__player_fo_rec(opp_name, opp_pos, opp_rec[3:7]) return fo
def __read_line(self, tr): rec = _rem(tr.xpath('.//text()')) if len(rec) == 25: # player info num = to_int(rec[0], 0) if not num: return {} dat = {} dat['pos'] = rec[1] last, first = rec[2].split(',') dat['name'] = {'first': first.strip(), 'last': last.strip()} dat['shifts'] = to_int(rec[10], 0) cols = chain( [(i + 3, v) for i, v in enumerate(['g', 'a', 'p', 'pm', 'pn', 'pim'])], [(i + 15, v) for i, v in enumerate( ['s', 'ab', 'ms', 'ht', 'gv', 'tk', 'bs'])]) for i, col in cols: dat[col] = to_int(rec[i], 0) dat['toi'] = {} for k, v in [(9, 'tot'), (11, 'avg'), (12, 'pp'), (13, 'sh'), (14, 'ev')]: if ':' in rec[k]: mins, secs = rec[k].split(':') dat['toi'][v] = { 'min': to_int(mins, 0), 'sec': to_int(secs, 0) } else: dat['toi'][v] = {'min': 0, 'sec': 0} w, l = to_int(rec[22], 0), to_int(rec[23], 0) dat['fo'] = {'won': w, 'total': w + l} return num, dat else: return -1, {}
def __read_line(self, tr): rec = _rem(tr.xpath('.//text()')) if len(rec) == 25: # player info num = to_int(rec[0],0) if not num: return { } dat = { } dat['pos'] = rec[1] last, first = rec[2].split(',') dat['name'] = { 'first': first.strip(), 'last': last.strip() } dat['shifts'] = to_int(rec[10],0) cols = chain( [ (i+3,v) for i,v in enumerate(['g','a','p','pm','pn','pim']) ], [ (i+15,v) for i,v in enumerate(['s','ab','ms','ht','gv','tk','bs']) ] ) for i, col in cols: dat[col] = to_int(rec[i],0) dat['toi'] = { } for k, v in [ (9,'tot'), (11,'avg'), (12,'pp'), (13,'sh'), (14,'ev') ]: if ':' in rec[k]: mins, secs = rec[k].split(':') dat['toi'][v] = { 'min': to_int(mins,0), 'sec': to_int(secs,0) } else: dat['toi'][v] = { 'min': 0, 'sec': 0 } w, l = to_int(rec[22],0), to_int(rec[23],0) dat['fo'] = { 'won': w, 'total': w+l } return num, dat else: return -1, { }
def __get_time(self, time_str): if time_str and ':' in time_str: mins, secs = time_str.split(':') return {'min': to_int(mins), 'sec': to_int(secs)} else: return {'min': 0, 'sec': 0}
def parse_scoring_summary(self): lx_doc = self.html_doc() main = lx_doc.xpath('//*[@id="MainTable"]')[0] scr_summ = main.xpath('child::tr[4]//tr') for r in scr_summ: #print r.get('class') if r.get('class') in ['oddColor','evenColor']: tds = r.xpath('./td') scr = [td.xpath('text()') for td in tds[:8]] # goal summry data goals = { } # goal num, game state, scoring team gn = to_int(scr[0][0]) if scr[0] else -1 period = self.__period(scr[1]) time = split_time(scr[2][0] if period < 4 else '0:00') strength = self.__strength(scr[3][0] if scr[3] else 'EV') team = team_abbr_parser(scr[4][0]) # skaters on the ice sks = tds[8:] goals[gn] = { 'per': period, 'time': time, 'strength': strength, 'team': team, 'home': self.__skaters(sks[0]), 'away': self.__skaters(sks[1]) } scorer = self.__scorer(scr[5][0]) if scorer['num'] in goals[gn][ assists = [] for s in scr[6:8]: if s and s[0] != u'\xa0': #print s[0], self.__scorer(s[0]) assists.append(self.__scorer(s[0])) print { 'goal_num': gn, 'scorer': scorer, 'assists': assists } def __period(self, scr): period = 0 if scr: if scr[0] == 'SO': period = 5 elif scr[0] == 'OT': period = 4 else: period = to_int(scr[0]) return period def __strength(self, sg_str): if 'PP' in sg_str: return Strength.PP elif 'SH' in sg_str: return Strength.PP else: return Strength.Even def __position(self, long_name): return ''.join(s[0] for s in long_name.split(' ')) def __scorer(self, num_name_tot): nnt = num_name_tot.replace('(',' ').replace(')','') nnt_l = nnt.split(' ') return { 'num': to_int(nnt_l[0]), 'name': nnt_l[1].split('.')[1].strip(), 'seas_tot': to_int(nnt_l[2]) if len(nnt_l) == 3 else -1 } def __skaters(td): sk_d = { } for sk in td.xpath('./font'): pos_pl = sk.get('title').split(' - ') num = to_int(sk.xpath('text()')[0]) if num > 0: sk_d[num] = { 'pos': self.__position(pos_pl[0]), 'name': pos_pl[1] } return sk_d
def parse_scoring_summary(self): lx_doc = self.html_doc() main = lx_doc.xpath('//*[@id="MainTable"]')[0] scr_summ = main.xpath('child::tr[4]//tr') for r in scr_summ: print r.get('class') if r.get('class') in ['oddColor','evenColor']: tds = r.xpath('./td') scr = [td.xpath('text()') for td in tds[:8]] # goal summry data goals = { } # goal num, game state, scoring team gn = to_int(scr[0][0]) if scr[0] else -1 period = self.__period(scr[1]) time = split_time(scr[2][0] if period < 4 else '0:00') strength = self.__strength(scr[3][0] if scr[3] else 'EV') team = team_abbr_parser(scr[4][0]) # skaters on the ice sks = tds[8:] goals[gn] = { 'per': period, 'time': time, 'strength': strength, 'team': team, 'home': self.__skaters(sks[0]), 'away': self.__skaters(sks[1]) } scorer = self.__scorer(scr[5][0]) if scorer['num'] in goals[gn][ assists = [] for s in scr[6:8]: if s and s[0] != u'\xa0': print s[0], self.__scorer(s[0]) assists.append(self.__scorer(s[0])) print { 'goal_num': gn, 'scorer': scorer, 'assists': assists } def __period(self, scr): period = 0 if scr: if scr[0] == 'SO': period = 5 elif scr[0] == 'OT': period = 4 else: period = to_int(scr[0]) return period def __strength(self, sg_str): if 'PP' in sg_str: return Strength.PP elif 'SH' in sg_str: return Strength.PP else: return Strength.Even def __position(self, long_name): return ''.join(s[0] for s in long_name.split(' ')) def __scorer(self, num_name_tot): nnt = num_name_tot.replace('(',' ').replace(')','') nnt_l = nnt.split(' ') return { 'num': to_int(nnt_l[0]), 'name': nnt_l[1].split('.')[1].strip(), 'seas_tot': to_int(nnt_l[2]) if len(nnt_l) == 3 else -1 } def __skaters(td): sk_d = { } for sk in td.xpath('./font'): pos_pl = sk.get('title').split(' - ') num = to_int(sk.xpath('text()')[0]) if num > 0: sk_d[num] = { 'pos': self.__position(pos_pl[0]), 'name': pos_pl[1] } return sk_d
def __tot_pct(res): ct = res.split("/")[0].strip() won, tot = tuple(to_int(i) for i in ct.split("-")) return {"won": won, "total": tot}
def __tot_pct(res): ct = res.split('/')[0].strip() won, tot = tuple(to_int(i) for i in ct.split('-')) return {'won': won, 'total': tot}
def __get_time(self, time_str): if time_str and ':' in time_str: mins, secs = time_str.split(':') return { 'min': to_int(mins), 'sec': to_int(secs) } else: return { 'min': 0, 'sec': 0 }
def key_g_s(rows_lst): res = { } for k, v in _zip_top_bot(_rem(rows_lst)): g, s = v.split('-') if '-' in v else ('0', '0') res[k.lower()] = { 'g': to_int(g, 0), 's': to_int(s, 0) } return res
def __get_time(self, time_str): mins, secs = time_str.split(':') return { 'min': to_int(mins), 'sec': to_int(secs) }
def key_g_s(rows_lst): res = {} for k, v in _zip_top_bot(_rem(rows_lst)): g, s = v.split('-') if '-' in v else ('0', '0') res[k.lower()] = {'g': to_int(g, 0), 's': to_int(s, 0)} return res