from flask import Flask, request
from pprint import pformat
import json
app = Flask(__name__)

import urllib2
from urllib import quote
import re
from random import shuffle

def gif(searchterm, unsafe=False):
    searchterm = quote(searchterm)

    safe = "&safe=" if unsafe else "&safe=active"
    searchurl = "https://www.google.com/search?tbs=itp:animated&tbm=isch&q={0}{1}".format(searchterm, safe)

    # this is an old iphone user agent. Seems to make google return good results.
    useragent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Versio  n/4.0.5 Mobile/8A293 Safari/6531.22.7"

    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', useragent)]
    result = opener.open(searchurl).read()

    gifs = re.findall(r'imgurl.*?(http.*?)\\', result)
    shuffle(gifs)

    return gifs[0] if gifs else ""

@app.route("/", methods=['POST'])
def hello():
    text = request.form.get("text", "")
    if text.startswith("!gif"):
        match = re.findall(r"!gif (.*)", text)
        term = match[0] if match else "dumb running sonic"
        return json.dumps({"text": gif(term), "parse": "full"})
    return ""

if __name__ == "__main__":
    app.run()

@@ -2,21 +2,20 @@
 rom 
-flask
+glob
  import 
 Flas
@@ -14,882 +14,1193 @@
 ort 
-Flask, request%0Afrom pprint import pformat%0Aimport json%0Aapp = Flask(__name__)%0A%0Aimport urllib2%0Afrom urllib import quote%0Aimport re%0Afrom random import shuffle%0A%0Adef gif(searchterm, unsafe=False):%0A    searchterm = quote(searchterm)%0A%0A    safe = %22&safe=%22 if unsafe else %22&safe=active%22%0A    searchurl = %22https://www.google.com/search?tbs=itp:animated&tbm=isch&q=%7B0%7D%7B1%7D%22.format(searchterm, safe)%0A%0A    # this is an old iphone user agent. Seems to make google return good results.%0A    useragent = %22Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Versio  n/4.0.5 Mobile/8A293 Safari/6531.22.7%22%0A%0A    opener = urllib2.build_opener()%0A    opener.addheaders = %5B('User-agent', useragent)%5D%0A    result = opener.open(searchurl).read()%0A%0A    gifs = re.findall(r'imgurl.*?(http.*?)%5C%5C', result)%0A    shuffle(gifs)%0A%0A    return gifs%5B0%5D if gifs else %22%22
+glob%0Aimport importlib%0Aimport json%0Aimport re%0Aimport sys%0Aimport traceback%0A%0Afrom flask import Flask, request%0Aapp = Flask(__name__)%0A%0Ahooks = %7B%7D%0Adef init_plugins():%0A    for plugin in glob('plugins/%5B!_%5D*.py'):%0A        print %22plugin: %25s%22 %25 plugin%0A        try:%0A            mod = importlib.import_module(plugin.replace(%22/%22, %22.%22)%5B:-3%5D)%0A            modname = mod.__name__.split('.')%5B1%5D%0A%0A            for hook in re.findall(%22on_(%5Cw+)%22, %22 %22.join(dir(mod))):%0A                hookfun = getattr(mod, %22on_%22 + hook)%0A                print %22attaching %25s.%25s to %25s%22 %25 (modname, hookfun, hook)%0A                hooks.setdefault(hook, %5B%5D).append(hookfun)%0A%0A            if mod.__doc__:%0A                firstline = mod.__doc__.split('%5Cn')%5B0%5D%0A                hooks.setdefault('help', %5B%5D).append(firstline)%0A                hooks.setdefault('extendedhelp', %7B%7D)%5Bmodname%5D = mod.__doc__%0A%0A        #bare except, because the modules could raise any number of errors%0A        #on import, and we want them not to kill our server%0A        except:%0A            print %22import failed on module %25s, module not loaded%22 %25 plugin%0A            print %22%25s%22 %25 sys.exc_info()%5B0%5D%0A            print %22%25s%22 %25 traceback.format_exc()%0A%0Ainit_plugins()
 %0A%0A@a
@@ -1239,21 +1239,20 @@
 def 
-hello
+main
 ():%0A    
 text
@@ -1251,256 +1251,160 @@
     
-text = request.form.get(%22text%22, %22%22)%0A    if text.startswith(%22!gif%22):%0A        match = re.findall(r%22!gif (.*)%22, text)%0A        term = match%5B0%5D if match else %22dumb running sonic%22%0A        return json.dumps(%7B%22text%22: gif(term), %22parse%22: %22full%22%7D)%0A    return
+print hooks%0A    response = %22%5Cn%22.join(hook(request.form) for hook in hooks.get(%22message%22, %5B%5D))%0A    return json.dumps(%7B%22text%22: response%7D) if response else
  %22%22%0A
@@ -1443,10 +1443,20 @@
 app.run(
+debug=True
 )%0A

modify slask to load plugins