Litet Pythonskript som exemplifierar:
Kod:
#!/usr/bin/env python
import requests
import urllib.parse
stations = []
placeIds = []
stations.append(input("Från vilken station? "))
stations.append(input("Till vilken station? "))
def fetchStationId(search_string):
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
encoded_search_string = urllib.parse.quote(search_string)
url = f'https://services.c.web.sl.se/api/locationlookup?search={encoded_search_string}'
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as error:
print(f'Error fetching location lookup: {error}')
return None
def fetchSLdata(place1, place2):
url = "https://services.c.web.sl.se/api/journeyplanner"
headers = {
"accept": "application/json, text/plain, */*",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "sv-SE,sv;q=0.8",
"cache-control": "no-cache",
"content-length": "192",
"content-type": "application/json",
"origin": "https://sl.se",
"pragma": "no-cache",
"priority": "u=1, i",
"referer": "https://sl.se/",
"sec-ch-ua": '"Brave";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"sec-gpc": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
}
payload = {
"destination": {
"placeId": place2
},
"origin": {
"placeId": place1
},
"searchForArrival": False,
"transportTypes": ["METRO", "BUS", "TRAIN", "TRAM", "SHIP", "NARBUSS"]
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
print("Request successful!")
return(response.json())
else:
print(f"Failed with status code: {response.status_code}")
print(response.text)
return(false)
for station in stations:
placeIds.append(fetchStationId(station)[0]["placeId"])
jsonData = fetchSLdata(placeIds[0],placeIds[1])
print(jsonData)