Skip to content

Commit

Permalink
Skip inactive stations, remove locations file
Browse files Browse the repository at this point in the history
  • Loading branch information
clampr committed Feb 23, 2024
1 parent 188834b commit 3fe29da
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 33 deletions.
25 changes: 6 additions & 19 deletions .github/workflows/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,11 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
- name: Mirror files
uses: SamKirkland/[email protected]
- name: Upload stations.db
uses: bayssmekanique/action-simple-file-upload@v2
with:
server: ${{ secrets.FTP_SERVER }}
username: ${{ secrets.FTP_USER }}
user: ${{ secrets.FTP_USER }}
password: ${{ secrets.FTP_PASSWORD }}
state-name: .sync
exclude: |
**/.git*
**/.git*/**
**/*.py
**/*.sql
.github
.github/**
lib
lib/**
scripts
scripts/**
.pylintrc
README.md
requirements.txt
host: ${{ secrets.FTP_SERVER }}
src: stations.db
dest: stations.db
15 changes: 3 additions & 12 deletions database.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import os
import json
import sqlite3
import pandas as pd


STATIONS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "stations"))
QUERY_TABLES_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "tables.sql"))
QUERY_LOCATIONS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "locations.sql"))
DATABASE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "stations.db"))
LOCATIONS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "locations.csv.gz"))

# Purge DB file
if os.path.exists(DATABASE_PATH):
os.remove(DATABASE_PATH)

# Purge locations file
if os.path.exists(LOCATIONS_PATH):
os.remove(LOCATIONS_PATH)

# Connect to SQLite
conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()
Expand All @@ -32,6 +25,9 @@
# Read file and parse JSON
with open(os.path.join(dirpath, filename), "r", encoding="utf-8") as f:
data: dict = json.load(f)
# Skip inactive stations
if data.get('active', False) == False:
continue
# Insert into stations
cursor.execute(
"""INSERT INTO `stations` VALUES (?, ?, ?, ?, ?, ?, ?)""",
Expand All @@ -53,10 +49,5 @@
# Commit changes to database
conn.commit()

# Extract locations
with open(QUERY_LOCATIONS_PATH, "r", encoding="utf-8") as f:
df = pd.read_sql(f.read(), conn, index_col='id')
df.to_csv(LOCATIONS_PATH, compression='gzip', header=True)

# Close connection
conn.close()
Binary file removed locations.csv.gz
Binary file not shown.
1 change: 0 additions & 1 deletion locations.sql

This file was deleted.

1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
pandas

0 comments on commit 3fe29da

Please sign in to comment.