aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/vk/utils/tracking
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-06-17 03:12:51 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-06-17 03:12:51 +0300
commitd0d1fca2ba5337095b953e0f83dba8cf7a2ec3a8 (patch)
tree84689152079e3160263ff65f10675166c118caf3 /vk/utils/tracking
parentrefactoring & support more user fields (diff)
downloadvk-scripts-d0d1fca2ba5337095b953e0f83dba8cf7a2ec3a8.tar.gz
vk-scripts-d0d1fca2ba5337095b953e0f83dba8cf7a2ec3a8.zip
add the platform a user was "last seen" using
Diffstat (limited to 'vk/utils/tracking')
-rw-r--r--vk/utils/tracking/db/record.py58
-rw-r--r--vk/utils/tracking/logger.py13
2 files changed, 42 insertions, 29 deletions
diff --git a/vk/utils/tracking/db/record.py b/vk/utils/tracking/db/record.py
index 4748a37..fd684cc 100644
--- a/vk/utils/tracking/db/record.py
+++ b/vk/utils/tracking/db/record.py
@@ -6,7 +6,7 @@ from collections import OrderedDict
from collections.abc import MutableMapping
from datetime import datetime, timezone
-from vk.user import LastSeen, User, UserField
+from vk.user import LastSeen, LastSeenField, User, UserField
class Timestamp:
@staticmethod
@@ -28,23 +28,14 @@ class Timestamp:
dt = self._new()
dt = dt.replace(microsecond=0)
dt = self._lose_timezone(dt)
- self._dt = 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'
-
- @staticmethod
- def from_last_seen(ls):
- return Timestamp(ls.get_time())
-
- def to_last_seen(self):
- ls = LastSeen()
- ls.set_time(self._dt)
- return ls
+ return self.dt.isoformat() + 'Z'
class Record(MutableMapping):
FIELDS = (
@@ -53,7 +44,8 @@ class Record(MutableMapping):
UserField.LAST_NAME,
UserField.SCREEN_NAME,
UserField.ONLINE,
- UserField.LAST_SEEN,
+ LastSeenField.TIME,
+ LastSeenField.PLATFORM,
)
def __init__(self, timestamp=None, fields=None):
@@ -65,21 +57,26 @@ class Record(MutableMapping):
self._fields = fields
def __getitem__(self, field):
- if field is UserField.LAST_SEEN:
- return Timestamp.from_last_seen(self._fields[field])
+ if field is LastSeenField.TIME:
+ return Timestamp(self._fields[field])
return self._fields[field]
def __setitem__(self, field, value):
- if field is UserField.LAST_SEEN:
+ if field is LastSeenField.TIME:
if isinstance(value, str):
- value = Timestamp.from_string(value).to_last_seen()
+ value = Timestamp.from_string(value).dt
elif isinstance(value, Timestamp):
- value = value.to_last_seen()
- elif isinstance(value, LastSeen):
+ value = value.dt
+ elif isinstance(value, datetime):
pass
else:
raise TypeError()
- self._fields[field] = User.parse(field, value)
+ 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]
@@ -95,13 +92,26 @@ class Record(MutableMapping):
@staticmethod
def from_user(user):
- instance = Record()
+ record = Record()
for field in Record.FIELDS:
- instance[field] = user[field]
- return instance
+ 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 to_user(self):
user = User()
+ last_seen = LastSeen()
for field in self:
- user[field] = self[field]
+ if isinstance(field, LastSeenField):
+ last_seen[field] = self[field]
+ elif isinstance(field, UserField):
+ user[field] = self[field]
+ else:
+ assert False
+ if len(last_seen):
+ user.set_last_seen(last_seen)
return user
diff --git a/vk/utils/tracking/logger.py b/vk/utils/tracking/logger.py
index a36e679..8da418a 100644
--- a/vk/utils/tracking/logger.py
+++ b/vk/utils/tracking/logger.py
@@ -42,20 +42,23 @@ class Logger:
@staticmethod
def _format_user_is_online(user):
- return '{} is ONLINE'.format(Logger._format_user(user))
+ return '{} is ONLINE.'.format(Logger._format_user(user))
@staticmethod
def _format_user_is_offline(user):
- return '{} is OFFLINE'.format(Logger._format_user(user))
+ return '{} is OFFLINE.'.format(Logger._format_user(user))
@staticmethod
def _format_user_last_seen(user):
- return '{} was last seen at {}'.format(Logger._format_user(user), user.get_last_seen_time_local())
+ 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))
+ return '{} went ONLINE.'.format(Logger._format_user(user))
@staticmethod
def _format_user_went_offline(user):
- return '{} went OFFLINE'.format(Logger._format_user(user))
+ return '{} went OFFLINE.'.format(Logger._format_user(user))