diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-17 03:24:58 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-17 03:24:58 +0300 |
commit | 2c25de11d1db0621a6d21a2f3d12245304bfdc31 (patch) | |
tree | d5ff5ab973c4c4e139467cfae818118223070356 /vk/utils/tracking/db/writer | |
parent | add the platform a user was "last seen" using (diff) | |
download | vk-scripts-2c25de11d1db0621a6d21a2f3d12245304bfdc31.tar.gz vk-scripts-2c25de11d1db0621a6d21a2f3d12245304bfdc31.zip |
make the status logger a database writer
Diffstat (limited to 'vk/utils/tracking/db/writer')
-rw-r--r-- | vk/utils/tracking/db/writer/__init__.py | 2 | ||||
-rw-r--r-- | vk/utils/tracking/db/writer/log.py | 56 |
2 files changed, 57 insertions, 1 deletions
diff --git a/vk/utils/tracking/db/writer/__init__.py b/vk/utils/tracking/db/writer/__init__.py index 330d5a8..8eb62e1 100644 --- a/vk/utils/tracking/db/writer/__init__.py +++ b/vk/utils/tracking/db/writer/__init__.py @@ -2,4 +2,4 @@ # This file is licensed under the terms of the MIT License. # See LICENSE.txt for details. -__all__ = 'csv', +__all__ = 'csv', 'log' diff --git a/vk/utils/tracking/db/writer/log.py b/vk/utils/tracking/db/writer/log.py new file mode 100644 index 0000000..02697b7 --- /dev/null +++ b/vk/utils/tracking/db/writer/log.py @@ -0,0 +1,56 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +import logging + +class Logger: + @staticmethod + def on_initial_status(user): + if user.is_online(): + logging.info(Logger._format_user_is_online(user)) + else: + logging.info(Logger._format_user_is_offline(user)) + logging.info(Logger._format_user_last_seen(user)) + + @staticmethod + def on_status_update(user): + if user.is_online(): + logging.info(Logger._format_user_went_online(user)) + else: + logging.info(Logger._format_user_went_offline(user)) + logging.info(Logger._format_user_last_seen(user)) + + @staticmethod + def on_exception(e): + logging.exception(e) + + @staticmethod + def _format_user(user): + if user.has_last_name(): + return '{} {}'.format(user.get_first_name(), user.get_last_name()) + else: + return '{}'.format(user.get_first_name()) + + @staticmethod + def _format_user_is_online(user): + return '{} is ONLINE.'.format(Logger._format_user(user)) + + @staticmethod + def _format_user_is_offline(user): + return '{} is OFFLINE.'.format(Logger._format_user(user)) + + @staticmethod + def _format_user_last_seen(user): + return '{} was last seen at {} using {}.'.format( + Logger._format_user(user), + user.get_last_seen_time_local(), + user.get_last_seen_platform().get_description_for_sentence()) + + @staticmethod + def _format_user_went_online(user): + return '{} went ONLINE.'.format(Logger._format_user(user)) + + @staticmethod + def _format_user_went_offline(user): + return '{} went OFFLINE.'.format(Logger._format_user(user)) |