From dea3b1b8966cb41918e7438ef4b1924e316df9b5 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 16 Jul 2013 02:52:55 +1200 Subject: [PATCH] Added very early version of SCP plugin --- plugins/scp.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 plugins/scp.py diff --git a/plugins/scp.py b/plugins/scp.py new file mode 100644 index 0000000..cce33d5 --- /dev/null +++ b/plugins/scp.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +from util import hook, http +import re + + +def api_get(kind, query): + """Use the RESTful Google Search API""" + url = 'http://ajax.googleapis.com/ajax/services/search/%s?' \ + 'v=1.0&safe=moderate' + return http.get_json(url % kind, q=query) + + +def get_info(url): + try: + page = http.get(url) + except Exception as e: + return "Could not get SCP information: Unable to fetch URL. ({})".format(e) + contents = re.sub('<[^<]+?>', '', page) + + try: + item_id = http.unescape(re.findall("Item #: (.+?)\n", contents, re.S)[0]) + object_class = http.unescape(re.findall("Object Class: (.+?)\n", contents, re.S)[0]) + description = http.unescape(re.findall("Description: (.+?)\n", contents, re.S)[0]) + except IndexError as e: + return "Could not get SCP information: Page was not a valid SCP page." + + return u"\x02Item Name:\x02 COMING SOON, \x02Item #:\x02 {}, \x02Class\x02: {}," \ + " \x02Description:\x02 {}.".format(item_id, object_class, description) + + +@hook.command +def scp(inp): + "scp / -- Returns SCP Foundation wiki search result for /." + + if not inp.isdigit(): + term = inp + else: + if len(inp) == 3: + term = inp + if len(inp) == 2: + term = "0" + inp + if len(inp) == 1: + term = "00" + inp + + # search for the SCP on google + search_term = "site:scp-wiki.net {}".format(term) + parsed = api_get("web", search_term) + if not parsed['responseData']['results']: + return 'Could not get SCP information: No results found.' + result = parsed['responseData']['results'][0] + url = result['unescapedUrl'] + + return get_info(url)