From 5eccfb18af4f8dc1c0c3054005e1ac1232e9f052 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 22 Mar 2018 17:06:07 -0700 Subject: [PATCH] Allow a single Livestream instance to work on multiple subs. -r subreddit1+subreddit2 -u user1+user2 will manage multiple independent livestream generators and print the results for all. --- timesearch/common.py | 7 +++++++ timesearch/livestream.py | 45 +++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/timesearch/common.py b/timesearch/common.py index 3d6d09d..e8f5ed3 100644 --- a/timesearch/common.py +++ b/timesearch/common.py @@ -112,3 +112,10 @@ def nofailrequest(function): print('Retrying in 2...') time.sleep(2) return a + +def split_any(text, delimiters): + delimiters = list(delimiters) + (splitter, replacers) = (delimiters[0], delimiters[1:]) + for replacer in replacers: + text = text.replace(replacer, splitter) + return text.split(splitter) diff --git a/timesearch/livestream.py b/timesearch/livestream.py index c5e2ab9..1187b7c 100644 --- a/timesearch/livestream.py +++ b/timesearch/livestream.py @@ -9,9 +9,9 @@ from . import tsdb def generator_printer(generator): for step in generator: - newtext = '%ds, %dc' % (step['new_submissions'], step['new_comments']) + newtext = '%s: +%ds, %dc' % (step['tsdb'].filepath.basename, step['new_submissions'], step['new_comments']) totalnew = step['new_submissions'] + step['new_comments'] - status = '{now} +{new}'.format(now=common.human(common.get_now()), new=newtext) + status = '{now} {new}'.format(now=common.human(common.get_now()), new=newtext) print(status, end='') if totalnew == 0 and common.log.level != common.logging.DEBUG: # Since there were no news, allow the next line to overwrite status @@ -39,22 +39,39 @@ def livestream( calling `next` on each of them, instead of getting stuck in here. ''' - generator = _livestream_as_a_generator( - subreddit=subreddit, - username=username, - do_submissions=do_submissions, - do_comments=do_comments, - limit=limit, - params={'show': 'all'}, - ) - if as_a_generator: - return generator + def _listify(x): + if x is None: + return [] + if isinstance(x, str): + return common.split_any(x, ['+', ' ', ',']) + return x - generator = generator_printer(generator) + subreddits = _listify(subreddit) + usernames = _listify(username) + kwargs = { + 'do_submissions': do_submissions, + 'do_comments': do_comments, + 'limit': limit, + 'params': {'show': 'all'}, + } + subreddit_generators = [ + _livestream_as_a_generator(subreddit=subreddit, username=None, **kwargs) for subreddit in subreddits + ] + user_generators = [ + _livestream_as_a_generator(subreddit=None, username=username, **kwargs) for username in usernames + ] + generators = subreddit_generators + user_generators + if as_a_generator: + if len(generators) == 1: + return generators[0] + return generators + + generators = [generator_printer(generator) for generator in generators] while True: try: - step = next(generator) + for generator in generators: + step = next(generator) if only_once: break time.sleep(sleepy)