/
mbgifs.py
127 lines (87 loc) · 3.22 KB
/
mbgifs.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from mbclient import mb
import json,urllib,urllib2,re,traceback
from random import choice,shuffle
show_me="^show\s+me\s+(?P<query>.*?)(?:\s+#(?P<index>\d+))?$"
lewd = "^lewd\s+(?P<what>.+)"
tumblr="tumblr\s+(?:(?P<type>photo|text)\s+)?(?P<what>.+)"
html_tags = re.compile(r'<[^>]+>')
def tumblr_func(nick,match,target):
t=match.group("type")
if t:
types=[t]
else:
types=["text","photo"]
what = match.group('what')
query = urllib.urlencode({'tag':what})
try:
req = urllib2.Request("https://api.tumblr.com/v2/tagged?api_key=oBkePh2f80LWzw0jfO3DEYz2YLfRRCmmz8VM9rsv5cxwa4kGAZ&"+query)
response = json.loads(urllib2.urlopen(req).read().decode('utf-8'))
except:
mb.tell("whoa there hol up something aint right",target)
traceback.print_exc()
return
posts=response["response"]
shuffle(posts)
for post in posts:
if post["type"] in types:
if post["type"] =="photo":
photo = choice(post["photos"])["original_size"]["url"]
mb.tell(nick+": "+photo,target)
mb.tell("post: "+post["short_url"],target)
return
else:
msg=re.sub("\n|\r"," ",re.sub(html_tags,"",post["body"]))
mb.tell(msg[:200],target)
mb.tell("post: "+post["short_url"],target)
return
mb.tell(nick+": nope, nothing like that", target)
def lewd_func(nick,match,target):
what = match.group('what')
query = urllib.urlencode({'tag':what})
tumbles = mb.data['stuff']['porn-blogs']
shuffle(tumbles)
posts = []
result = None
for tumble in tumbles:
try:
req = urllib2.Request("https://api.tumblr.com/v2/blog/"+tumble+".tumblr.com/posts/photo?api_key=oBkePh2f80LWzw0jfO3DEYz2YLfRRCmmz8VM9rsv5cxwa4kGAZ&"+query)
response = json.loads(urllib2.urlopen(req).read().decode('utf-8'))
except:
mb.tell("whoa there hol up something aint right",target)
traceback.print_exc()
return
posts = response['response']['posts']
shuffle(posts)
if posts == []:
continue
else:
for post in posts:
photo = choice(post['photos'])['original_size']['url']
if re.search(".gif$",photo):
mb.tell(nick+": "+photo,target)
mb.tell("post: "+post["short_url"],target)
return
mb.tell(nick+": none of that here, what is wrong with you",target)
def show_me_func(nick,match,target):
index = match.group('index')
headers={'X-Mashape-Key': '6SRh5ZIyhhmshOjLLIEVlfRzZR3Mp1KJgLsjsny2Vq36opmhI6',
'Accept':'application/json'}
query=urllib.urlencode({"q":match.group('query')})
req=urllib2.Request('https://giphy.p.mashape.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&'+query,headers=headers)
response=urllib2.urlopen(req).read()
results=json.loads(response.decode("utf-8"))["data"]
if results==[]:
mb.tell(nick+": none of that stuff", target)
return
if index:
data=results[int(index)]
else:
data=choice(results)
gif="https://media.giphy.com/media/{id}/giphy.gif".format(id=data['id'])
mb.tell(nick+": "+gif,target,True)
mb.add_command(show_me,show_me_func,priority=3)
mb.add_command(lewd,lewd_func)
mb.add_command(tumblr,tumblr_func)
mb.help["gifs"]="mb show me <whatever> (searches for <whatever> on giphy), mb lewd <whatever> (searches for gifs in \\tagged of several porn tumblrs)"
mb.help["tumblr"]="mb tumblr [text|photo](optional) <something>"
print("loaded gifs")