diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 23:56:48 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 23:56:48 +0300 |
commit | 48b19f2c9fec8a7e2f26ddd058c794d4f5a33894 (patch) | |
tree | 23f397fdad7a38ea2f7d3890593fddedd0572867 /vk/tracking | |
parent | README update (diff) | |
download | vk-scripts-48b19f2c9fec8a7e2f26ddd058c794d4f5a33894.tar.gz vk-scripts-48b19f2c9fec8a7e2f26ddd058c794d4f5a33894.zip |
vk.utils.tracking -> vk.tracking
Diffstat (limited to 'vk/tracking')
-rw-r--r-- | vk/tracking/__init__.py | 8 | ||||
-rw-r--r-- | vk/tracking/db/__init__.py | 7 | ||||
-rw-r--r-- | vk/tracking/db/backend/__init__.py | 5 | ||||
-rw-r--r-- | vk/tracking/db/backend/csv.py | 63 | ||||
-rw-r--r-- | vk/tracking/db/backend/log.py | 75 | ||||
-rw-r--r-- | vk/tracking/db/backend/null.py | 37 | ||||
-rw-r--r-- | vk/tracking/db/format.py | 35 | ||||
-rw-r--r-- | vk/tracking/db/record.py | 98 | ||||
-rw-r--r-- | vk/tracking/db/timestamp.py | 34 | ||||
-rw-r--r-- | vk/tracking/online_streaks.py | 140 | ||||
-rw-r--r-- | vk/tracking/status_tracker.py | 99 |
11 files changed, 601 insertions, 0 deletions
diff --git a/vk/tracking/__init__.py b/vk/tracking/__init__.py new file mode 100644 index 0000000..0f9c422 --- /dev/null +++ b/vk/tracking/__init__.py @@ -0,0 +1,8 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from .online_streaks import OnlineStreakEnumerator +from .status_tracker import StatusTracker + +__all__ = 'online_streaks', 'status_tracker', diff --git a/vk/tracking/db/__init__.py b/vk/tracking/db/__init__.py new file mode 100644 index 0000000..9e6b74a --- /dev/null +++ b/vk/tracking/db/__init__.py @@ -0,0 +1,7 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from .format import Format + +__all__ = 'format' diff --git a/vk/tracking/db/backend/__init__.py b/vk/tracking/db/backend/__init__.py new file mode 100644 index 0000000..4b3c278 --- /dev/null +++ b/vk/tracking/db/backend/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +__all__ = 'csv', 'log', 'null' diff --git a/vk/tracking/db/backend/csv.py b/vk/tracking/db/backend/csv.py new file mode 100644 index 0000000..10a504f --- /dev/null +++ b/vk/tracking/db/backend/csv.py @@ -0,0 +1,63 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections.abc import Iterable +import csv + +from ..record import Record +from ..timestamp import Timestamp + +class Writer: + def __init__(self, fd): + self._fd = fd + self._writer = csv.writer(fd, lineterminator='\n') + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def on_initial_status(self, user): + self._write_record(user) + self._fd.flush() + + def on_status_update(self, user): + self._write_record(user) + self._fd.flush() + + def on_connection_error(self, e): + pass + + def _write_record(self, user): + if not self: + return + self._write_row(self._record_to_row(Record.from_user(user))) + + def _write_row(self, row): + self._writer.writerow(row) + + @staticmethod + def _record_to_row(record): + return [str(record.get_timestamp())] + [str(record[field]) for field in record] + +class Reader(Iterable): + def __init__(self, fd): + self._reader = csv.reader(fd) + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def __iter__(self): + return map(Reader._record_from_row, self._reader) + + @staticmethod + def _record_from_row(row): + record = Record(Timestamp.from_string(row[0])) + for i in range(len(Record.FIELDS)): + record[Record.FIELDS[i]] = row[i + 1] + return record diff --git a/vk/tracking/db/backend/log.py b/vk/tracking/db/backend/log.py new file mode 100644 index 0000000..625257b --- /dev/null +++ b/vk/tracking/db/backend/log.py @@ -0,0 +1,75 @@ +# 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 Writer: + def __init__(self, fd): + self._logger = logging.getLogger(__file__) + self._logger.setLevel(logging.INFO) + handler = logging.StreamHandler(fd) + handler.setFormatter(logging.Formatter( + fmt='[%(asctime)s] %(message)s', + datefmt='%Y-%m-%d %H:%M:%S')) + self._logger.addHandler(handler) + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def info(self, msg): + self._logger.info(msg) + + def exception(self, e): + self._logger.exception(e) + + def on_initial_status(self, user): + if user.is_online(): + self.info(self._format_user_is_online(user)) + else: + self.info(self._format_user_is_offline(user)) + self.info(self._format_user_last_seen(user)) + + def on_status_update(self, user): + if user.is_online(): + self.info(self._format_user_went_online(user)) + else: + self.info(self._format_user_went_offline(user)) + self.info(self._format_user_last_seen(user)) + + def on_connection_error(self, e): + #self.exception(e) + pass + + @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(Writer._format_user(user)) + + @staticmethod + def _format_user_is_offline(user): + return '{} is OFFLINE.'.format(Writer._format_user(user)) + + @staticmethod + def _format_user_last_seen(user): + return '{} was last seen at {} using {}.'.format( + Writer._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(Writer._format_user(user)) + + @staticmethod + def _format_user_went_offline(user): + return '{} went OFFLINE.'.format(Writer._format_user(user)) diff --git a/vk/tracking/db/backend/null.py b/vk/tracking/db/backend/null.py new file mode 100644 index 0000000..139a9f0 --- /dev/null +++ b/vk/tracking/db/backend/null.py @@ -0,0 +1,37 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections.abc import Iterable + +class Writer: + def __init__(self, fd): + pass + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def on_initial_status(self, user): + pass + + def on_status_update(self, user): + pass + + def on_connection_error(self, e): + pass + +class Reader(Iterable): + def __init__(self, fd): + pass + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def __iter__(self): + pass diff --git a/vk/tracking/db/format.py b/vk/tracking/db/format.py new file mode 100644 index 0000000..4856094 --- /dev/null +++ b/vk/tracking/db/format.py @@ -0,0 +1,35 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from enum import Enum + +from .backend import * + +class Format(Enum): + CSV = 'csv' + LOG = 'log' + NULL = 'null' + + def create_writer(self, fd): + if self is Format.CSV: + return csv.Writer(fd) + elif self is Format.LOG: + return log.Writer(fd) + elif self is Format.NULL: + return null.Writer(fd) + else: + raise NotImplementedError('unsupported database format: ' + str(self)) + + def create_reader(self, fd): + if self is Format.CSV: + return csv.Reader(fd) + elif self is Format.LOG: + raise NotImplementedError() + elif self is Format.NULL: + return null.Reader(fd) + else: + raise NotImplementedError('unsupported database format: ' + str(self)) + + def __str__(self): + return self.value diff --git a/vk/tracking/db/record.py b/vk/tracking/db/record.py new file mode 100644 index 0000000..93be97c --- /dev/null +++ b/vk/tracking/db/record.py @@ -0,0 +1,98 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections import OrderedDict +from collections.abc import MutableMapping +from datetime import datetime + +from .timestamp import Timestamp +from vk.user import LastSeen, LastSeenField, User, UserField + +class Record(MutableMapping): + FIELDS = ( + UserField.UID, + UserField.FIRST_NAME, + UserField.LAST_NAME, + UserField.SCREEN_NAME, + UserField.ONLINE, + LastSeenField.TIME, + LastSeenField.PLATFORM, + ) + + def __init__(self, timestamp=None, fields=None): + if timestamp is None: + timestamp = Timestamp() + if fields is None: + fields = OrderedDict() + self._timestamp = timestamp + self._fields = fields + + def __getitem__(self, field): + if field is LastSeenField.TIME: + return Timestamp(self._fields[field]) + return self._fields[field] + + def __setitem__(self, field, value): + if field is LastSeenField.TIME: + if isinstance(value, str): + value = Timestamp.from_string(value).dt + elif isinstance(value, Timestamp): + value = value.dt + elif isinstance(value, datetime): + pass + else: + raise TypeError() + if isinstance(field, LastSeenField): + self._fields[field] = LastSeen.parse(field, value) + elif isinstance(field, UserField): + self._fields[field] = User.parse(field, value) + else: + raise TypeError() + + def __delitem__(self, field): + del self._fields[field] + + def __iter__(self): + return iter(self._fields) + + def __len__(self): + return len(self._fields) + + def get_timestamp(self): + return self._timestamp + + @staticmethod + def from_user(user): + record = Record() + for field in Record.FIELDS: + if isinstance(field, UserField): + record[field] = user[field] + elif isinstance(field, LastSeenField): + record[field] = user.get_last_seen()[field] + else: + assert False + return record + + def _update_last_seen_field(self, last_seen, field): + if field is LastSeenField.TIME: + last_seen[field] = self[field].dt + else: + last_seen[field] = self[field] + + def _update_user_field(self, user, field): + user[field] = self[field] + + def to_user(self): + user = User() + last_seen = LastSeen() + for field in self: + if isinstance(field, LastSeenField): + self._update_last_seen_field(last_seen, field) + elif isinstance(field, UserField): + self._update_user_field(user, field) + else: + assert False + if len(last_seen): + user.set_last_seen(last_seen) + return user diff --git a/vk/tracking/db/timestamp.py b/vk/tracking/db/timestamp.py new file mode 100644 index 0000000..35cf5b3 --- /dev/null +++ b/vk/tracking/db/timestamp.py @@ -0,0 +1,34 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from datetime import datetime, timezone + +class Timestamp: + @staticmethod + def _new(): + return datetime.utcnow() + + @staticmethod + def _is_timezone_aware(dt): + return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None + + @staticmethod + def _lose_timezone(dt): + if Timestamp._is_timezone_aware(dt): + return dt.astimezone(timezone.utc).replace(tzinfo=None) + return dt + + def __init__(self, dt=None): + if dt is None: + dt = self._new() + dt = dt.replace(microsecond=0) + dt = self._lose_timezone(dt) + self.dt = dt + + @staticmethod + def from_string(s): + return Timestamp(datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')) + + def __str__(self): + return self.dt.isoformat() + 'Z' diff --git a/vk/tracking/online_streaks.py b/vk/tracking/online_streaks.py new file mode 100644 index 0000000..5c9aa48 --- /dev/null +++ b/vk/tracking/online_streaks.py @@ -0,0 +1,140 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections import OrderedDict +from collections.abc import MutableMapping +from datetime import timedelta +from enum import Enum + +from vk.user import User + +class Weekday(Enum): + MONDAY = 0 + TUESDAY = 1 + WEDNESDAY = 2 + THURSDAY = 3 + FRIDAY = 4 + SATURDAY = 5 + SUNDAY = 6 + + def __str__(self): + return self.name[0] + self.name[1:].lower() + +class OnlineStreakEnumerator(MutableMapping): + def __init__(self, date_from=None, date_to=None): + self._records = {} + self._date_from = date_from + self._date_to = date_to + + def __getitem__(self, user): + return self._records[user] + + def __setitem__(self, user, record): + self._records[user] = record + + def __delitem__(self, user): + del self._records[user] + + def __iter__(self): + return iter(self._records) + + def __len__(self): + return len(self._records) + + def _cut_period(self, streak): + user, time_from, time_to = streak + #print(user.get_first_name(), time_from, self._date_from) + if self._date_from is not None: + if time_to < self._date_from: + #print(1) + return None + if time_from < self._date_from: + #print(2) + time_from = self._date_from + if self._date_to is not None: + if time_from > self._date_to: + #print(3) + return None + if time_to > self._date_to: + #print(4) + time_to = self._date_to + return user, time_from, time_to + + def enum(self, db_reader): + for record in db_reader: + streak = self._insert_record(record) + if streak is not None: + streak = self._cut_period(streak) + if streak is not None: + yield streak + + def group_by_user(self, db_reader): + by_user = {} + for user, time_from, time_to in self.enum(db_reader): + if user not in by_user: + by_user[user] = timedelta() + by_user[user] += time_to - time_from + return by_user + + def group_by_date(self, db_reader): + by_date = OrderedDict() + for _, time_from, time_to in self.enum(db_reader): + for date, duration in self._enum_dates_and_durations(time_from, time_to): + if date not in by_date: + by_date[date] = timedelta() + by_date[date] += duration + return by_date + + def group_by_weekday(self, db_reader): + by_weekday = OrderedDict() + for weekday in Weekday: + by_weekday[weekday] = timedelta() + for _, time_from, time_to in self.enum(db_reader): + for date, duration in self._enum_dates_and_durations(time_from, time_to): + by_weekday[Weekday(date.weekday())] += duration + return by_weekday + + def group_by_hour(self, db_reader): + by_hour = OrderedDict() + for i in range(24): + by_hour[i] = timedelta() + for _, time_from, time_to in self.enum(db_reader): + for hour, duration in self._enum_hours_and_durations(time_from, time_to): + by_hour[hour] += duration + return by_hour + + @staticmethod + def _enum_dates_and_durations(time_from, time_to): + while time_from.date() != time_to.date(): + next_day = time_from.date() + timedelta(days=1) + yield time_from.date(), next_day - time_from + time_from = next_day + yield time_to.date(), time_to - time_from + + @staticmethod + def _enum_hours_and_durations(time_from, time_to): + while time_from.date() != time_to.date() or time_from.hour != time_to.hour: + next_hour = time_from.replace(minute=0, second=0) + timedelta(hours=1) + yield time_from.hour, next_hour - time_from + time_from = next_hour + yield time_to.hour, time_to - time_from + + def _insert_record(self, record): + return self._insert_user(record.to_user()) + + def _known_user(self, user): + return user.get_uid() in self._records + + def _unknown_user(self, user): + return not self._known_user(user) + + def _insert_user(self, user): + if user not in self or self[user].is_offline(): + self[user] = user + return None + if user.is_online(): + return None + streak = user, self[user].get_last_seen_time(), user.get_last_seen_time() + self[user] = user + return streak diff --git a/vk/tracking/status_tracker.py b/vk/tracking/status_tracker.py new file mode 100644 index 0000000..3d1f032 --- /dev/null +++ b/vk/tracking/status_tracker.py @@ -0,0 +1,99 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections import Callable +import time + +import vk.error +from vk.user import UserField + +class StatusTracker: + DEFAULT_TIMEOUT = 5 + + def __init__(self, api, timeout=DEFAULT_TIMEOUT): + self._api = api + self._timeout = timeout + self._on_initial_status = [] + self._on_status_update = [] + self._on_connection_error = [] + + def _wait_after_connection_error(self): + time.sleep(self._timeout) + + def add_database_writer(self, writer): + self.add_initial_status_handler(lambda user: writer.on_initial_status(user)) + self.add_status_update_handler(lambda user: writer.on_status_update(user)) + self.add_connection_error_handler(lambda e: writer.on_connection_error(e)) + + def add_initial_status_handler(self, fn): + self._assert_is_callback(fn) + self._on_initial_status.append(fn) + + def add_status_update_handler(self, fn): + self._assert_is_callback(fn) + self._on_status_update.append(fn) + + def add_connection_error_handler(self, fn): + self._assert_is_callback(fn) + self._on_connection_error.append(fn) + + @staticmethod + def _assert_is_callback(fn): + if not isinstance(fn, Callable): + raise TypeError() + + _USER_FIELDS = UserField.SCREEN_NAME, UserField.ONLINE, UserField.LAST_SEEN + + def _query_status(self, uids): + return {user.get_uid(): user for user in self._api.users_get(uids, self._USER_FIELDS)} + + def _notify_status(self, user): + for fn in self._on_initial_status: + fn(user) + + def _notify_status_update(self, user): + for fn in self._on_status_update: + fn(user) + + def _notify_connection_error(self, e): + for fn in self._on_connection_error: + fn(e) + + def _query_initial_status(self, uids): + while True: + try: + return self._query_status(uids) + except vk.error.ConnectionError as e: + self._notify_connection_error(e) + self._wait_after_connection_error() + + def _query_status_updates(self, uids): + while True: + self._wait_after_connection_error() + try: + return self._query_status(uids) + except vk.error.ConnectionError as e: + self._notify_connection_error(e) + + @staticmethod + def _filter_status_updates(old_users, new_users): + for uid, user in new_users.items(): + if old_users[uid].is_online() != user.is_online(): + old_users[uid] = user + yield user + + def _do_loop(self, uids): + users = self._query_initial_status(uids) + for user in users.values(): + self._notify_status(user) + while True: + updated_users = self._query_status_updates(uids) + for user in self._filter_status_updates(users, updated_users): + self._notify_status_update(user) + + def loop(self, uids): + try: + self._do_loop(uids) + except KeyboardInterrupt: + pass |