def __init__(self, stop, bus_name): """Initialize the GttData class.""" from pygtt import PyGTT self._pygtt = PyGTT() self._stop = stop self._bus_name = bus_name self.bus_list = {} self.state_bus = {}
class GttData: """Inteface to PyGTT.""" def __init__(self, stop, bus_name): """Initialize the GttData class.""" from pygtt import PyGTT self._pygtt = PyGTT() self._stop = stop self._bus_name = bus_name self.bus_list = {} self.state_bus = {} def get_data(self): """Get the data from the api.""" self.bus_list = self._pygtt.get_by_stop(self._stop) self.bus_list.sort(key=get_datetime) if self._bus_name is not None: self.state_bus = self.get_bus_by_name() return self.state_bus = self.bus_list[0] def get_bus_by_name(self): """Get the bus by name.""" for bus in self.bus_list: if bus["bus_name"] == self._bus_name: return bus
async def test_404(aresponses): aresponses.add(response=aresponses.Response(status=404)) async with aiohttp.ClientSession() as session: pygtt = PyGTT(session=session, stop_name="512") with pytest.raises(PyGTTConnectionError): await pygtt._request()
class GttData: """Inteface to PyGTT.""" def __init__(self, stop, bus_name): """Initialize the GttData class.""" from pygtt import PyGTT self._pygtt = PyGTT() self._stop = stop self._bus_name = bus_name self.bus_list = {} self.state_bus = {} def get_data(self): """Get the data from the api.""" self.bus_list = self._pygtt.get_by_stop(self._stop) self.bus_list.sort(key=get_datetime) if self._bus_name is not None: self.state_bus = self.get_bus_by_name() return self.state_bus = self.bus_list[0] def get_bus_by_name(self): """Get the bus by name.""" for bus in self.bus_list: if bus['bus_name'] == self._bus_name: return bus
async def test_request(aresponses): aresponses.add(response="OK") async with aiohttp.ClientSession() as session: pygtt = PyGTT(session=session, stop_name="512") r = await pygtt._request() assert r == "OK"
async def test_timeout(aresponses): async def response_handler(_): await asyncio.sleep(2) return aresponses.Response(body="Not important.") aresponses.add(response_handler) async with aiohttp.ClientSession() as session: pygtt = PyGTT(session=session, stop_name="512", request_timeout=1) with pytest.raises(PyGTTConnectionError): await pygtt._request()
async def test(): async with aiohttp.ClientSession() as session: pygtt = PyGTT( session=session, stop_name="512", ) stop = await pygtt.get_state() print(stop.name) for b in stop.bus_list: print(b.name) for t in b.time: print(t)
async def test_get_state(aresponses): aresponses.add(response=load_fixture("sample.html")) async with PyGTT(stop_name="512") as p: assert await p.get_state()
async def test_parse(): pygtt = PyGTT(stop_name="512") pygtt._parse_data(load_fixture("sample.html")) assert pygtt._stop is not None assert len(pygtt._stop.bus_list)
async def test_builtin_session(aresponses): aresponses.add("OK") async with PyGTT(stop_name="512") as pygtt: with pytest.raises(PyGTTConnectionError): await pygtt._request()
from pygtt import PyGTT p = PyGTT() print(p.get_by_stop(108))