-
Notifications
You must be signed in to change notification settings - Fork 0
/
gelbooru.py
111 lines (90 loc) · 3.59 KB
/
gelbooru.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
# coding=utf8
"""
gelbooru.py - People on IRC will love your leggings fetish!
Copyright 2014 Max Gurela
Adapted for use with sopel from https://github.com/infinitylabs/uguubot/blob/master/plugins/gelbooru.py
Licensed under the Eiffel Forum License 2 (It's GPL compatible!).
"""
from __future__ import unicode_literals
from sopel.module import commands, rule
from sopel.formatting import color, colors
from sopel import tools, web
from bs4 import BeautifulSoup
import random
import re
gelbooru_cache = []
lastsearch = ''
def setup(bot):
regex = re.compile('gelbooru.com.*(?:\?|&)id\=([-_a-zA-Z0-9]+)')
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = tools.SopelMemory()
bot.memory['url_callbacks'][regex] = gelbooru_url
def refresh_cache(bot, inp):
global gelbooru_cache
gelbooru_cache = []
num = 0
if inp == '':
search = 'rating:safe'
else:
search = (inp.replace('explicit', 'rating:explicit')
.replace('nsfw', 'rating:explicit')
.replace('safe', 'rating:safe')
.replace('sfw', 'rating:safe'))
if 'rating:' not in search:
search += ' rating:safe'
soup = get_soup('http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=10&tags={0}'.format(search))
posts = soup.find_all('post')
while num < len(posts):
gelbooru_cache.append((posts[num].get('id'),
posts[num].get('score'),
posts[num].get('file_url'),
posts[num].get('rating'),
posts[num].get('tags')))
num += 1
random.shuffle(gelbooru_cache)
return
@commands('gb', 'gelbooru')
def gelbooru(bot, trigger):
"""
.gelbooru <tags> -- Gets a random image, based on given tags from gelbooru.com
"""
global lastsearch
global gelbooru_cache
if trigger.group(2):
search = trigger.group(2).strip().lower()
else:
search = ''
if not search in lastsearch or len(gelbooru_cache) < 2:
refresh_cache(bot, search)
lastsearch = search
if len(gelbooru_cache) == 0:
bot.say('No results for search "{0}"'.format(trigger.group(2).strip()))
return
post_id, score, url, rating, tags = gelbooru_cache.pop()
if 'e' in rating:
rating = color('NSFW', colors.RED)
elif 'q' in rating:
rating = color('Questionable', colors.YELLOW)
elif 's' in rating:
rating = color('Safe', colors.GREEN)
bot.say('[gelbooru] Score: {0} | Rating: {1} | http://gelbooru.com/index.php?page=post&s=view&id={2} | Tags: {3}'
.format(score, rating, post_id, tags.strip()))
@rule(r'(?:.*)(?:gelbooru.com.*?id=)([-_a-zA-Z0-9]+)(?: .+)?')
def gelbooru_url(bot, trigger):
soup = get_soup('http://gelbooru.com/index.php?page=dapi&s=post&q=index&id={0}'.format(trigger.group(1)))
posts = soup.find_all('post')
post = posts[0]
post_id, score, url, rating, tags = (post.get('id'),
post.get('score'),
post.get('file_url'),
post.get('rating'),
post.get('tags'))
if 'e' in rating:
rating = color('NSFW', colors.RED)
elif 'q' in rating:
rating = color('Questionable', colors.YELLOW)
elif 's' in rating:
rating = color('Safe', colors.GREEN)
bot.say('[gelbooru] Score: {0} | Rating: {1} | Tags: {2}'.format(score, rating, tags.strip()))
def get_soup(url):
return BeautifulSoup(web.get(url), 'lxml')