all repos — python-meme-bot @ 9c164c36cd723d573c0fc185ee2db9771701d8dc

Telegram Bot that uses PIL to compute light image processing.

Api.py (view raw)

 1
 2from PIL import Image
 3from io import BytesIO
 4import requests, random, time
 5
 6url = "https://danbooru.donmai.us/post/index.json"
 7test_url = "https://testbooru.donmai.us/post/index.json"
 8
 9ratings = [
10    'g', # general
11    's', # sensitive
12    'q', # questionable
13    'e', # explicit
14]
15
16params = {
17    "limit": 100,
18    "page": random.randint(1,560),
19    #"page": 560,
20    "tags": "order:change_desc rating:"
21}
22
23def get_random_image(rating="g,s"):
24    params["tags"] += rating
25    
26    switch = True
27    while switch:
28        r = requests.get(url, params)
29        page = r.json()
30        print("Page: " + str(params['page']))
31    
32        if 'success' in page:
33            if page['success'] == False:
34                print("Error: " + page['error'])
35                print("Message: " + page['message'])
36                print("Retrying in 3 seconds...\n")
37            else:
38                print(page)
39            time.sleep(3)
40        else:
41            n = random.randint(0, 99)
42            print("File: " + str(n))
43            file_url = page[n]['file_url']
44            r = requests.get(file_url)
45            try:
46                img = Image.open(BytesIO(r.content))
47            except UnidentifiedImageError:
48                print("Unidentified image.")
49                
50            print("Done.\n")
51            return img, f"https://danbooru.donmai.us/posts/{page[n]['id']}"