<commit_before>from bs4 import BeautifulSoup

from core.models import Player, Matchday, Season, Country, PlayerUserOwnership
from core.parsers.base_parser import BaseParser


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

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

    def parse_players_html(self, soup):
        """
        :param soup: BeautifulSoup of player page
        :return: parsed players
        :rtype: list
        """
        players_table = soup.find(id="playerTable").tbody
        player_list = players_table.find_all('tr')  # 1 row per player
        return [self.parse_player_row(player_row) for player_row in player_list]

    def parse_player_row(self, player_row):
        matchday = Matchday.objects.all()[0]

        player_values = self._filter_invalid_cells(player_row.find_all('td'))
        ofm_id = player_row.find_all('input', class_='playerid')[0]['value']
        name = player_values[6].a.get_text().replace('\n', '').replace('\t', '').strip(' ')
        position = player_values[5].find_all('span')[1].get_text()
        age = int(player_values[7].get_text())
        birth_season, success = Season.objects.get_or_create(number=matchday.season.number - age)

        displayed_country = player_values[8].get_text().replace('\n', '').replace('\t', '').strip(' ')
        country_name = ''.join([i for i in displayed_country if not i.isdigit()])
        country_choices = dict(Country._meta.get_field('country').choices)
        country_no = list(country_choices.keys())[list(country_choices.values()).index(country_name)]
        nationality, success = Country.objects.get_or_create(country=country_no)

        player, success = Player.objects.get_or_create(
            id=ofm_id,
            name=name,
            position=position,
            birth_season=birth_season,
            nationality=nationality
        )

        self._create_player_user_ownership(player, matchday)

        return player

    def _create_player_user_ownership(self, player, matchday):
        contract = PlayerUserOwnership.objects.get(player=player, user=self.user, sold_on_matchday=None)
        if contract is None:
            contract, success = PlayerUserOwnership.objects.get_or_create(player=player, user=self.user, bought_on_matchday=matchday)
        return contract<commit_msg>revert to previously running code<commit_after>from bs4 import BeautifulSoup

from core.models import Player, Matchday, Season, Country, PlayerUserOwnership
from core.parsers.base_parser import BaseParser


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

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

    def parse_players_html(self, soup):
        """
        :param soup: BeautifulSoup of player page
        :return: parsed players
        :rtype: list
        """
        players_table = soup.find(id="playerTable").tbody
        player_list = players_table.find_all('tr')  # 1 row per player
        return [self.parse_player_row(player_row) for player_row in player_list]

    def parse_player_row(self, player_row):
        matchday = Matchday.objects.all()[0]
        player_values = self._filter_invalid_cells(player_row.find_all('td'))
        ofm_id = player_row.find_all('input', class_='playerid')[0]['value']
        name = player_values[6].a.get_text().replace('\n', '').replace('\t', '').strip(' ')
        position = player_values[5].find_all('span')[1].get_text()
        age = int(player_values[7].get_text())
        birth_season, success = Season.objects.get_or_create(number=matchday.season.number - age)

        displayed_country = player_values[8].get_text().replace('\n', '').replace('\t', '').strip(' ')
        country_name = ''.join([i for i in displayed_country if not i.isdigit()])
        country_choices = dict(Country._meta.get_field('country').choices)
        country_no = list(country_choices.keys())[list(country_choices.values()).index(country_name)]
        nationality, success = Country.objects.get_or_create(country=country_no)

        player, success = Player.objects.get_or_create(
            id=ofm_id,
            name=name,
            position=position,
            birth_season=birth_season,
            nationality=nationality
        )

        self._create_player_user_ownership(player, matchday)

        return player

    def _create_player_user_ownership(self, player, matchday):
        existing_contracts = PlayerUserOwnership.objects.filter(player=player, user=self.user, sold_on_matchday=None)
        if existing_contracts.count() > 0:
            contract = existing_contracts[0]
        else:
            contract, success = PlayerUserOwnership.objects.get_or_create(player=player, user=self.user, bought_on_matchday=matchday)
        return contract
{'fraction_non_alphanumeric': 0.11577082102776137, 'fraction_numerical': 0.003937783028155149, 'mean_word_length': 3.345594525235244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '3492224', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1509, 'n_words': 273}