Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion understatdb/__init__.py → footballdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

from . import (
db,
understat
scraper
)
4 changes: 2 additions & 2 deletions understatdb/_nbdev.py → footballdb/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"db.py",
"understat.py"]

doc_url = "https://torvaney.github.io/understatdb/"
doc_url = "https://torvaney.github.io/footballdb/"

git_url = "https://github.com/torvaney/understatdb/tree/master/"
git_url = "https://github.com/torvaney/footballdb/tree/master/"

def custom_doc_links(name): return None
48 changes: 24 additions & 24 deletions understatdb/cli.py → footballdb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import pyprojroot
import typer

import understatdb
import footballdb

# Cell

Expand All @@ -40,7 +40,7 @@ def __call__(self, *args, **kwargs):
def initialize_db():
"""
Load database config from environment and initialise
`understatdb.db.DB` with a database connection.
`footballdb.db.DB` with a database connection.
"""

# Load database config from environment
Expand All @@ -54,7 +54,7 @@ def initialize_db():

# Configure proxy database to use configured postgres
typer.secho('Initialising database connection...', fg=typer.colors.BRIGHT_BLACK)
understatdb.db.DB.initialize(postgres_db)
footballdb.db.DB.initialize(postgres_db)

# Cell

Expand All @@ -71,8 +71,8 @@ def migrate(interactive: bool = True):

# Migrate database tables
typer.secho('Migrating database tables...', fg=typer.colors.BRIGHT_BLACK)
understatdb.db.DB.evolve(
ignore_tables=understatdb.db.EVOLVE_IGNORE_TABLES + dbt_tables,
footballdb.db.DB.evolve(
ignore_tables=footballdb.db.EVOLVE_IGNORE_TABLES + dbt_tables,
interactive=interactive
)
typer.secho('Done!', fg=typer.colors.GREEN, bold=True)
Expand Down Expand Up @@ -105,7 +105,7 @@ def build_tables(args: typing.List[str] = typer.Option([], help='Additional argu

# Use the list of league *values* (i.e. strings) so that the help text shows
# all the possible inputs the user can use
_DEFAULT_INGEST_LEAGUES = [l.value for l in understatdb.understat.League]
_DEFAULT_INGEST_LEAGUES = [l.value for l in footballdb.scraper.League]
_DEFAULT_INGEST_SEASONS = list(range(2014, 2021))


Expand All @@ -115,29 +115,29 @@ def ingest(
leagues: typing.List[str] = typer.Option(
_DEFAULT_INGEST_LEAGUES,
help='Leagues to import',
callback=lambda xs: [understatdb.understat.League(x) for x in xs]
callback=lambda xs: [footballdb.scraper.League(x) for x in xs]
),
seasons: typing.List[int] = typer.Option(
_DEFAULT_INGEST_SEASONS,
help='Seasons to import (by start year)'
),
):
""" Ingest match and shot data from Understat.com """
""" Ingest match and shot data """

initialize_db()
client = understatdb.understat.Understat()
client = footballdb.scraper.FootballScraper()

for league, season in itertools.product(
[understatdb.understat.League(l) for l in leagues],
[footballdb.scraper.League(l) for l in leagues],
seasons
):
# Add league & season to DB
with understatdb.db.DB.atomic():
db_league, _ = understatdb.db.League.get_or_create(name=league.value)
db_season, _ = understatdb.db.Season.get_or_create(name=season)
with footballdb.db.DB.atomic():
db_league, _ = footballdb.db.League.get_or_create(name=league.value)
db_season, _ = footballdb.db.Season.get_or_create(name=season)

# Check if a record for this league and season already exists. If so, skip it.
existing_record = understatdb.db.Matches.get_or_none(
existing_record = footballdb.db.Matches.get_or_none(
league_id=db_league.id,
season_id=db_season.id
)
Expand All @@ -151,23 +151,23 @@ def ingest(

# Add match and shot data to DB
typer.secho(f'Ingesting data for {league.value}, {season}', fg=typer.colors.BLUE)
with understatdb.db.DB.atomic():
with footballdb.db.DB.atomic():

# Fetch match data from understat
matches = client.matches(league, season)

# Delete any old match data
if refresh:
understatdb.db.Matches.delete().where(
(understatdb.db.Matches.league_id==db_league.id) &
(understatdb.db.Matches.season_id==db_season.id)
footballdb.db.Matches.delete().where(
(footballdb.db.Matches.league_id==db_league.id) &
(footballdb.db.Matches.season_id==db_season.id)
).execute()

db_matches = understatdb.db.Matches.create(
db_matches = footballdb.db.Matches.create(
league_id=db_league.id,
season_id=db_season.id,
json=matches,
version=understatdb.__version__
version=footballdb.__version__
)

with typer.progressbar(matches, label="Shots") as progress:
Expand All @@ -187,14 +187,14 @@ def ingest(

# Delete any old shots data
if refresh:
understatdb.db.Shots.delete().where(
understatdb.db.Shots.match_id==match_id
footballdb.db.Shots.delete().where(
footballdb.db.Shots.match_id==match_id
).execute()

db_shots = understatdb.db.Shots.create(
db_shots = footballdb.db.Shots.create(
match_id=match_id,
json=shots,
version=understatdb.__version__
version=footballdb.__version__
)

# Rebuild tables in dbt
Expand Down
2 changes: 1 addition & 1 deletion understatdb/db.py → footballdb/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def prefixed_snake_case(prefix, cls):
@evolve_ignore
class BaseModel(peewee.Model):
"""
A model for base (json) data from Understat
A model for base (json) football match data
"""
class Meta:
database = DB
Expand Down
6 changes: 3 additions & 3 deletions understatdb/understat.py → footballdb/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def extract_json(soup, json_var):
# 'Competition' might be a better name, but let's stick with understat's terminology
class League(enum.Enum):
"""
Understat leagues
Football leagues
"""
EPL = 'EPL'
LA_LIGA = 'La_Liga'
Expand All @@ -47,9 +47,9 @@ class League(enum.Enum):

# Cell

class Understat:
class FootballScraper:
"""
Fetches understat data webpages
Fetches football data webpages
"""

def __init__(self, base_url: str='https://understat.com'):
Expand Down
6 changes: 3 additions & 3 deletions settings.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[DEFAULT]
# All sections below are required unless otherwise specified
host = github
lib_name = understatdb
repo_name = understat-db
lib_name = footballdb
repo_name = football-postgres

user = torvaney
description = An extendable project for creating a database of soccer data
Expand Down Expand Up @@ -39,7 +39,7 @@ dev_requirements =
nbdev

console_scripts =
understat-db=understatdb.cli:app
football-postgres=footballdb.cli:app
# Optional. Same format as setuptools dependency-links
# dep_links =

Expand Down