diff --git a/.gitignore b/.gitignore index 4d19556..e094e6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .env -secrets.yaml \ No newline at end of file +secrets.yaml +.venv/* +output/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4d6e47d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Install cron +RUN apt-get update && apt-get install -y cron + +# Set the working directory +WORKDIR /usr/cta-archive/ + +# Copy the current directory contents into the container +COPY . . + +# Install any needed packages +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the cron job file into the cron.d directory +COPY cronjob /etc/cron.d/cronjob + +# Give execution rights on the cron job +RUN chmod 0644 /etc/cron.d/cronjob + +# Apply the cron job +RUN crontab /etc/cron.d/cronjob + +# Create the log file to be able to run tail +RUN touch /var/log/cron.log + +# Run the command on container startup +CMD cron && tail -f /var/log/cron.log \ No newline at end of file diff --git a/archive.py b/archive.py new file mode 100644 index 0000000..c1dc8bb --- /dev/null +++ b/archive.py @@ -0,0 +1,29 @@ +import requests +import datetime +import os +import json + +# Import API key from .env file +api_key=os.environ['API_KEY'] + +# Get latest URL for each route, source for routes:https://www.transitchicago.com/developers/ttdocs/ +routes= ['Red', 'Blue', 'Brn', 'G', 'Org', 'P', 'Pink', 'Y'] + +for route in routes: + response = requests.get("https://lapi.transitchicago.com/api/1.0/ttpositions.aspx?key="+api_key+"&rt="+route+"&outputType=JSON") + + trains = response.json() + #trains_query_time = datetime.datetime.fromisoformat(trains['ctatt']['tmst']) + trains_query_time = trains['ctatt']['tmst'] + + outdir = "./output/"+route+"/" + if not os.path.exists(outdir): + os.makedirs(outdir) + + outfile = outdir+trains_query_time+"_location_"+route+".json" + # Lazy format simplify + outfile = outfile.replace(":","") + outfile = outfile.replace("-","") + + with open(outfile,mode='w') as f: + json.dump(trains, f) diff --git a/cronjob b/cronjob new file mode 100644 index 0000000..0b72777 --- /dev/null +++ b/cronjob @@ -0,0 +1 @@ +2 * * * * /usr/cta-archive/archive.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9a11b49 Binary files /dev/null and b/requirements.txt differ