69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
import requests
|
|
from datetime import datetime
|
|
|
|
|
|
def extract_time(json):
|
|
|
|
print "extract_time"
|
|
try:
|
|
return json['DepartureTime']
|
|
except:
|
|
return -1
|
|
|
|
|
|
url = 'http://api.trafikinfo.trafikverket.se/beta/data.json'
|
|
headers = {'Content-Type': 'text/xml'}
|
|
xml_data = """<?xml version='1.0' encoding='utf-8'?>
|
|
<REQUEST>
|
|
<LOGIN authenticationkey="ac4b2399b54648d09a30db1a33fc6eda" /> <QUERY objecttype="FerryAnnouncement">
|
|
<FILTER>
|
|
<AND>
|
|
<EQ name="Route.Id" value="14" />
|
|
<AND>
|
|
<GT name="DepartureTime" value="$now"/>
|
|
<LT name ="DepartureTime" value='$dateadd(0.05:15:00)' />
|
|
</AND>
|
|
</AND>
|
|
</FILTER>
|
|
<INCLUDE>DepartureTime</INCLUDE>
|
|
<INCLUDE>Info</INCLUDE>
|
|
<INCLUDE>ToHarbor.Id</INCLUDE>
|
|
<INCLUDE>ToHarbor.Name</INCLUDE>
|
|
<INCLUDE>FromHarbor.Name</INCLUDE>
|
|
<INCLUDE>FromHarbor.Id</INCLUDE>
|
|
</QUERY>
|
|
</REQUEST>
|
|
"""
|
|
r = requests.post(url, data=xml_data, headers=headers)
|
|
|
|
|
|
if r.status_code == 200:
|
|
data = r.json()
|
|
|
|
dataFerry = data['RESPONSE']['RESULT'][0]['FerryAnnouncement']
|
|
|
|
dataFerry.sort(key=extract_time, reverse=False)
|
|
|
|
result = []
|
|
for item in dataFerry:
|
|
mydict = {}
|
|
timestamp = item['DepartureTime']
|
|
timestamp = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S")
|
|
timestamp = timestamp.strftime("%H:%M:%S")
|
|
mydict['time'] = timestamp
|
|
|
|
|
|
if 'Info' in item:
|
|
info = unicode(item['Info'][0])
|
|
info = info.decode('UTF-8')
|
|
mydict['info'] = info
|
|
|
|
result.append(mydict)
|
|
|
|
print result
|
|
|
|
|
|
else:
|
|
print "error"
|
|
print r.text
|