Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging #1 #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/hoas.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import templates


logger = logging.Logger(__name__)
class AuthException(Exception):
"""Authentication failed."""

Expand All @@ -28,22 +29,29 @@ def __init__(self, login_params: Dict[str, str]) -> None:

def _login(self) -> None:
"""Create session for handling stuff"""

logger.info(
"HoasInterface: Logged in to {}".format(self.login_params["login"])
)
page = self.session.post(f"{self.BASE_URL}/auth/login", data=self.login_params)

# Hoas site redirects user back to login site if auth fails
if page.url == f"{self.BASE_URL}/auth/login":
logger.critical(
"HoasInterface: Logging in to {} failed".format(
self.login_params["user"]
)
)
raise AuthException("Login failed")

def get_page(self, *args: Any, **kwargs: Any) -> requests.Response:
r = self.session.get(*args, **kwargs)

logger.debug("HoasInterface: get_page {}".format(args))
if r.url == f"{self.BASE_URL}/auth/login":
self._login()
r = self.session.get(*args, **kwargs)

assert r.url != f"{self.BASE_URL}/auth/login"

logger.info("HoasInterface: got page {}".format(r.url))
r.encoding = "utf-8"
return r

Expand All @@ -59,7 +67,7 @@ def view_page(
cache_key = frozenset((service, date_path))
new_request_time = time.time() - cache_time
if cache_key in self.cache and self.cache[cache_key][0] >= new_request_time:
logging.debug(
logger.debug(
"return from cache",
self.cache[cache_key][0],
cache_time,
Expand Down Expand Up @@ -94,7 +102,7 @@ def __init__(self, accounts: List[Dict[str, str]]) -> None:
HoasInterface(account) for account in accounts
]
except Exception:
logging.error("Couldn't parse configs")
logger.error("Couldn't parse configs")
raise

def create_config(self) -> dict:
Expand All @@ -104,16 +112,13 @@ def create_config(self) -> dict:
# for stuff in
page = bs(hoas.view_page(0), "html.parser")
menus = hoasparser.parse_menu(page)
print(menus)
for i, (service_type, view_id) in enumerate(menus):
config[service_type] = {}
page = bs(hoas.view_page(view_id), "html.parser")

view_ids = hoasparser.parse_view_ids(page)
# The first viewed sites id is found in menus, but not on page
view_ids[0] = view_ids[0][0], menus[i][1]
print(view_ids, menus)
print(service_type)
services_dict: Dict[str, Dict[str, Any]] = {}
for name, view_id in view_ids:
services_dict.setdefault(name, {"reserve": {}, "view": view_id})
Expand All @@ -128,7 +133,6 @@ def create_config(self) -> dict:
hoasparser.get_reservation_ids(soup).items(),
)
)
print(services_dict)
if len(services_dict[name]["reserve"]) and all(
services_dict[name]["reserve"].values()
):
Expand Down
6 changes: 0 additions & 6 deletions src/hoasparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def get_users_reservations(soup) -> Tuple[list, list, list]:
if link:
# This is user made reservation
reservation = parse_users_reservations(r)
# print(type_)
# print(json.dumps(reservation, indent=4))
if "washer" in type_ or "dryer" in type_:
laundry.append(reservation)
if "sauna" in type_:
Expand Down Expand Up @@ -109,7 +107,6 @@ def get_reservation_ids(soup):

def parse_view_ids(soup):
fin = []
print(type(soup))
service_nav = soup.find(class_="service-nav")
fin.append((service_nav.span.text, None))
# TODO: do not guess
Expand All @@ -121,7 +118,6 @@ def parse_view_ids(soup):

def parse_menu(soup):
fin = []
print(type(soup))
menu = soup.find(class_="menu")
for a in menu.find_all("a"):
view_id = a["href"].rsplit("/", 1)[-1]
Expand All @@ -144,7 +140,6 @@ def parse_calendar(soup):
reservations_left = ""

for row in cal:
# print(row)
data = {}
time, *data_cols = row.find_all("td")
# If item inside tag is a link, it's free
Expand All @@ -162,5 +157,4 @@ def parse_calendar(soup):
status = 2
this_row.append((status, info))
final_cal.append(this_row)
# print(final_cal)
return topics, final_cal, reservations_left
20 changes: 10 additions & 10 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
main.py (-h | --help)
main.py --create-config
main.py (-d | --debug ) [<debuglevel>]
main.py --global-debug [<debuglevel>]
main.py

Options:
-h, --help Show this help message and exit
--create-config Find view and reservation ids from hoas site. Makes
multiple requests to site
-h, --help Show this help message and exit
--create-config Find view and reservation ids from hoas site. Makes
multiple requests to site
-d, --debug Set debug level. [default: DEBUG]
"""

import logging
Expand Down Expand Up @@ -54,18 +56,16 @@ async def reservation_changes(stream):
yield format_diff(*sauna_diff(previous, saunas))
previous = saunas

async def send_to_senso( message):

async def send_to_senso(message):
await bot.send_message(219882470, message)


if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s %(levelname)s:%(message)s",
datefmt="%d/%m/%Y %H:%M:%S",
level=logging.INFO,
)
args = docopt(__doc__, version="Sauna-bot 0.0.1")

args = docopt(__doc__, version="Sauna-bot 0.0.1")
config = utils.get_hoas_credentials()
utils.configure_logging(names=("stream_utils", "notifier", "hoas"), level=logging.DEBUG, base_level=logging.INFO)

if not config or config.get("token") is None or config.get("accounts") is None:
raise SystemExit(
Expand Down
4 changes: 4 additions & 0 deletions src/notifier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import logging
import collections
from contextlib import contextmanager
from datetime import datetime
Expand All @@ -7,6 +8,7 @@

DEFAULT = object()

logger = logging.Logger(__name__)

class Notifier:
def __init__(self, stream, loop=None):
Expand All @@ -19,7 +21,9 @@ def __init__(self, stream, loop=None):
self.loop.create_task(self.sd.run())

async def _new_coro(self, coroutine_callback, limit=0):
logger.debug("Created new instance")
async for update in self.sd.wait_for_updates(limit):
logger.debug("Feeding")
await coroutine_callback(update)

def subscribe(self, sub_id, coroutine_callback, limit=0):
Expand Down
17 changes: 14 additions & 3 deletions src/stream_utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import asyncio
import collections
import logging

from contextlib import contextmanager
from datetime import datetime

logger = logging.getLogger(__name__)


async def poller(func, args=[], sleep=10 * 60, limit=0, loop=None):
""" Call :func: every :sleep: seconds and yield result"""
if hasattr(func, "__name__"):
name = func.__name__
else:
name = str(func)
if not loop:
loop = asyncio.get_event_loop()
yielded = 0
calls = 0

logger.info(f"Starting polling {name} with args {args}")
while True:
print("asdf")
logger.info(f"Polling {name}")
new_result = await loop.run_in_executor(None, func, *args)

logger.debug(f"Got result {new_result} from {name}")
yield new_result
yielded += 1

logger.debug(f"Sleeping {sleep} ({name})")
await asyncio.sleep(sleep)
if limit > 0 and yielded > limit:
logger.debug(f"Finished polling {name}")
break


Expand Down
1 change: 0 additions & 1 deletion src/userconfigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def check_conf(self, conf):

def send_configs(self, chat_id):
configs = DBHelper()[chat_id].data
print(configs)
uconfigs = ""
for key in configs:
value = configs[key]
Expand Down
23 changes: 23 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import yaml
import getpass
import logging
import sys

from functools import partial
from inspect import cleandoc
Expand Down Expand Up @@ -120,9 +122,30 @@ def get_hoas_credentials():
)
return config


def load_data_to_notifier(configs, notifier, bot):
"""Add callback messages to notifier"""
for user_id, lang, onreserve, notify in configs:
if onreserve:
coro_func = partial(bot.send_message, user_id)
notifier.subscribe(user_id, coro_func)


def configure_logging(names=(), level=logging.INFO, base_level=logging.INFO):
formatter = logging.Formatter(
"%(asctime)s "
'%(module)s.'
'%(funcName)s: '
'%(levelname)s: '
'%(message)s '
)

stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setFormatter(formatter)

logging.basicConfig(level=base_level, handlers=(stderr_handler,))

for name in names:
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(stderr_handler)