Add cronjob to fetch new timetable data from HSL

This commit is contained in:
Jan Tuomi
2017-01-20 15:07:26 +02:00
parent 332928ce30
commit 81a7f0a4a4
7 changed files with 51 additions and 25 deletions
+2
View File
@@ -10,3 +10,5 @@ members/migrations/*
infoscreen/migrations/* infoscreen/migrations/*
webapp/migrations/* webapp/migrations/*
.idea/ .idea/
logs/
logs/*
Executable
+11
View File
@@ -0,0 +1,11 @@
#!/bin/bash
echo "Adding cron entries..."
# write out current crontab
crontab -l > temp_crontab
# echo new cron into cron file
printf "*/5 * * * * source $PWD/../virtualenv.sikweb/bin/activate && python $PWD/manage.py hsl &>$PWD/logs/fetch_hsl.log\n\n" >> temp_crontab
# install new cron file
crontab temp_crontab
rm temp_crontab
echo "Done. View your crontab with 'crontab -l' and edit it with 'crontab -e'."
+28
View File
@@ -0,0 +1,28 @@
from infoscreen.models import HSLDataModel
from django.conf import settings
import urllib.request
import json
class HSLFetcher:
def fetch(self):
location_coords = (2545565, 6675319)
src = urllib.request.urlopen(
"http://api.reittiopas.fi/hsl/prod/?userhash={}&request=stops_area&center_coordinate={},{}"
.format(settings.HSL_USERHASH, location_coords[0], location_coords[1]))\
.read().decode("utf-8")
data = json.loads(src)
arr = []
for element in data:
src = urllib.request.urlopen(
"http://api.reittiopas.fi/hsl/prod/?userhash={}&request=stop&code={}"
.format(settings.HSL_USERHASH, element['code'])).read().decode("utf-8")
parsed = json.loads(src)[0]
arr.append({"name": parsed['name_fi'], "lines": parsed['lines'],
"dist": element['dist'], "departures": parsed['departures']})
HSLDataModel.objects.create(data=json.dumps(arr))
+3 -24
View File
@@ -1,30 +1,9 @@
import json
import urllib.request
from django.conf import settings
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from infoscreen.models import HSLDataModel from infoscreen.hsl_fetcher import HSLFetcher
class Command(BaseCommand): class Command(BaseCommand):
help = 'Loads HSL timetables and save to json file.' help = 'Loads HSL timetables and save to json file.'
def handle(self, *args, **options): def handle(self, *args, **options):
location_coords = (2545565, 6675319) fetcher = HSLFetcher()
src = urllib.request.urlopen( fetcher.fetch()
"http://api.reittiopas.fi/hsl/prod/?userhash={}&request=stops_area&center_coordinate={},{}"
.format(settings.HSL_USERHASH, location_coords[0], location_coords[1]))\
.read().decode("utf-8")
data = json.loads(src)
arr = []
for element in data:
src = urllib.request.urlopen(
"http://api.reittiopas.fi/hsl/prod/?userhash={}&request=stop&code={}"
.format(settings.HSL_USERHASH, element['code'])).read().decode("utf-8")
parsed = json.loads(src)[0]
arr.append({"name": parsed['name_fi'], "lines": parsed['lines'],
"dist": element['dist'], "departures": parsed['departures']})
HSLDataModel.objects.create(data=json.dumps(arr))
+1
View File
@@ -0,0 +1 @@
This is a log directory.
+5
View File
@@ -56,6 +56,11 @@ pip install --upgrade pip
pip install -r requirements.txt pip install -r requirements.txt
``` ```
### Register cron jobs
```
./add_cronjob.sh
```
## Configure Django settings ## Configure Django settings
+1 -1
View File
@@ -130,7 +130,7 @@ GOOGLE_RECAPTCHA_SECRET_KEY = "YOUR-PRIVATE-KEY"
#Logger level #Logger level
LOGGERLEVEL = logging.ERROR LOGGERLEVEL = logging.ERROR
LOGPATH = expanduser("~")+ "/debug.log" LOGPATH = "logs/debug.log"
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/ # https://docs.djangoproject.com/en/1.9/topics/i18n/