From 9333b9ad99ee0fdb47a94882fce329bbfb193374 Mon Sep 17 00:00:00 2001 From: Barbu Paul - Gheorghe Date: Fri, 29 Jan 2016 14:01:31 +0200 Subject: [PATCH] readded so.py file --- src/cmds/so.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/cmds/so.py diff --git a/src/cmds/so.py b/src/cmds/so.py new file mode 100644 index 0000000..c048e15 --- /dev/null +++ b/src/cmds/so.py @@ -0,0 +1,39 @@ +import stackexchange +import urllib2 + +api_key = 'TODO: populate this variable' + +def so(components): # !so + '''Search the Stack Overflow site and returns the first question's title and + URL + ''' + response = '' + + terms = components['arguments'].split('!so ') # notice the trailing space + + if 1 == len(terms): # no search term given + response = 'Usage: !so ' + else: + if terms[1].lstrip(): + so = stackexchange.Site(stackexchange.StackOverflow, api_key) + + try: + qs = so.search(intitle = terms[1].lstrip()) + except urllib2.HTTPError, e: + response = "The server couldn't fulfill the request!" + + if hasattr(e, 'reason'): # pragma: no branch + response = response + '\r\nReason: ' + str(e.reason) + + if hasattr(e, 'code'): # pragma: no branch + response = response + '\r\nCode: ' + str(e.code) + else: + + if 1 <= len(qs): + response = qs[0].title + '\r\n' + qs[0].url + else: + response = 'Not found: ' + terms[1] + else: + response = 'Usage: !so ' + + return str(response)