-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr34API.py
49 lines (40 loc) · 1.5 KB
/
r34API.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
from xml.etree.ElementTree import fromstring
from config import config
import requests
class R34API:
def __init__(self):
self.__template = config['r34api']['template']
self.__limit = config['r34api']['limit_per_request']
def __constructRequest(self, tags=None, page: int = -1, enabledLimit=False):
request = self.__template
if tags is not None:
request += '&tags='
for tag in tags:
request += (tag + '+')
if enabledLimit:
request += f'&limit={self.__limit}'
if page != -1:
request += f'&pid={page}'
return request
def __get(self, tags=None, page: int = -1, enabledLimit=False):
return fromstring(
requests.get(
self.__constructRequest(tags, page, enabledLimit)
).text
)
def getPosts(self, amount: int, postTags: set = None, postFilter: set = None, page: int = -1, enabledLimit=False):
if postTags is None:
postTags = set()
if postFilter is None:
postFilter = set()
any_in = lambda a, b: any(i in b for i in a)
posts = []
data = self.__get(postTags, page, enabledLimit)
gotPostsValue = 0
for child in data:
if not any_in(child.attrib['tags'].split(), postFilter):
posts.append(child.attrib['file_url'])
gotPostsValue += 1
if gotPostsValue == amount:
break
return posts