Skip to content

Instantly share code, notes, and snippets.

@ignaciobll
Created June 8, 2019 23:05
Show Gist options
  • Select an option

  • Save ignaciobll/76133567c8a30d9b4da33b307817be94 to your computer and use it in GitHub Desktop.

Select an option

Save ignaciobll/76133567c8a30d9b4da33b307817be94 to your computer and use it in GitHub Desktop.
import requests
import json
import sys
base = "https://openapi.emtmadrid.es/v1"
def login(email, password):
headers = {'email' : email, 'password': password }
r = requests.get(url = base + "/mobilitylabs/user/login/", headers = headers)
if r.status_code != 200:
raise Exception("Status code: {}, response:\n{}".format(r.status_code, r.text))
j = json.loads(r.text)
return j['data'][0]['accessToken']
def get_stop_info(token, stopId):
headers = {'accessToken': token}
endpoint = "/transport/busemtmad/stops/{}/detail/".format(stopId)
r = requests.get(url = base + endpoint, headers = headers)
return json.loads(r.text)
def get_bus_arrives_at_stop(token, stopId):
headers = {'accessToken': token}
info = get_stop_info(token, stopId)
data = info['data'][0]['stops'][0]
lines = [line['label'] for line in data['dataLine']]
for line in lines:
endpoint = "/transport/busemtmad/stops/{}/arrives/".format(stopId, line)
body = {
'Text_EstimationsRequired_YN': 'Y'
}
r = requests.post(url = base + endpoint, headers = headers, data=body)
print(json.loads(r.text))
def main():
stopId = int(sys.argv[1])
with open('credentials.secure', 'r') as cred:
email,password = cred.readline().split(':') # Formato user:pass
token = login(email, password)
info = get_stop_info(token, stopId)
data = info['data'][0]['stops'][0]
print("{} ({})".format(data['name'], data['stop']))
lines = [line['label'] for line in data['dataLine']]
print("Lineas: {}".format(" ".join(lines)))
# print(json.dumps(info))
# get_bus_arrives_at_stop(token, stopId)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment