From 024694edb909eeb047115ab62a52df6786f931c0 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Tue, 15 Dec 2020 02:56:27 +0100 Subject: fix cmdline tool --- fuzzycat/__main__.py | 14 +++++++++++--- fuzzycat/utils.py | 18 ++++++++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/fuzzycat/__main__.py b/fuzzycat/__main__.py index 8ff20f5..23009d3 100644 --- a/fuzzycat/__main__.py +++ b/fuzzycat/__main__.py @@ -72,9 +72,17 @@ def run_verify_single(args): if args.a and args.b: a, b = args.a, args.b elif not args.a and not args.b: - word = random_word(wordsfile='/usr/share/dict/words') - a, b = random_idents_from_query(query=word, r=2) - result.update({"extra": {"q": "https://fatcat.wiki/release/search?q={}".format(word)}}) + for _ in range(10): + word = random_word(wordsfile='/usr/share/dict/words') + try: + idents = random_idents_from_query(query=word, r=2) + except RuntimeError: + continue + result.update({"extra": {"q": "https://fatcat.wiki/release/search?q={}".format(word)}}) + a, b = idents + break + else: + raise RuntimeError('could not fetch random releases') else: raise ValueError('specify either both -a, -b or none') diff --git a/fuzzycat/utils.py b/fuzzycat/utils.py index 9aa3a4c..1cdac47 100644 --- a/fuzzycat/utils.py +++ b/fuzzycat/utils.py @@ -159,17 +159,15 @@ def random_word(func=lambda w: True, wordsfile='/usr/share/dict/words'): def random_idents_from_query(query="*", es="https://search.fatcat.wiki/fatcat_release/_search", - max_retries=10, r=2): """ Return a number of random idents from a search query. """ - for _ in range(max_retries): - resp = requests.get(es, params={"q": query}) - if resp.status_code != 200: - raise RuntimeError('could not query {} for random item: {}'.format(es, r.url)) - payload = resp.json() - if payload["hits"]["total"] < 2: - continue - idents = [doc["_source"]["ident"] for doc in payload["hits"]["hits"]] - return random.sample(idents, r) + resp = requests.get(es, params={"q": query}) + if resp.status_code != 200: + raise RuntimeError('could not query {} for random item: {}'.format(es, r.url)) + payload = resp.json() + if payload["hits"]["total"] < 2: + raise RuntimeError('to few documents') + idents = [doc["_source"]["ident"] for doc in payload["hits"]["hits"]] + return random.sample(idents, r) -- cgit v1.2.3