From 57eb73b53b88669d5f0bb4af4770f74e8dbe90dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Thu, 28 Dec 2017 18:13:23 +0100 Subject: [PATCH] Fix README --- README.md | 3 ++- bitter/cli.py | 14 +++++++++----- bitter/utils.py | 29 ++++++++++++++++++----------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 1f900ec..0a9a2e2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -#Description +# Description + There are two parts to bitter. First of all, it is a wrapper over Python twitter that adds support for several Twitter API credentials (e.g. authorizing the same app with different user accounts). Secondly, it is a command line tool to automate several actions (e.g. downloading user networks) using the wrapper. diff --git a/bitter/cli.py b/bitter/cli.py index 60b9141..e4431b3 100644 --- a/bitter/cli.py +++ b/bitter/cli.py @@ -393,7 +393,7 @@ def stream(ctx): @stream.command('get') @click.option('-l', '--locations', default=None) @click.option('-t', '--track', default=None) -@click.option('-f', '--file', help='File to store the stream of tweets') +@click.option('-f', '--file', default=None, help='File to store the stream of tweets') @click.option('-p', '--politelyretry', help='Politely retry after a hangup/connection error', is_flag=True, default=True) @click.pass_context def get_stream(ctx, locations, track, file, politelyretry): @@ -416,10 +416,14 @@ def get_stream(ctx, locations, track, file, politelyretry): iterator = wq.statuses.sample() else: iterator = wq.statuses.filter(**query_args)#"-4.25,40.16,-3.40,40.75") - for i in iterator: - yield i - if not politelyretry: - return + try: + for i in iterator: + yield i + if not politelyretry: + return + except Exception: + if not politelyretry: + raise ex thishangup = time.time() if thishangup - lasthangup < 60: raise Exception('Too many hangups in a row.') diff --git a/bitter/utils.py b/bitter/utils.py index af46ac9..6830cba 100644 --- a/bitter/utils.py +++ b/bitter/utils.py @@ -155,17 +155,24 @@ def get_hashtags(iter_tweets, best=None): def read_file(filename, tail=False): - with open(filename) as f: - while True: - line = f.readline() - if line not in (None, '', '\n'): - tweet = json.loads(line.strip()) - yield tweet - else: - if tail: - time.sleep(1) - else: - return + if filename == '-': + f = sys.stdin + else: + f = open(filename) + try: + while True: + line = f.readline() + if line not in (None, '', '\n'): + tweet = json.loads(line.strip()) + yield tweet + else: + if tail: + time.sleep(1) + else: + return + finally: + if f != sys.stdin: + close(f) def get_users(wq, ulist, by_name=False, queue=None, max_users=100):