<commit_before>import logging

from bs4 import BeautifulSoup

from core.models import Matchday, Match, MatchTeamStatistics
from core.parsers.base_parser import BaseParser

logger = logging.getLogger(__name__)


class MatchParser(BaseParser):
    def __init__(self, html_source, user, is_home_match):
        self.html_source = html_source
        self.user = user
        self.is_home_match = is_home_match

    def parse(self):
        soup = BeautifulSoup(self.html_source, "html.parser")
        return self.parse_html(soup)

    def parse_html(self, soup):
        """
        :param soup: BeautifulSoup of match page
        :return: parsed match
        :rtype: Match
        """

        # we assume to have parsed the season beforehand (via matchday)
        season = Matchday.objects.all()[0].season
        matchday_number = soup.find_all('tbody')[2].find_all('b')[0].get_text().split(',')[1].split('.')[0].strip()
        matchday, success = Matchday.objects.get_or_create(season=season, number=matchday_number)

        venue = soup.find_all('em')[1].get_text()
        match_result = soup.find_all('table')[5].find_all('tr')[0].find_all('td')[3].div.font.get_text()
        home_team_score = match_result.split(':')[0]
        guest_team_score = match_result.split(':')[1]
        home_team_name = soup.find_all('td', class_='erganz')[0].get_text().strip()
        guest_team_name = soup.find_all('td', class_='erganz')[1].get_text().strip()
        strength = soup.find_all('table')[5].find_all('tr')[5].find_all('b')
        home_team_strength = strength[0].get_text().split(':')[1].strip()
        guest_team_strength = strength[1].get_text().split(':')[1].strip()
        ball_possesions = soup.find_all('table')[5].find_all('tr')[6].find_all('b')
        home_team_ball_possession = ball_possesions[0].get_text().replace(',', '.').replace('%', '').strip()
        guest_team_ball_possession = ball_possesions[1].get_text().replace(',', '.').replace('%', '').strip()
        chances = soup.find_all('table')[5].find_all('tr')[7].find_all('b')
        home_team_chances = chances[0].get_text().strip()
        guest_team_chances = chances[1].get_text().strip()
        yellow_cards = soup.find_all('table')[5].find_all('tr')[8].find_all('b')
        home_team_yellow_cards = yellow_cards[0].get_text().strip()
        guest_team_yellow_cards = yellow_cards[1].get_text().strip()
        red_cards = soup.find_all('table')[5].find_all('tr')[9].find_all('b')
        home_team_red_cards = red_cards[0].get_text().strip()
        guest_team_red_cards = red_cards[1].get_text().strip()

        home_team_stat, success = MatchTeamStatistics.objects.get_or_create(
            score=home_team_score,
            team_name=home_team_name,
            strength=home_team_strength,
            ball_possession=home_team_ball_possession,
            chances=home_team_chances,
            yellow_cards=home_team_yellow_cards,
            red_cards=home_team_red_cards
        )

        guest_team_stat, success = MatchTeamStatistics.objects.get_or_create(
            score=guest_team_score,
            team_name=guest_team_name,
            strength=guest_team_strength,
            ball_possession=guest_team_ball_possession,
            chances=guest_team_chances,
            yellow_cards=guest_team_yellow_cards,
            red_cards=guest_team_red_cards
        )

        existing_match = Match.objects.filter(matchday=matchday, user=self.user)

        if existing_match:
            existing_match.home_team_statistics = home_team_stat
            existing_match.guest_team_statistics = guest_team_stat
            existing_match.venue = venue
            existing_match.save()
            return existing_match
        else:
            match, success = Match.objects.get_or_create(
                matchday=matchday,
                is_home_match=self.is_home_match,
                user=self.user,
                home_team_statistics=home_team_stat,
                guest_team_statistics=guest_team_stat
            )
            match.venue = venue
            match.save()
            return match<commit_msg>#86 fixed match parser for existint matches<commit_after>import logging

from bs4 import BeautifulSoup

from core.models import Matchday, Match, MatchTeamStatistics
from core.parsers.base_parser import BaseParser

logger = logging.getLogger(__name__)


class MatchParser(BaseParser):
    def __init__(self, html_source, user, is_home_match):
        self.html_source = html_source
        self.user = user
        self.is_home_match = is_home_match

    def parse(self):
        soup = BeautifulSoup(self.html_source, "html.parser")
        return self.parse_html(soup)

    def parse_html(self, soup):
        """
        :param soup: BeautifulSoup of match page
        :return: parsed match
        :rtype: Match
        """

        # we assume to have parsed the season beforehand (via matchday)
        season = Matchday.objects.all()[0].season
        matchday_number = soup.find_all('tbody')[2].find_all('b')[0].get_text().split(',')[1].split('.')[0].strip()
        matchday, success = Matchday.objects.get_or_create(season=season, number=matchday_number)

        venue = soup.find_all('em')[1].get_text()
        match_result = soup.find_all('table')[5].find_all('tr')[0].find_all('td')[3].div.font.get_text()
        home_team_score = match_result.split(':')[0]
        guest_team_score = match_result.split(':')[1]
        home_team_name = soup.find_all('td', class_='erganz')[0].get_text().strip()
        guest_team_name = soup.find_all('td', class_='erganz')[1].get_text().strip()
        strength = soup.find_all('table')[5].find_all('tr')[5].find_all('b')
        home_team_strength = strength[0].get_text().split(':')[1].strip()
        guest_team_strength = strength[1].get_text().split(':')[1].strip()
        ball_possesions = soup.find_all('table')[5].find_all('tr')[6].find_all('b')
        home_team_ball_possession = ball_possesions[0].get_text().replace(',', '.').replace('%', '').strip()
        guest_team_ball_possession = ball_possesions[1].get_text().replace(',', '.').replace('%', '').strip()
        chances = soup.find_all('table')[5].find_all('tr')[7].find_all('b')
        home_team_chances = chances[0].get_text().strip()
        guest_team_chances = chances[1].get_text().strip()
        yellow_cards = soup.find_all('table')[5].find_all('tr')[8].find_all('b')
        home_team_yellow_cards = yellow_cards[0].get_text().strip()
        guest_team_yellow_cards = yellow_cards[1].get_text().strip()
        red_cards = soup.find_all('table')[5].find_all('tr')[9].find_all('b')
        home_team_red_cards = red_cards[0].get_text().strip()
        guest_team_red_cards = red_cards[1].get_text().strip()

        home_team_stat, success = MatchTeamStatistics.objects.get_or_create(
            score=home_team_score,
            team_name=home_team_name,
            strength=home_team_strength,
            ball_possession=home_team_ball_possession,
            chances=home_team_chances,
            yellow_cards=home_team_yellow_cards,
            red_cards=home_team_red_cards
        )

        guest_team_stat, success = MatchTeamStatistics.objects.get_or_create(
            score=guest_team_score,
            team_name=guest_team_name,
            strength=guest_team_strength,
            ball_possession=guest_team_ball_possession,
            chances=guest_team_chances,
            yellow_cards=guest_team_yellow_cards,
            red_cards=guest_team_red_cards
        )

        existing_match = Match.objects.filter(matchday=matchday, user=self.user)

        if existing_match:
            match = existing_match[0]
            match.home_team_statistics = home_team_stat
            match.guest_team_statistics = guest_team_stat
        else:
            match, success = Match.objects.get_or_create(
                matchday=matchday,
                is_home_match=self.is_home_match,
                user=self.user,
                home_team_statistics=home_team_stat,
                guest_team_statistics=guest_team_stat
            )

        match.venue = venue
        match.save()
        return match
{'fraction_non_alphanumeric': 0.1291778482556721, 'fraction_numerical': 0.009148572822639669, 'mean_word_length': 3.0250368188512518, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6511743', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2621, 'n_words': 334}