Skip to content

Commit

Permalink
update teams club on import_league
Browse files Browse the repository at this point in the history
  • Loading branch information
djbrown authored Mar 29, 2024
1 parent 20cbca5 commit 89578da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/base/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ def parse_club_option(option: str) -> tuple[str, int]:
raise ValueError(f'invalid club option text: {option}')


def parse_team_club_name(team_name: str) -> str:
match: re.Match[str] | None = re.match(r"^(.*?)( \d)?$", team_name)
if match:
return match.group(1)
raise ValueError(f'cannot parse team club name: {team_name}')


def parse_team_bhv_id(link: _Element) -> int:
return int(parse_link_query_item(link, 'teamID'))

Expand Down
6 changes: 5 additions & 1 deletion src/leagues/management/commands/import_leagues.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from datetime import date, datetime, timedelta

from clubs.models import Club
from django.core.management import BaseCommand
from django.db import transaction

Expand Down Expand Up @@ -200,11 +201,14 @@ def scrape_team(link, league):
bhv_id = parsing.parse_team_bhv_id(link)
name = link.text

club_name = parsing.parse_team_club_name(name)
club = Club.objects.filter(name=club_name).first()

url = Team.build_source_url(league.bhv_id, bhv_id)
html = http.get_text(url)
dom = parsing.html_dom(html)
game_rows = parsing.parse_game_rows(dom)
short_team_names = parsing.parse_team_short_names(game_rows)
short_team_name = Team.find_matching_short_name(name, short_team_names)

Team.create_or_update_team(name, short_team_name, league, bhv_id, LOGGER)
Team.create_or_update_team(name, short_team_name, league, club, bhv_id, LOGGER)
8 changes: 6 additions & 2 deletions src/teams/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def build_source_url(league_bhv_id, team_bhv_id):
return f'{settings.ROOT_SOURCE_URL}Spielbetrieb/index.php?orgGrpID=1&score={league_bhv_id}&teamID={team_bhv_id}'

@staticmethod
def create_or_update_team(name, short_name, league, bhv_id, logger: logging.Logger = logging.getLogger()):
def create_or_update_team(name, short_name, league, club, bhv_id, logger: logging.Logger = logging.getLogger()):
team = Team.objects.filter(bhv_id=bhv_id).first()
if team is None:
team = Team.objects.create(name=name, short_name=short_name, league=league, bhv_id=bhv_id)
team = Team.objects.create(name=name, short_name=short_name, league=league, club=club, bhv_id=bhv_id)
logger.info('CREATED Team: %s', team)
return

Expand All @@ -49,6 +49,10 @@ def create_or_update_team(name, short_name, league, bhv_id, logger: logging.Logg
team.short_name = short_name
updated = True

if team.club != club:
team.club = club
updated = True

if updated:
team.save()
logger.info('UPDATED Team: %s', team)
Expand Down

0 comments on commit 89578da

Please sign in to comment.