diff options
Diffstat (limited to 'bin')
-rw-r--r-- | bin/mutual_friends.py | 17 | ||||
-rw-r--r-- | bin/online_sessions.py | 45 | ||||
-rw-r--r-- | bin/show_status.py | 7 | ||||
-rw-r--r-- | bin/track_status.py | 10 | ||||
-rw-r--r-- | bin/utils/bar_chart.py | 2 | ||||
-rw-r--r-- | bin/utils/io.py | 7 |
6 files changed, 68 insertions, 20 deletions
diff --git a/bin/mutual_friends.py b/bin/mutual_friends.py index 82015bb..550d957 100644 --- a/bin/mutual_friends.py +++ b/bin/mutual_friends.py @@ -14,22 +14,27 @@ from vk.user import UserField from .utils import io + _OUTPUT_USER_FIELDS = UserField.UID, UserField.FIRST_NAME, UserField.LAST_NAME + def _query_friend_list(api, user): return api.friends_get(user.get_uid(), fields=_OUTPUT_USER_FIELDS) + def _filter_user_fields(user): new_user = OrderedDict() for field in _OUTPUT_USER_FIELDS: new_user[str(field)] = user[field] if field in user else None return new_user + class OutputSinkMutualFriends(metaclass=abc.ABCMeta): @abc.abstractmethod def write_mutual_friends(self, friend_list): pass + class OutputSinkCSV(OutputSinkMutualFriends): def __init__(self, fd=sys.stdout): self._writer = io.FileWriterCSV(fd) @@ -38,6 +43,7 @@ class OutputSinkCSV(OutputSinkMutualFriends): for user in friend_list: self._writer.write_row(user.values()) + class OutputSinkJSON(OutputSinkMutualFriends): def __init__(self, fd=sys.stdout): self._writer = io.FileWriterJSON(fd) @@ -45,6 +51,7 @@ class OutputSinkJSON(OutputSinkMutualFriends): def write_mutual_friends(self, friend_list): self._writer.write(friend_list) + class OutputFormat(Enum): CSV = 'csv' JSON = 'json' @@ -59,10 +66,10 @@ class OutputFormat(Enum): def create_sink(self, fd=sys.stdout): if self is OutputFormat.CSV: return OutputSinkCSV(fd) - elif self is OutputFormat.JSON: + if self is OutputFormat.JSON: return OutputSinkJSON(fd) - else: - raise NotImplementedError('unsupported output format: ' + str(self)) + raise NotImplementedError('unsupported output format: ' + str(self)) + def _parse_output_format(s): try: @@ -70,6 +77,7 @@ def _parse_output_format(s): except ValueError: raise argparse.ArgumentTypeError('invalid output format: ' + s) + def _parse_args(args=None): if args is None: args = sys.argv[1:] @@ -89,6 +97,7 @@ def _parse_args(args=None): return parser.parse_args(args) + def write_mutual_friends(uids, out_path=None, out_fmt=OutputFormat.CSV): api = API() users = api.users_get(uids) @@ -101,8 +110,10 @@ def write_mutual_friends(uids, out_path=None, out_fmt=OutputFormat.CSV): sink = out_fmt.create_sink(out_fd) sink.write_mutual_friends(mutual_friends) + def main(args=None): write_mutual_friends(**vars(_parse_args(args))) + if __name__ == '__main__': main() diff --git a/bin/online_sessions.py b/bin/online_sessions.py index 670a8c3..e0de7c9 100644 --- a/bin/online_sessions.py +++ b/bin/online_sessions.py @@ -17,6 +17,7 @@ from vk.user import UserField from .utils.bar_chart import BarChartBuilder from .utils import io + class GroupBy(Enum): USER = 'user' DATE = 'date' @@ -30,14 +31,14 @@ class GroupBy(Enum): online_streaks = OnlineSessionEnumerator(time_from, time_to) if self is GroupBy.USER: return online_streaks.group_by_user(db_reader) - elif self is GroupBy.DATE: + if self is GroupBy.DATE: return online_streaks.group_by_date(db_reader) - elif self is GroupBy.WEEKDAY: + if self is GroupBy.WEEKDAY: return online_streaks.group_by_weekday(db_reader) - elif self is GroupBy.HOUR: + if self is GroupBy.HOUR: return online_streaks.group_by_hour(db_reader) - else: - raise NotImplementedError('unsupported grouping: ' + str(self)) + raise NotImplementedError('unsupported grouping: ' + str(self)) + _OUTPUT_USER_FIELDS = ( UserField.UID, @@ -46,11 +47,13 @@ _OUTPUT_USER_FIELDS = ( UserField.DOMAIN, ) + class OutputSinkOnlineSessions(metaclass=abc.ABCMeta): @abc.abstractmethod def process_database(self, group_by, db_reader, time_from=None, time_to=None): pass + class OutputConverterCSV: @staticmethod def convert_user(user): @@ -68,6 +71,7 @@ class OutputConverterCSV: def convert_hour(hour): return [str(timedelta(hours=hour))] + class OutputSinkCSV(OutputSinkOnlineSessions): def __init__(self, fd=sys.stdout): self._writer = io.FileWriterCSV(fd) @@ -91,6 +95,7 @@ class OutputSinkCSV(OutputSinkOnlineSessions): row.append(str(duration)) self._writer.write_row(row) + class OutputConverterJSON: _DATE_FIELD = 'date' _WEEKDAY_FIELD = 'weekday' @@ -125,6 +130,7 @@ class OutputConverterJSON: obj[OutputConverterJSON._HOUR_FIELD] = str(timedelta(hours=hour)) return obj + class OutputSinkJSON(OutputSinkOnlineSessions): def __init__(self, fd=sys.stdout): self._writer = io.FileWriterJSON(fd) @@ -142,7 +148,7 @@ class OutputSinkJSON(OutputSinkOnlineSessions): @staticmethod def _key_to_object(group_by, key): - if not group_by in OutputSinkJSON._CONVERT_KEY: + if group_by not in OutputSinkJSON._CONVERT_KEY: raise NotImplementedError('unsupported grouping: ' + str(group_by)) return OutputSinkJSON._CONVERT_KEY[group_by](key) @@ -154,6 +160,7 @@ class OutputSinkJSON(OutputSinkOnlineSessions): entries.append(entry) self._writer.write(entries) + class OutputConverterPlot: @staticmethod def convert_user(user): @@ -171,6 +178,7 @@ class OutputConverterPlot: def convert_hour(hour): return '{}:00'.format(hour) + class OutputSinkPlot(OutputSinkOnlineSessions): def __init__(self, fd=sys.stdout): self._fd = fd @@ -200,11 +208,11 @@ class OutputSinkPlot(OutputSinkOnlineSessions): @staticmethod def _extract_labels(group_by, durations): - return tuple(map(lambda key: OutputSinkPlot._format_key(group_by, key), durations.keys())) + return (OutputSinkPlot._format_key(group_by, key) for key in durations.keys()) @staticmethod def _extract_values(durations): - return tuple(map(OutputSinkPlot._duration_to_seconds, durations.values())) + return (OutputSinkPlot._duration_to_seconds(duration) for duration in durations.values()) def process_database( self, group_by, db_reader, time_from=None, time_to=None): @@ -219,8 +227,8 @@ class OutputSinkPlot(OutputSinkOnlineSessions): fontsize='small', rotation=30) bar_chart.set_value_label_formatter(self._format_duration) - labels = self._extract_labels(group_by, durations) - durations = self._extract_values(durations) + labels = tuple(self._extract_labels(group_by, durations)) + durations = tuple(self._extract_values(durations)) if group_by is GroupBy.HOUR: bar_chart.labels_align_middle = False @@ -237,6 +245,7 @@ class OutputSinkPlot(OutputSinkOnlineSessions): else: bar_chart.save(self._fd) + class OutputFormat(Enum): CSV = 'csv' JSON = 'json' @@ -248,38 +257,42 @@ class OutputFormat(Enum): def create_sink(self, fd=sys.stdout): if self is OutputFormat.CSV: return OutputSinkCSV(fd) - elif self is OutputFormat.JSON: + if self is OutputFormat.JSON: return OutputSinkJSON(fd) - elif self is OutputFormat.PLOT: + if self is OutputFormat.PLOT: return OutputSinkPlot(fd) - else: - raise NotImplementedError('unsupported output format: ' + str(self)) + raise NotImplementedError('unsupported output format: ' + str(self)) def open_file(self, path=None): if self is OutputFormat.PLOT: return io.open_output_binary_file(path) return io.open_output_text_file(path) + def _parse_group_by(s): try: return GroupBy(s) except ValueError: raise argparse.ArgumentTypeError('invalid "group by" value: ' + s) + def _parse_database_format(s): try: return DatabaseFormat(s) except ValueError: raise argparse.ArgumentTypeError('invalid database format: ' + s) + def _parse_output_format(s): try: return OutputFormat(s) except ValueError: raise argparse.ArgumentTypeError('invalid output format: ' + s) + _DATE_RANGE_LIMIT_FORMAT = '%Y-%m-%dT%H:%M:%SZ' + def _parse_date_range_limit(s): try: dt = datetime.strptime(s, _DATE_RANGE_LIMIT_FORMAT) @@ -289,6 +302,7 @@ def _parse_date_range_limit(s): raise argparse.ArgumentTypeError( msg.format(_DATE_RANGE_LIMIT_FORMAT, s)) + def _parse_args(args=None): if args is None: args = sys.argv[1:] @@ -324,6 +338,7 @@ def _parse_args(args=None): return parser.parse_args(args) + def process_online_sessions( db_path=None, db_fmt=DatabaseFormat.CSV, out_path=None, out_fmt=OutputFormat.CSV, @@ -343,8 +358,10 @@ def process_online_sessions( time_from=time_from, time_to=time_to) + def main(args=None): process_online_sessions(**vars(_parse_args(args))) + if __name__ == '__main__': main() diff --git a/bin/show_status.py b/bin/show_status.py index 1ca9cb5..cf59280 100644 --- a/bin/show_status.py +++ b/bin/show_status.py @@ -3,12 +3,14 @@ # For details, see https://github.com/egor-tensin/vk-scripts. # Distributed under the MIT License. -import argparse, sys +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:] @@ -23,6 +25,7 @@ def _parse_args(args=None): return parser.parse_args(args) + def track_status(uids, log_path=None): api = API() tracker = StatusTracker(api) @@ -32,8 +35,10 @@ def track_status(uids, log_path=None): 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/bin/track_status.py b/bin/track_status.py index 9c32ae9..2a974a5 100644 --- a/bin/track_status.py +++ b/bin/track_status.py @@ -3,15 +3,18 @@ # For details, see https://github.com/egor-tensin/vk-scripts. # Distributed under the MIT License. -import argparse, sys +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) @@ -21,12 +24,14 @@ def _parse_positive_integer(s): 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:] @@ -52,6 +57,7 @@ def _parse_args(args=None): return parser.parse_args(args) + def track_status( uids, timeout=DEFAULT_TIMEOUT, log_path=None, @@ -72,8 +78,10 @@ def track_status( tracker.loop(uids) + def main(args=None): track_status(**vars(_parse_args(args))) + if __name__ == '__main__': main() diff --git a/bin/utils/bar_chart.py b/bin/utils/bar_chart.py index 522dfed..f051efc 100644 --- a/bin/utils/bar_chart.py +++ b/bin/utils/bar_chart.py @@ -7,6 +7,7 @@ import matplotlib.pyplot as plt from matplotlib import ticker import numpy as np + class BarChartBuilder: _BAR_HEIGHT = .5 @@ -130,6 +131,7 @@ class BarChartBuilder: def save(self, path): self._fig.savefig(path, bbox_inches='tight') + if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() diff --git a/bin/utils/io.py b/bin/utils/io.py index 04baa6a..bb8eef9 100644 --- a/bin/utils/io.py +++ b/bin/utils/io.py @@ -8,6 +8,7 @@ import csv import json import sys + class FileWriterJSON: def __init__(self, fd=sys.stdout): self._fd = fd @@ -16,13 +17,14 @@ class FileWriterJSON: self._fd.write(json.dumps(something, indent=3, ensure_ascii=False)) self._fd.write('\n') + class FileWriterCSV: def __init__(self, fd=sys.stdout): self._writer = csv.writer(fd, lineterminator='\n') @staticmethod def _convert_row_old_python(row): - if isinstance(row, list) or isinstance(row, tuple): + if isinstance(row, (list, tuple)): return row return list(row) @@ -31,6 +33,7 @@ class FileWriterCSV: row = self._convert_row_old_python(row) self._writer.writerow(row) + @contextmanager def _open_file(path=None, default=None, **kwargs): if path is None: @@ -39,8 +42,10 @@ def _open_file(path=None, default=None, **kwargs): with open(path, **kwargs) as fd: yield fd + def open_output_text_file(path=None): return _open_file(path, default=sys.stdout, mode='w', encoding='utf-8') + def open_output_binary_file(path=None): return _open_file(path, default=sys.stdout, mode='wb') |