Repository navigation
Expand file tree
/
Copy pathtrams.py
More file actions
140 lines (128 loc) · 3.49 KB
/
Copy pathtrams.py
File metadata and controls
140 lines (128 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import requests
import json
def main():
# Ask user for ATCO code
atco_code = input("Enter ATCO code (e.g. 9400ZZMAPIC): ").strip()
url = "https://api-tf.tfgm.com/"
headers = {
"Host": "api-tf.tfgm.com",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:152.0) Gecko/20100101 Firefox/152.0",
"Accept": "*/*",
"Accept-Language": "en-GB,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Referer": "https://tfgm.com/",
"Content-Type": "application/json",
"Origin": "https://tfgm.com",
"Connection": "keep-alive",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
}
# IMPORTANT: Do NOT manually set Content-Length
# requests handles it automatically
payload = {
"query": """
query GetTramStopTravelInformation($atcoCodes: [String!]!, $limit: Int!) {
tramStops: locationByAtco(atcoCodes: $atcoCodes) {
name
mode
atcoCode
... on MassTransportLocation {
lines {
... on TramLine {
id
mode
services {
name
}
}
}
departures(limit: $limit) {
trip {
__typename
mode
... on TramTrip {
carriages
destinationDisplay
}
}
timings {
scheduledDepartureTime
expectedDepartureTime
lastUpdated
status
wait
}
}
stopAlerts: alerts {
...AlertFragment
}
}
}
lineAlerts: transportModes(modes: [TRAM]) {
alerts {
...AlertFragment
}
}
}
fragment AlertFragment on Alert {
advice
description
title
impactedServices {
name
}
impactedLocations {
atcoCode
name
}
impactedServicesAtLocations {
locations {
atcoCode
name
}
services {
name
}
}
id
effect
validityScopes
validityPeriods {
start
end
}
link {
href
label
}
}
""",
"variables": {
"atcoCodes": [atco_code],
"limit": 20
},
"operationName": "GetTramStopTravelInformation"
}
try:
response = requests.post(
url,
headers=headers,
json=payload, # CRITICAL: use json= not data=
timeout=15
)
print("\nStatus Code:", response.status_code)
print("\nResponse Headers:\n", response.headers)
# Try to pretty print JSON
try:
print("\nResponse JSON:\n")
print(json.dumps(response.json(), indent=2))
except:
print("\nRaw Response:\n")
print(response.text)
except requests.exceptions.RequestException as e:
print("Request failed:", e)
if __name__ == "__main__":
main()