-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbooru.py
50 lines (38 loc) · 1.18 KB
/
booru.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
#Gelbooru Image Searcher
from bs4 import BeautifulSoup
import urllib.request
class Booru:
@staticmethod
def get_data(url):
"""
:param url: pass full url to get the JSON/XML raw data
:return: JSON/XML raw data
"""
response = urllib.request.urlopen(url)
soup = BeautifulSoup(response)
return soup
def parse(self):
"""
parse() method return a list consists of images URL.
:return: list of images URL.
"""
raise NotImplementedError("parse() method not implemented.")
class Safebooru(Booru):
base_url = "http://safebooru.org"
api_url = u"/index.php?page=dapi&s=post&q=index&tags={0}&limit={1}"
def __init__(self, tags, limit):
self.url = self.base_url + self.api_url.format(tags, limit)
def parse(self):
img_key = 'post'
data = super().get_data(self.url)
links = [dict(post.attrs)['file_url'] for post in data.findAll(img_key)]
return links
def runbooru(tags, limit=1):
tag = Safebooru(tags, limit)
return tag
def getImage(tag):
try:
tag = runbooru(tag)
return tag.parse()[0]
except IndexError:
pass