~anjan/err-reddit

281d1a6916ab9f80e0e74437d05d3243537c8c11 — Anjan Momi 5 years ago fbe5d44 master
added subreddit not existing error handling and choosing from hot
1 files changed, 7 insertions(+), 1 deletions(-)

M reddit.py
M reddit.py => reddit.py +7 -1
@@ 35,11 35,17 @@ class Reddit(BotPlugin):
                arg = "+"
            url += arg

        url += "/top.json"
        url += "/hot.json"

        response = requests.get(url, headers = {'User-agent': 'Chrome'})
        parsed = json.loads(response.text)

        try:
            if len(parsed['data']['children']) == 0:
                return "The subreddit {args} does not exist".format(args=args)
        except KeyError:
            return "The subreddit {args} does not exist".format(args=args)

        randomItem = random.choice(parsed['data']['children'])
        url = randomItem['data']['url']
        title = randomItem['data']['title']