122 lines
2.9 KiB
Python
122 lines
2.9 KiB
Python
from math import floor
|
|
from datetime import datetime
|
|
from dateutil import parser
|
|
|
|
class Filter:
|
|
|
|
storage = dict()
|
|
|
|
@staticmethod
|
|
def get(name: str):
|
|
if name in Filter.storage:
|
|
return Filter.storage[name]
|
|
return None
|
|
|
|
def __init__(self, name: str, filters):
|
|
if "lines" not in filters:
|
|
filters["lines"] = None
|
|
if "types" not in filters:
|
|
filters["types"] = None
|
|
if "destinations" not in filters:
|
|
filters["destinations"] = None
|
|
self.name = name
|
|
self.lines = filters["lines"]
|
|
self.types = filters["types"]
|
|
self.destinations = filters["destinations"]
|
|
Filter.storage[name] = self
|
|
|
|
def filter(self, departures):
|
|
results = []
|
|
for d in departures:
|
|
if not self.types or d.type in self.types:
|
|
if not self.lines or d.line in self.lines:
|
|
if not self.destinations or d.last_stop in self.destinations:
|
|
results.append(d)
|
|
return results
|
|
|
|
class Departure:
|
|
|
|
id_pool = {}
|
|
storage = dict()
|
|
|
|
@staticmethod
|
|
def get(stop_id: str):
|
|
resp = []
|
|
for id in Departure.storage:
|
|
d = Departure.storage[id]
|
|
if d.stop_id == stop_id:
|
|
resp.append(d)
|
|
return resp
|
|
|
|
@staticmethod
|
|
def get_id(stop_id: str):
|
|
if stop_id not in Departure.id_pool:
|
|
Departure.id_pool[stop_id] = []
|
|
i = 0
|
|
while True:
|
|
i += 1
|
|
if i not in Departure.id_pool[stop_id]:
|
|
Departure.id_pool[stop_id].append(i)
|
|
return i
|
|
|
|
@staticmethod
|
|
def clear():
|
|
remove = []
|
|
for did in Departure.storage:
|
|
d = Departure.storage[did]
|
|
if d.get_departure() < -1:
|
|
Departure.id_pool[d.stop_id].remove(d.id)
|
|
remove.append(did)
|
|
for did in remove:
|
|
Departure.storage.pop(did)
|
|
|
|
def __init__(self, did, stop_id, type, line, last_stop, departure, delay):
|
|
departure = (parser.parse(departure)).timestamp()
|
|
if -(datetime.now().timestamp() - (departure + delay*60))/60 <= -1:
|
|
return
|
|
self.did = did
|
|
self.stop_id = stop_id
|
|
self.id = Departure.get_id(stop_id)
|
|
self.line = line
|
|
self.type = type
|
|
self.last_stop = last_stop
|
|
self.departure = departure
|
|
self.delay = delay
|
|
self.updated = {}
|
|
Departure.storage[did] = self
|
|
|
|
def update(self, delay):
|
|
if delay != self.delay:
|
|
for id in self.updated:
|
|
self.updated[id] += 1
|
|
self.delay = delay
|
|
|
|
def resend(self, id):
|
|
if id in self.updated:
|
|
self.updated[id] += 1
|
|
|
|
def get_departure(self):
|
|
return -(floor(((datetime.now().timestamp() - (self.departure + self.delay*60))/60)*10)/10)
|
|
|
|
def data(self, id):
|
|
if id not in self.updated or self.updated[id] != 0:
|
|
self.updated[id] = 0
|
|
return f"{self}"
|
|
else:
|
|
return None
|
|
|
|
def __repr__(self):
|
|
last_stop = self.last_stop
|
|
if len(last_stop) >= 21:
|
|
last_stop = last_stop[:20].strip() + "..."
|
|
return f"{self.id}|{self.type}|{self.line}|{last_stop}|{(self.get_departure()*10):.0f}"
|
|
|
|
def json(self):
|
|
return {
|
|
'id': self.id,
|
|
'line': self.line,
|
|
'type': self.type,
|
|
'last_stop': self.last_stop,
|
|
'departure': self.get_departure(),
|
|
'delay': self.delay
|
|
} |