aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-x.ci/bin/show_status.sh2
-rwxr-xr-x.ci/bin/track_status.sh2
-rw-r--r--vk/tracking/__init__.py4
-rw-r--r--vk/tracking/show_status.py44
-rw-r--r--vk/tracking/status.py84
-rw-r--r--vk/tracking/track_status.py87
6 files changed, 87 insertions, 136 deletions
diff --git a/.ci/bin/show_status.sh b/.ci/bin/show_status.sh
index b3f4cfe..890cc3d 100755
--- a/.ci/bin/show_status.sh
+++ b/.ci/bin/show_status.sh
@@ -12,7 +12,7 @@ script_dir="$( cd -- "$script_dir" && pwd )"
readonly script_dir
test_users() {
- "$script_dir/../lib/test.sh" vk.tracking.show_status "$@"
+ "$script_dir/../lib/test.sh" vk.tracking.status --only-once "$@"
}
main() {
diff --git a/.ci/bin/track_status.sh b/.ci/bin/track_status.sh
index 9118144..9ff5d6a 100755
--- a/.ci/bin/track_status.sh
+++ b/.ci/bin/track_status.sh
@@ -30,7 +30,7 @@ test_users() {
rm_aux_files="$( printf -- 'rm -f -- %q %q' "$log_path" "$db_path" )"
trap "$rm_aux_files" RETURN
- "$script_dir/../lib/test.sh" vk.tracking.track_status "$@" --log "$log_path" --format csv --output "$db_path" &
+ "$script_dir/../lib/test.sh" vk.tracking.status "$@" --log "$log_path" --format csv --output "$db_path" &
local pid="$!"
sleep 3
diff --git a/vk/tracking/__init__.py b/vk/tracking/__init__.py
index 78589b5..73133d8 100644
--- a/vk/tracking/__init__.py
+++ b/vk/tracking/__init__.py
@@ -2,6 +2,4 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
-from .status import StatusTracker
-
-__all__ = 'sessions', 'status_tracker',
+__all__ = 'sessions', 'status',
diff --git a/vk/tracking/show_status.py b/vk/tracking/show_status.py
deleted file mode 100644
index cf59280..0000000
--- a/vk/tracking/show_status.py
+++ /dev/null
@@ -1,44 +0,0 @@
-# Copyright (c) 2019 Egor Tensin <Egor.Tensin@gmail.com>
-# This file is part of the "VK scripts" project.
-# For details, see https://github.com/egor-tensin/vk-scripts.
-# Distributed under the MIT License.
-
-import argparse
-import sys
-
-from vk.api import API
-from vk.tracking import StatusTracker
-from vk.tracking.db import Format as DatabaseFormat
-
-
-def _parse_args(args=None):
- if args is None:
- args = sys.argv[1:]
-
- parser = argparse.ArgumentParser(
- description='Show if people are online/offline.')
-
- parser.add_argument('uids', metavar='UID', nargs='+',
- help='user IDs or "screen names"')
- parser.add_argument('-l', '--log', metavar='PATH', dest='log_path',
- help='set log file path (standard output by default)')
-
- return parser.parse_args(args)
-
-
-def track_status(uids, log_path=None):
- api = API()
- tracker = StatusTracker(api)
-
- with DatabaseFormat.LOG.open_output_file(log_path) as log_fd:
- log_writer = DatabaseFormat.LOG.create_writer(log_fd)
- tracker.add_database_writer(log_writer)
- tracker.query_status(uids)
-
-
-def main(args=None):
- track_status(**vars(_parse_args(args)))
-
-
-if __name__ == '__main__':
- main()
diff --git a/vk/tracking/status.py b/vk/tracking/status.py
index a16a34e..0a02413 100644
--- a/vk/tracking/status.py
+++ b/vk/tracking/status.py
@@ -3,13 +3,17 @@
# For details, see https://github.com/egor-tensin/vk-scripts.
# Distributed under the MIT License.
+import argparse
from collections.abc import Callable
import contextlib
import time
import signal
+import sys
+from vk.api import API
import vk.error
from vk.user import UserField
+from vk.tracking.db import Format as DatabaseFormat
class StatusTracker:
@@ -137,3 +141,83 @@ class StatusTracker:
self._do_loop(uids)
except (KeyboardInterrupt, StatusTracker.StopLooping):
pass
+
+
+DEFAULT_TIMEOUT = StatusTracker.DEFAULT_TIMEOUT
+DEFAULT_DB_FORMAT = DatabaseFormat.CSV
+
+
+def _parse_positive_integer(s):
+ try:
+ n = int(s)
+ except ValueError:
+ raise argparse.ArgumentTypeError('must be a positive integer: ' + s)
+ if n < 1:
+ raise argparse.ArgumentTypeError('must be a positive integer: ' + s)
+ return n
+
+
+def _parse_database_format(s):
+ try:
+ return DatabaseFormat(s)
+ except ValueError:
+ raise argparse.ArgumentTypeError('invalid database format: ' + s)
+
+
+def _parse_args(args=None):
+ if args is None:
+ args = sys.argv[1:]
+
+ parser = argparse.ArgumentParser(
+ description='Track when people go online/offline.')
+
+ parser.add_argument('uids', metavar='UID', nargs='+',
+ help='user IDs or "screen names"')
+ parser.add_argument('-t', '--timeout', metavar='SECONDS',
+ type=_parse_positive_integer,
+ default=DEFAULT_TIMEOUT,
+ help='set refresh interval')
+ parser.add_argument('-O', '--only-once', action='store_true',
+ help='query the status only once and exit')
+ parser.add_argument('-l', '--log', metavar='PATH', dest='log_path',
+ help='set log file path (standard output by default)')
+ parser.add_argument('-f', '--format', dest='db_fmt',
+ type=_parse_database_format,
+ choices=DatabaseFormat,
+ default=DEFAULT_DB_FORMAT,
+ help='specify database format')
+ parser.add_argument('-o', '--output', metavar='PATH', dest='db_path',
+ help='set database file path')
+
+ return parser.parse_args(args)
+
+
+def track_status(
+ uids, timeout=DEFAULT_TIMEOUT, log_path=None,
+ db_path=None, db_fmt=DEFAULT_DB_FORMAT, only_once=False):
+
+ api = API()
+ tracker = StatusTracker(api, timeout)
+
+ if db_fmt is DatabaseFormat.LOG or db_path is None:
+ db_fmt = DatabaseFormat.NULL
+
+ with DatabaseFormat.LOG.open_output_file(log_path) as log_fd:
+ log_writer = DatabaseFormat.LOG.create_writer(log_fd)
+ tracker.add_database_writer(log_writer)
+ with db_fmt.open_output_file(db_path) as db_fd:
+ db_writer = db_fmt.create_writer(db_fd)
+ tracker.add_database_writer(db_writer)
+
+ if only_once:
+ tracker.query_status(uids)
+ else:
+ tracker.loop(uids)
+
+
+def main(args=None):
+ track_status(**vars(_parse_args(args)))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/vk/tracking/track_status.py b/vk/tracking/track_status.py
deleted file mode 100644
index 2a974a5..0000000
--- a/vk/tracking/track_status.py
+++ /dev/null
@@ -1,87 +0,0 @@
-# Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
-# This file is part of the "VK scripts" project.
-# For details, see https://github.com/egor-tensin/vk-scripts.
-# Distributed under the MIT License.
-
-import argparse
-import sys
-
-from vk.api import API
-from vk.tracking import StatusTracker
-from vk.tracking.db import Format as DatabaseFormat
-
-
-DEFAULT_TIMEOUT = StatusTracker.DEFAULT_TIMEOUT
-DEFAULT_DB_FORMAT = DatabaseFormat.CSV
-
-
-def _parse_positive_integer(s):
- try:
- n = int(s)
- except ValueError:
- raise argparse.ArgumentTypeError('must be a positive integer: ' + s)
- if n < 1:
- raise argparse.ArgumentTypeError('must be a positive integer: ' + s)
- return n
-
-
-def _parse_database_format(s):
- try:
- return DatabaseFormat(s)
- except ValueError:
- raise argparse.ArgumentTypeError('invalid database format: ' + s)
-
-
-def _parse_args(args=None):
- if args is None:
- args = sys.argv[1:]
-
- parser = argparse.ArgumentParser(
- description='Track when people go online/offline.')
-
- parser.add_argument('uids', metavar='UID', nargs='+',
- help='user IDs or "screen names"')
- parser.add_argument('-t', '--timeout', metavar='SECONDS',
- type=_parse_positive_integer,
- default=DEFAULT_TIMEOUT,
- help='set refresh interval')
- parser.add_argument('-l', '--log', metavar='PATH', dest='log_path',
- help='set log file path (standard output by default)')
- parser.add_argument('-f', '--format', dest='db_fmt',
- type=_parse_database_format,
- choices=DatabaseFormat,
- default=DEFAULT_DB_FORMAT,
- help='specify database format')
- parser.add_argument('-o', '--output', metavar='PATH', dest='db_path',
- help='set database file path')
-
- return parser.parse_args(args)
-
-
-def track_status(
- uids, timeout=DEFAULT_TIMEOUT,
- log_path=None,
- db_path=None, db_fmt=DEFAULT_DB_FORMAT):
-
- api = API()
- tracker = StatusTracker(api, timeout)
-
- if db_fmt is DatabaseFormat.LOG or db_path is None:
- db_fmt = DatabaseFormat.NULL
-
- with DatabaseFormat.LOG.open_output_file(log_path) as log_fd:
- log_writer = DatabaseFormat.LOG.create_writer(log_fd)
- tracker.add_database_writer(log_writer)
- with db_fmt.open_output_file(db_path) as db_fd:
- db_writer = db_fmt.create_writer(db_fd)
- tracker.add_database_writer(db_writer)
-
- tracker.loop(uids)
-
-
-def main(args=None):
- track_status(**vars(_parse_args(args)))
-
-
-if __name__ == '__main__':
- main()