Skip to content

Commit

Permalink
More robust number check
Browse files Browse the repository at this point in the history
- don't allow to large negative numbers (which go <= 0)
- don't allow the 0th comic when randomly checked
- allow a + sign in front
- remove a possible #
- don't show an error when accessing sopel-irc#404
  • Loading branch information
xZise committed Aug 8, 2014
1 parent 9604bfc commit 1545c92
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions willie/modules/xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,17 @@ def xkcd(bot, trigger):
# if no input is given (pre - lior's edits code)
if not trigger.group(2): # get rand comic
random.seed()
requested = get_info(random.randint(0, max_int + 1))
requested = get_info(random.randint(1, max_int + 1))
else:
query = trigger.group(2).strip()

# Positive or 0; get given number or latest
if query.isdigit():
if query[0] == "#":
query = query[1:]

try:
query = int(query)
if query > max_int:
bot.say(("Sorry, comic #{} hasn't been posted yet. "
"The last comic was #{}").format(query, max_int))
return
elif query == 0:
requested = latest
else:
requested = get_info(query)
# Negative: go back that many from current
elif query[0] == '-' and query[1:].isdigit():
query = int(query[1:])
requested = get_info(max_int - query)
# Non-number: google.
else:
except ValueError:
# Non-number: google.
if (query.lower() == "latest" or query.lower() == "newest"):
requested = latest
else:
Expand All @@ -95,6 +85,24 @@ def xkcd(bot, trigger):
bot.say('Could not find any comics for that query.')
return
requested = get_info(number)
else:
if query > max_int:
bot.say(("Sorry, comic #{} hasn't been posted yet. "
"The last comic was #{}").format(query, max_int))
return
elif query <= -max_int:
bot.say(("Sorry, but there were only {} comics "
"released yet").formatm(max_int))
return
elif abs(query) == 0:
requested = latest
elif query == 404 or max_int + query == 404:
bot.say("404 - Not Found") # don't error on that one
elif query > 0:
requested = get_info(query)
else:
# Negative: go back that many from current
requested = get_info(max_int + query)

message = '{} [{}]'.format(requested['url'], requested['title'])
bot.say(message)

0 comments on commit 1545c92

Please sign in to comment.