forked from David-Lor/python-qqddm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
66 lines (55 loc) · 2.2 KB
/
example.py
File metadata and controls
66 lines (55 loc) · 2.2 KB
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
import asyncio
import random
from PIL import Image
from numpy import asarray
import httpx
from qqddm import AnimeConverter, InvalidQQDDMApiResponseException, IllegalPictureQQDDMApiResponseException
PROXYFORMAT = ['http://user:pass@ip:port']
USERAGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:106.0) Gecko/20100101 Firefox/106.0"
def imagecrop(namefile: str):
image = Image.open(namefile).convert('RGB')
x = asarray(image)
lens1 = len(x)
lens = len(x[0])
if lens > lens1:
image = image.crop((0, 0, lens, lens1 - 177))
else:
image = image.crop((0, 0, lens, lens1 - 185))
image.save(namefile)
return f'{namefile}'
async def get_pic_using_api(picture_filename, PROXY):
PROXY = random.choice(PROXY)
if picture_filename.startswith("http"):
async with httpx.AsyncClient(headers={"User-Agent": USERAGENT}) as client:
r = await client.get(url=picture_filename)
r.raise_for_status()
picture_bytes = r.content
else:
with open(picture_filename, "rb") as f:
picture_bytes = f.read()
# Initialize the AnimeConverter class. Optional settings can be used for customizing the requesting behaviour.
converter = AnimeConverter(
global_useragents=[USERAGENT],
generate_proxy=PROXY,
)
# Result is returned as an `AnimeResult` object
try:
result = await converter.convert(picture_bytes)
except IllegalPictureQQDDMApiResponseException:
# The API may forbid converting images with sensible content
print("The image provided is forbidden, try with another picture")
return 0
except InvalidQQDDMApiResponseException as ex:
# If the API returned any other error, show the response body
print(f"API returned error ({ex}); response body: {ex.response_body}")
return 1
except Exception as _EX:
print(f'API returned {_EX} exception. check it up!')
return 2
pic = await converter.download_one(result.pictures_urls[0])
with open(picture_filename, 'wb') as file:
file.write(pic)
naming = imagecrop(picture_filename)
return naming
if __name__ == '__main__':
asyncio.run(get_pic_using_api('1.jpg', ["http://"]))