def get_stations(cls, query):
if query.isdigit():
query = 'nom:{query} OR idstation:{query}'.format(query=query)
else:
# Querying idstation with a string value raises an error, we remove this field
query = 'nom:{}'.format(query)
req = requests.get(settings.STAR_API_BASE_URL, params={'apikey': settings.STAR_API_KEY,
'dataset': 'vls-stations-etat-tr',
'fields': 'idstation,nom',
'q': query,
'sort': 'nom',
'timezone': timezone.get_current_timezone_name()})
if not req.ok:
return ()
data = req.json()
if not data.get('nhits', 0):
# No matching data found
return ()
return [(item['fields']['idstation'], item['fields']['nom']) for item in data.get('records', [])]
评论列表
文章目录