-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmlfw.py
81 lines (73 loc) · 2.76 KB
/
mlfw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
mlfw.py - A simple willie module to parse tags and return results from the
mlfw site
Copyright 2013, Tim Dreyer
Licensed under the Eiffel Forum License 2.
http://bitbucket.org/tdreyer/fineline
"""
import json
from urllib import quote
import willie.web as web
from willie.module import commands, example
try:
import imp
import sys
from permissions import perm_chk
except:
try:
ffp, pathname, description = imp.find_module('permissions',['/home/dropbox/Dropbox/WillieBot'])
permissions = imp.load_source('permissions', pathname, ffp)
sys.modules['permissions'] = permissions
finally:
if ffp:
ffp.close()
from permissions import perm_chk
def mlfw_search(willie, terms):
base_url = u'http://mylittlefacewhen.com/api/v3/face/'
query_strings = u'?removed=false&limit=1' + terms
willie.debug(u"mlfw.py:mlfw_search", query_strings, u"verbose")
result = web.get(base_url + query_strings, 10)
try:
json_results = json.loads(result)
except ValueError:
willie.debug(u"mlfw.py:mlfw_search", u"Bad json returned", u"warning")
willie.debug(u"mlfw.py:mlfw_search", result, u"warning")
willie.debug(u"mlfw.py:mlfw_search",
json.dumps(json_results, sort_keys=False, indent=2),
u"verbose"
)
try:
return json_results['objects'][0]['image']
except IndexError:
return None
except TypeError:
return False
@commands(u'mlfw')
@example(u"!mlfw tag one, tag two, tag three")
def mlfw(willie, trigger):
"""Searches mlfw and returns the top result with all tags specified."""
if not perm_chk(trigger.hostmask, "Bc", willie):
return
willie.debug(u"mlfw.py:mlfw", u"Triggered ==============", u"verbose")
willie.debug(u"mlfw.py:mlfw", trigger.groups()[1], u"verbose")
list = trigger.groups()[1]
if not list:
willie.reply(u"try something like %s" % mlfw.example)
else:
willie.debug(u"mlfw.py:mlfw", list, u"verbose")
args = list.split(u',')
for i, str in enumerate(args):
args[i] = quote(str.strip())
willie.debug(u"mlfw.py:mlfw", args, u"verbose")
tags = u'&tags__all=' + u','.join(args)
willie.debug(u"mlfw.py:mlfw", tags, u"verbose")
mlfw_result = mlfw_search(willie, tags)
if mlfw_result:
willie.debug(u"mlfw.py:mlfw", mlfw_result, u"verbose")
willie.reply(u'http://mylittlefacewhen.com%s' % mlfw_result)
elif mlfw_result is False: # looks bad, but must since might be None
willie.reply(u"Uh oh, MLFW isn't working right. Try again later.")
else:
willie.reply(u"That doesn't seem to exist.")
if __name__ == "__main__":
print __doc__.strip()