from scrapUtil import get_pages, get_tables
import pandas as pd
class Horse():
    """docstring for ."""

    def __init__(self, id,name):
        self.id = id
        self.name = name
        self.profile = {}
        self.blood = {}
        self.past_race = {}
        self.result = {}
        if len(self.id) == 10:
            self.set_horse_info()
    def set_horse_info(self):
        url = r"https://db.netkeiba.com/horse/"
        data = get_tables(url+self.id)
        dd = data[1].to_dict()
        for d in dd[0]:
            self.profile[dd[0][d]] = dd[1][d]
        self.blood['f'] = data[2][0][0]
        self.blood['m'] = data[2][0][2]
        self.blood['ff'] = data[2][1][0]
        self.blood['fm'] = data[2][1][1]
        self.blood['mf'] = data[2][1][2]
        self.blood['mm'] = data[2][1][3]
        if len(data) > 4 and hasattr(data[3],'受賞歴'):
            data[3] = data[4]
        if len(data) < 4 or 'コース適性' in str(data[3]):
            print('過去レース情報なし',self.id)
        else:
            dd = data[3].rename(columns={'勝ち馬(2着馬)':'勝馬'})
            header = ['日付','開催','天気','レース名','距離',
                '頭数','枠番','馬番','人気','着順','着差','騎手',
                '斤量','タイム','通過','ペース','上り','馬体重','勝馬']
            dd["着順"] = pd.to_numeric(dd["着順"], errors='coerce')
            dd["人気"] = pd.to_numeric(dd["人気"], errors='coerce')
            # dd["人気"] = pd.to_numeric(dd["人気"], errors='coerce')
            self.past_race = dd[header]
            # self.result['平均順位'] = dd.着順.mean()
