Skip to content

Commit

Permalink
Handle when no results are returned
Browse files Browse the repository at this point in the history
  • Loading branch information
iranzo committed May 29, 2018
1 parent 99ca553 commit 9ed592c
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions stampy/plugin/chuck.py
Expand Up @@ -78,22 +78,27 @@ def cn(message):

text = "``` "
# we might get more than one result
result = json.loads(requests.get(url).content)
if 'result' in result:
if result['total'] != 0:
try:
totalelem = len(result['result'])
except:
totalelem = 0
if totalelem > 1:
elem = random.randint(0, totalelem - 1)
try:
result = json.loads(requests.get(url).content)
except:
result = None

if result:
if 'result' in result:
if result['total'] != 0:
try:
totalelem = len(result['result'])
except:
totalelem = 0
if totalelem > 1:
elem = random.randint(0, totalelem - 1)
else:
elem = 0
text += result['result'][elem]['value']
else:
elem = 0
text += result['result'][elem]['value']
text += "Chuck Norris didn't said a word about it."
else:
text += "Chuck Norris didn't said a word about it."
else:
text += result['value']
text += result['value']

text += " ```"
stampy.stampy.sendmessage(chat_id=chat_id, text=text,
Expand Down

0 comments on commit 9ed592c

Please sign in to comment.