Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Dec 22, 2023
0 parents commit dce3884
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Docker

on:
workflow_dispatch:
push:
branches:
- main
permissions:
actions: write
contents: read
packages: write

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build:
name: 🐳 Build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
with:
access_token: ${{ github.token }}

- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: 🔑 Github Registry Auth
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: 👀 Extract metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: 🐳 Build
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
COMMIT_SHA=${{ github.sha }}
BRANCH=${{ github.head_ref || github.ref_name }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.db
*.json
*.csv
__pycache__
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.9

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./app /code/app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Niklas Marion

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from typing import Union

from fastapi import FastAPI
import sqlite3
from typing import Optional

app = FastAPI(title="DLV", docs_url="/swagger",
openapi_url="/swagger-json", redoc_url=None)


def create_connection():
conn = sqlite3.connect("file:stammdaten.db?mode=ro",
uri=True, isolation_level='IMMEDIATE')
return conn


def query_db(query, args=(), one=False):
cur = create_connection().cursor()
cur.execute(query, args)
r = [dict((cur.description[i][0], value)
for i, value in enumerate(row)) for row in cur.fetchall()]
cur.connection.close()
return (r[0] if r else None) if one else r


@app.get("/clubs/{lv}")
def read_clubs_by_lv(lv: str, q: Union[str, None] = None):
query = f"SELECT * FROM Club WHERE lv = '{lv}'"
clubs = query_db(query)
return clubs


@app.get("/lv")
def read_lv():
query = f"SELECT DISTINCT lv FROM Club"
connection = create_connection()
lv = connection.execute(query).fetchall()
connection.close()
lv = [item for t in lv for item in t]
return lv


@app.get("/athletes/{guid}")
def get_athlete_by_guid(guid: str):
query = f"SELECT * FROM Athlete WHERE guid = '{guid}'"
athlete = query_db(query)
if (len(athlete) == 0):
return None
return athlete[0]


@app.get("/athletes")
def get_athletes(
firstname: Optional[str] = None,
lastname: Optional[str] = None,
clubId: Optional[str] = None,
worldAthleticsId: Optional[int] = None,
lv: Optional[str] = None,
limit: Optional[int] = 100,
page: Optional[int] = 0,
):
query = "SELECT Athlete.*,C.name as club,lv FROM Athlete JOIN main.Club C on Athlete.clubId = C.id"

conditions = []

if firstname:
conditions.append(f"firstname LIKE '%{firstname}%'")
if lastname:
conditions.append(f"lastname LIKE '%{lastname}%'")
if clubId:
conditions.append(f"clubId = '{clubId}'")
if worldAthleticsId:
conditions.append(f"worldAthleticsId = '{worldAthleticsId}'")
if lv:
conditions.append(f"lv = '{lv}'")

if conditions:
query += " WHERE " + " AND ".join(conditions)

if (limit > 100):
limit = 100

query += f" LIMIT {limit} OFFSET {page * limit}"

athletes = query_db(query)
return athletes
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pandas==2.1.4
fastapi==0.105.0
uvicorn[standard]==0.25.0
62 changes: 62 additions & 0 deletions taf2db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pandas as pd
import sqlite3
import os
from pathlib import Path
import argparse

def main(taf_path) -> None:
conn = sqlite3.connect("stammdaten.db")

clubs_path = Path(taf_path).joinpath("Settings/base_clubs.json")

with open(clubs_path, encoding="utf-16-le", errors='ignore') as f:
data = f.read()
data = data[11:-1]
with open('clubs.json', 'w', encoding="utf-8") as f:
f.write(data)

df = pd.read_json('clubs.json', encoding="utf-8-sig")
df = df[["LV", "Name", "ShortName", "Code", "Type"]]
df['Type'] = df['Type'].apply(lambda x: "CLUB" if x == 0 else "LG")

df = df.rename(columns={"LV": "lv", "Name": "name", "ShortName": "shortName", "Code": "id", "Type": "type"})

df.to_sql('Club', conn, if_exists='replace', index=False)
os.remove('clubs.json')

athletes_path = Path(taf_path).joinpath("Settings/base_athletes.json")

with open(athletes_path, encoding="utf-16-le", errors='ignore') as f:
data = f.read()
data = data[11:-1]
with open('athletes.json', 'w', encoding="utf-8") as f:
f.write(data)

df = pd.read_json('athletes.json', encoding="utf-8-sig")
df['WorldAthleticsId'] = df['WorldAthleticsId'].fillna(0)

df = df[df['Code'] != '']
df = df[df['ExternalId'].notnull()]

df = df[["ExternalId", "Code", "Firstname", "Lastname", "ClubCode", "Nation", "Yob", "Gender", "WorldAthleticsId"]]

df['WorldAthleticsId'] = df['WorldAthleticsId'].apply(lambda x: None if x == 0 else int(x))
df["Gender"] = df['Gender'].apply(lambda x: "M" if x == 0 else "W")

df = df.rename(columns={"Code": "id", "ExternalId": "guid", "Firstname": "firstname", "Lastname": "lastname", "ClubCode": "clubId", "Nation": "country", "Yob": "birthyear", "Gender": "sex", "WorldAthleticsId": "worldAthleticsId" })

df.to_sql('Athlete', conn, if_exists='replace', index=False)
os.remove('athletes.json')


conn.execute("DELETE FROM Athlete WHERE guid IN (SELECT guid FROM Athlete JOIN main.Club C on Athlete.clubId = C.id WHERE lv='')")
conn.execute("DELETE FROM Club WHERE lv=''");
conn.commit()
conn.close()

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--taf", required=True, dest="taf_path", help="Path to TAF folder")
args = parser.parse_args()

main(args.taf_path)

0 comments on commit dce3884

Please sign in to comment.