Exemple #1
0
def getMatchups(soup, _cshTeam, url):
    """
    Gets every team's data given the CSH team's name and url. Gathers the
    scores, dates for upcoming games, and opposing teams stats. Stores the
    information into the database after making the appropriate objects.
    """
    x = soup.find_all(text=re.compile(_cshTeam))
    _csh_wins = 0
    _csh_losses = 0
    _csh_ties = 0
    # sportContent = soup.find_all("div", { "class" : "popover-content" })[0]
    # li = sportContent.find_all("li")[2]
    # _sport = li.find_next("a").get_text()
    title = soup.title.string
    _sport = title.split("/")[1]
    # header = soup.find(id="ctl00_ucSiteHeader_divHeaderBox")
    header = soup.find("a", {"class": "school-logo"})
    csh_picture = header.find_next("img").get("src")
    try:
        cshTeam = Team.objects.get(link=url)
    except:
        cshTeam = None
    if cshTeam:
        cshTeam.picture = csh_picture
        cshTeam.save()
    else:
        cshTeam = Team(
            link=url,
            sport=_sport,
            name=_cshTeam,
            wins=_csh_wins,
            losses=_csh_losses,
            ties=_csh_ties,
            picture=csh_picture,
            rank=0,
            iscsh=True,
            season=Season.objects.all()[0].season,
        )
        cshTeam.save()
    matchList = list(cshTeam.CSH.all())
    overall = soup.find(id="ctl00_ContentPlaceHolder2_ucTeamRelated_pnlTeamSchedule")
    teamSchedule = overall.find_next("tbody")
    if teamSchedule == None:
        print("No team schedule found.")
    else:
        counter = 0
        teams = teamSchedule.find_all("tr")
        year = overall.get_text()[1:5]
        endofyear = False
        for i in range(len(teams) // 2):
            match = teams[counter].find_all("td")
            _clean_date = match[0].get_text() + " " + year
            if _clean_date.split(" ")[1] == "Dec":
                endofyear = True
            if endofyear == True and _clean_date.split(" ")[1] != "Dec":
                newyear = str(int(_clean_date.split(" ")[3]) + 1)
                _clean_date = match[0].get_text() + " " + newyear
            opponent = match[1].get_text().split("  ")
            _enemyTeam = opponent[1]
            loc = opponent[0]  # gets whether it is VS or @
            result = match[2].get_text().split()
            print(result)
            _outcome = result[0]  # gets whether it was a W or L or T
            if (_outcome != "W") and (_outcome != "L") and (_outcome != "T"):  # if matchup hasn't happened yet
                _outcome = None
                _upcoming = match[2].get_text()
            elif len(result) < 4:
                _outcome = None
                _upcoming = "Awaiting Scores..."
            else:
                if loc == "VS":
                    _cshScore = result[1]
                    _enemyScore = result[3]
                else:
                    _cshScore = result[3]
                    _enemyScore = result[1]
            record = match[4].get_text().split("-")
            _wins = record[0]
            _losses = record[1]
            _ties = record[2]
            enemyUrl = match[1].find_next("a").get("href")
            pictureLink = match[1].find_next("img").get("src")
            if "DefaultLogo" in pictureLink:
                _picture = pictureLink
            else:
                _picture = getPicture(pictureLink)
            if _outcome == "W":
                _csh_wins += 1
            elif _outcome == "L":
                _csh_losses += 1
            elif _outcome == "T":
                _csh_ties += 1
            _date = datetime.strptime(_clean_date, "%a, %b %d %Y")
            print(_date)
            counter += 2
            try:
                enemyTeam = Team.objects.get(link=enemyUrl)
            except:
                enemyTeam = None
            if enemyTeam:
                enemyTeam.wins = _wins
                enemyTeam.losses = _losses
                enemyTeam.ties = _ties
                enemyTeam.picture = _picture
                enemyTeam.rank = getRank(soup, enemyTeam.name, _wins, _losses, _ties)
                enemyTeam.save()
            else:
                enemyTeam = Team(
                    link=enemyUrl,
                    sport=_sport,
                    name=_enemyTeam,
                    wins=_wins,
                    losses=_losses,
                    ties=_ties,
                    picture=_picture,
                    rank=getRank(soup, _enemyTeam, _wins, _losses, _ties),
                    iscsh=False,
                    season=Season.objects.all()[0].season,
                )
                enemyTeam.save()
            try:
                happened = cshTeam.CSH.filter(enemy=enemyTeam).filter(date=_date)[0]
            except:
                happened = None
            if happened:  # if matchup made
                if _outcome:  # if game happened
                    happened.cshScore = _cshScore
                    happened.enemyScore = _enemyScore
                    happened.outcome = _outcome
                    happened.save()
                else:
                    happened.upcoming = _upcoming
                    happened.clean_date = _clean_date
                    happened.date = _date
                    happened.save()
                if happened in matchList:
                    matchList.remove(happened)
            else:
                if _outcome:
                    matchup = Matchup(
                        csh=cshTeam,
                        enemy=enemyTeam,
                        cshScore=_cshScore,
                        enemyScore=_enemyScore,
                        outcome=_outcome,
                        clean_date=_clean_date,
                        date=_date,
                    )
                    matchup.save()
                else:
                    matchup = Matchup(
                        csh=cshTeam,
                        enemy=enemyTeam,
                        cshScore=None,
                        enemyScore=None,
                        upcoming=_upcoming,
                        clean_date=_clean_date,
                        date=_date,
                    )
                    matchup.save()
                if matchup in matchList:
                    matchList.remove(matchup)
    for match in matchList:
        match.enemy.delete()
    cshTeam.wins = _csh_wins
    cshTeam.losses = _csh_losses
    cshTeam.ties = _csh_ties
    cshTeam.rank = getRank(soup, cshTeam.name, _csh_wins, _csh_losses, _csh_ties)
    cshTeam.save()