from scrapUtil import get_pages, get_tables
from horse import Horse

class Race(object):
    """docstring for ."""

    def __init__(self, id):
        self.id = id
        # self.name = name
        if len(self.id) == 12:
            self.set_race_info(self.id)

    def set_race_info(self,id):
        url = r"https://race.netkeiba.com/race/shutuba.html?race_id="+id
        sp = get_pages(url)
        race_name = ""
        race_name_pre =sp.find("div", class_="RaceName")
        if race_name_pre is None:
            print('データなし',id)
            return 0
        else:
            race_name = race_name_pre.text.replace('\n','')
        race_time=""
        race_dest=""
        race_time_pre = sp.find("div", class_="RaceData01").text
        if '/' in race_time_pre:
            race_time,race_dest = race_time_pre.replace('\n','').split(' / ')
        else:
            race_dest = race_time_pre.lstrip()
        race_site =sp.find("div", class_="RaceData02").text.replace('\n','')[2:4]
        d=sp.find("dl", id="RaceList_DateList")
        print(d)
        race_date=d.find("dd",class_="Active").text
        race_no=sp.find("span", class_="RaceNum").text
        self.horsePre = sp.find_all("span",class_="HorseName")
        self.name=race_name
        self.start_time=race_time
        self.distance=race_dest
        self.site=race_site
        self.no=race_no
        self.date=race_date
    def get_horse_info_in_race(self):
        horseInfo = []
        for hi in self.horsePre:
            if hi.text == '馬名':
                pass
            else:
                house_name = hi.text
                pos = str(hi).find('href=') + 6
                horse_no = str(hi)[pos+30:pos+30+10]
                horseInfo.append(Horse(horse_no,house_name))
        self.horse_info=horseInfo
