spansh.py: refactoring and cleanup
It’s now a bit less quick & dirty. Still has no error handling beyond “works” or “doesn’t work”.
This commit is contained in:
parent
cf3fb80238
commit
7ee4b28c59
1 changed files with 23 additions and 12 deletions
35
spansh.py
35
spansh.py
|
@ -1,19 +1,19 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# PYTHON_ARGCOMPLETE_OK
|
# PYTHON_ARGCOMPLETE_OK
|
||||||
import argcomplete, argparse
|
import argcomplete, argparse
|
||||||
|
import json as JSON
|
||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
|
|
||||||
def getOldStations(count, systemsonly):
|
def getOldStations(systemsonly):
|
||||||
url = "http://backend3.spansh.co.uk:3000/api/stations/search"
|
params = {"json": JSON.dumps({"filters": FILTERS, "sort": SORT, "size": COUNT})}
|
||||||
params = {"json": '{"filters":{"updated_at":{"value":["2017-11-06","2019-03-01"],"comparison":"<=>"}},"sort":[{"updated_at":{"direction":"asc"}}],"size":' + str(count) + '}'}
|
json = requests.post(APIURL, params).json()
|
||||||
|
|
||||||
json = requests.post(url, params).json()
|
|
||||||
|
|
||||||
ret =""
|
ret =""
|
||||||
|
|
||||||
if systemsonly:
|
if systemsonly:
|
||||||
for station in json["results"]:
|
for station in json["results"]:
|
||||||
ret += "{}\n".format(station["system_name"])
|
ret += "{}\n".format(station["system_name"])
|
||||||
|
@ -25,13 +25,11 @@ def getOldStations(count, systemsonly):
|
||||||
return ret[:-1]
|
return ret[:-1]
|
||||||
|
|
||||||
def getOldStationsInSystem(system):
|
def getOldStationsInSystem(system):
|
||||||
url = "http://backend3.spansh.co.uk:3000/api/stations/search"
|
FILTERS.update(system_name={"value": system})
|
||||||
params = {"json": '{"filters": {"system_name": {"value": "' + system + '"},"updated_at":{"value":["2017-11-06","2019-03-01"],"comparison":"<=>"}},"sort":[{"updated_at":{"direction":"asc"}}]}'}
|
params = {"json": JSON.dumps({"filters": FILTERS, "sort": SORT})}
|
||||||
|
json = requests.post(APIURL, params).json()
|
||||||
json = requests.post(url, params).json()
|
|
||||||
|
|
||||||
ret =""
|
ret =""
|
||||||
|
|
||||||
for station in json["results"]:
|
for station in json["results"]:
|
||||||
ret += "{}, ".format(station["name"])
|
ret += "{}, ".format(station["name"])
|
||||||
|
|
||||||
|
@ -60,12 +58,25 @@ args = parser.parse_args()
|
||||||
|
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
|
|
||||||
|
APIURL = "http://backend3.spansh.co.uk:3000/api/stations/search"
|
||||||
|
|
||||||
|
FILTERS = {"updated_at":
|
||||||
|
{"value":
|
||||||
|
["2017-11-06",
|
||||||
|
(datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")],
|
||||||
|
"comparison": "<=>"}}
|
||||||
|
SORT = {"updated_at": {"direction": "asc"}}
|
||||||
|
COUNT = args.count
|
||||||
|
|
||||||
out =""
|
out =""
|
||||||
if args.subcommand == "oldstations":
|
if args.subcommand == "oldstations":
|
||||||
if args.system:
|
if args.system:
|
||||||
out = getOldStationsInSystem(args.system)
|
out = getOldStationsInSystem(args.system)
|
||||||
else:
|
else:
|
||||||
out = getOldStations(args.count, args.systemlist)
|
out = getOldStations(args.systemlist)
|
||||||
|
else:
|
||||||
|
parser.print_usage(sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
if out == "":
|
if out == "":
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
|
|
Loading…
Reference in a new issue