diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-19 03:08:55 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-19 03:08:55 +0300 |
commit | 0eabb81f4a8664970ce11405fe3418cbcc1b1672 (patch) | |
tree | ba594a07945bd8d34d8147716917f86ade1db6ea /bin/track_status.py | |
parent | bugfix (diff) | |
download | vk-scripts-0eabb81f4a8664970ce11405fe3418cbcc1b1672.tar.gz vk-scripts-0eabb81f4a8664970ce11405fe3418cbcc1b1672.zip |
move files to subdirectories
Diffstat (limited to 'bin/track_status.py')
-rw-r--r-- | bin/track_status.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/bin/track_status.py b/bin/track_status.py new file mode 100644 index 0000000..f18d908 --- /dev/null +++ b/bin/track_status.py @@ -0,0 +1,58 @@ +# Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from vk.api import API, Language +from vk.tracking import StatusTracker +from vk.tracking.db import Format + +if __name__ == '__main__': + import argparse, sys + + def natural_number(s): + x = int(s) + if x < 1: + raise argparse.ArgumentError() + return x + def output_format(s): + try: + return Format(s) + except ValueError: + raise argparse.ArgumentError() + + parser = argparse.ArgumentParser( + description='Track when people go online/offline.') + + parser.add_argument(metavar='UID', dest='uids', nargs='+', + help='user IDs or "screen names"') + parser.add_argument('-t', '--timeout', type=natural_number, + default=StatusTracker.DEFAULT_TIMEOUT, + help='set refresh interval (seconds)') + parser.add_argument('-l', '--log', default=sys.stdout, + type=argparse.FileType('w'), + help='set log file path (standard output by default)') + parser.add_argument('--output-format', + type=output_format, default=Format.CSV, + choices=tuple(fmt for fmt in Format), + help='set database format') + parser.add_argument('-o', '--output', default=None, + type=argparse.FileType('w'), + help='set database file path') + + args = parser.parse_args() + + api = API(Language.EN) + tracker = StatusTracker(api, args.timeout) + + if args.output_format is Format.LOG or args.output is None: + args.output_format = Format.NULL + + with Format.LOG.create_writer(args.log) as log_writer: + tracker.add_database_writer(log_writer) + with args.output_format.create_writer(args.output) as db_writer: + tracker.add_database_writer(db_writer) + try: + tracker.loop(args.uids) + except Exception as e: + log_writer.exception(e) + sys.exit(1) |