PageSourceSearch

https://brickblock1.github.io/ul-avgangar/main.py

js brickblock1.github.io collected 2026-10-03 08:44:12 UTC 6,554 bytes, 185 lines download raw bytes

1import asyncio, micropip
2await micropip.install("python-dateutil")
3from pyodide.http import pyfetch
4from pyscript import document, window
5from datetime import datetime, timedelta
6from dateutil import tz
7
8
9def combine_departure(departure):
10    varstr = "output_departures_" + departure["area"]
11    global departure_strings
12    #departure_strings[varstr] = departure_strings[varstr] + line_info["name"] + suffix + " mot " + line_info["towards"] + "\navgår: "+ thing[11:-4]+ " UTC trafikslag: " + traffictype["Name"] + "\n"
13    deviations = departure["deviations"]
14    combined_deviations = ""
15    for d in range (0, len(deviations)):
16        deviation = deviations[d]
17        if d == len(deviations) - 1:
18            combined_deviations = combined_deviations + deviation["header"]
19        else:
20            combined_deviations = combined_deviations + deviation["header"] + ", "
21    if line_info["trainNo"] == 0:
22        departure_strings[varstr] = departure_strings[varstr] + line_info["name"] + " " + line_info["towards"] + "\n"+ str(scheduled_a)[11:-9] + " " + shown_realtime + " " + str(combined_deviations) + "\n"
23    else:
24        if line_info["lineNo"] == 995:
25            linenumber = " 40 "
26        elif line_info["lineNo"] == 996:
27            linenumber = " 43 "
28        else:
29            linenumber = " "
30        departure_strings[varstr] = departure_strings[varstr] + traffictype["Name"] + linenumber + line_info["towards"] + " " + str(line_info["trainNo"]) + "\n"+ str(scheduled_a)[11:-9] + " " + shown_realtime + " " + str(combined_deviations) + "\n"
31
32
33def write_departure():
34    global response_dict
35    areas = response_dict["areas"]
36    for a in range (0, len(areas)):
37        area = areas[a]
38        id = "#_" + area["name"]
39        output_div2 = document.querySelector(id)
40        output_div2.innerText = departure_strings["output_departures_"+area["name"]]
41
42def write_info():
43    info_div = document.querySelector("#_Info")
44    info_div.innerText = str(current_time)[11:-16]
45
46async def onclick(event):
47    global stop
48    areas = response_dict["areas"]
49    for a in range (0, len(areas)):
50        area = areas[a]
51        target = document.getElementById(area["name"])
52        target.style.display = "none"
53    input_text = document.querySelector("#input")
54    #stop = input_text.value
55    url = document.URL
56    position = url.rfind("?")
57    if position == -1:
58        new_url=url + "?" + input_text.value
59    else:
60        new_url = url[:position] + "?" + input_text.value
61    window.history.pushState(0, 0, new_url)
62    get_query()
63    await callapi()
64    create_divs()
65    unhide_divs(1)
66    toggle_config(1)
67    
68
69async def callapi():
70    global response_dict
71    try:
72        response = await pyfetch(url="https://api.ul.se/api/v4/stop/" + stop, method="GET")
73        response_dict = await response.json()
74    except:
75        response_dict = {
76            'name': "CORS (Cross origin resource sharing) isn't enabled",
77            'areas': [{'name': "fel"}],
78            'departures': [{'hasRealTimeDepartureDeviation': False,
79                            'departureDateTime': '2023-11-16T16:47:00Z'}]
80            }
81
82def unhide_divs(event):
83    areas = response_dict["areas"]
84    for a in range (0, len(areas)):
85        area = areas[a]
86        target = document.getElementById(area["name"])
87        target.style.display = "block"
88    target = document.getElementById("hide_Info")
89    target.style.display = "block"
90
91def create_divs():
92    areas = response_dict["areas"]
93    for a in range (0, len(areas)):
94        area = areas[a]
95        if document.getElementById(area["name"]) == None:
96            div = document.createElement('div')
97            div.id = area["name"]
98            document.getElementById("container").append(div)
99            h2 = document.createElement('h2')
100            h2.innerText = "Läge " + area["name"]
101            onclick = document.createAttribute("onclick")
102            onclick.value = "hide('" + area["name"] + "')"
103            h2.setAttributeNode(onclick)
104            document.getElementById(area["name"]).append(h2)
105            p = document.createElement('p')
106            p.id = "_" + area["name"]
107            document.getElementById(area["name"]).append(p)
108
109def toggle_config(event):
110    target = document.getElementById("hide_config")
111    if target.style.display == "block":
112        target.style.display = "none"
113    else:
114        target.style.display = "block"
115
116def strip_time(time_str):
117    striped_time = datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%SZ") # 2023-11-16T16:47:00Z
118    return striped_time
119
120def reset_text():
121    global departure_strings
122    departure_strings = {}
123    global response_dict
124    areas = response_dict["areas"]
125    for a in range(0, len(areas)):
126        area = areas[a]
127        departure_strings["output_departures_" + area["name"]] = ""
128
129def get_query():
130    global stop 
131    url = document.URL
132    position = url.find("?")
133    stop = url[position + 1:]
134    len(stop)
135    if len(stop) != 6:
136        stop = "700600"
137
138def make_time_aware(time):
139    time = time.replace(tzinfo=tz.gettz("UTC"))
140    time = time.astimezone(tz.gettz("Stockholm"))
141    return time
142        
143
144get_query()
145await callapi()
146create_divs()
147unhide_divs(1)
148
149while True:
150
151    await callapi()
152
153    header_name = f" {response_dict['name']}"
154
155    reset_text()
156
157    departures = response_dict["departures"]
158    for d in range (0, len(departures)):
159        depature = departures[d]
160        hasrealtime = depature["hasRealTimeDepartureDeviation"]
161        if hasrealtime == True:
162            realtime = str(depature["realTimeDepartureDateTime"])
163            realtime_a = strip_time(realtime)
164            realtime_a = make_time_aware(realtime_a)
165            shown_realtime = str(realtime_a)[11:-9]
166        else:
167            realtime_a = strip_time("0002-01-01T00:00:00Z")
168            realtime_a = make_time_aware(realtime_a)
169            shown_realtime = ""
170        scheduled = str(depature["departureDateTime"])
171        scheduled_a = strip_time(scheduled)
172        scheduled_a = make_time_aware(scheduled_a)
173        current_time = datetime.now(tz=tz.gettz("UTC"))
174        current_time = make_time_aware(current_time)
175        if (realtime_a >= current_time) | (scheduled_a >= current_time):
176            first_departure = departures[d]
177            line_info = first_departure["line"]
178            traffictype = line_info["trafficType"]
179            combine_departure(depature)
180    write_departure()
181    write_info()
182    output_div = document.querySelector("#request_text")
183    output_div.innerText = header_name
184
185

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.