-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagegetter.py
75 lines (63 loc) · 2.14 KB
/
imagegetter.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
import yaml
import aiohttp
import aiofiles
import image_map
import os
import queue
import asyncio
import io
with open('config.yml', 'r', encoding='utf8') as settingsfile:
SETTINGS = yaml.load(settingsfile, yaml.Loader)
APIURL = SETTINGS['img-store-baseurl'].strip('/')
def get_link(name:str) -> str | None:
return f'{APIURL}/{image_map.get_filename(name)}'
async def get_bytes_from_http(name:str):
link = get_link(name)
async with aiohttp.ClientSession() as session:
res = await session.get(link)
binary = await res.read()
return binary
async def download_file(name:str, force=False) -> None:
filename = image_map.get_filename(name)
if os.path.exists(f'./img/{filename}') and not force:
return
else:
print(f'downloading {name}')
link = get_link(name)
res = await get_bytes_from_http(name)
file = await aiofiles.open(f'./img/{filename}', 'wb')
await file.write(res)
await file.close()
# intended for use with pycord attachment function only
async def get_file_handle(name: str) -> str | bytes:
filename = image_map.get_filename(name)
if SETTINGS['download-files']:
await download_file(name)
if os.path.exists(f'./img/{filename}'):
return f'./img/{filename}'
else:
return io.BytesIO(await get_bytes_from_http(name))
async def download_thread(filequeue: queue.Queue) -> None:
while not filequeue.empty():
try:
name = filequeue.get(timeout=1)
await download_file(name)
except:
break
async def download_all() -> None:
if not os.path.isdir('./img'):
try:
os.mkdir('./img')
except:
print('failed to make storage dir, quitting')
exit()
file_list = image_map.get_all_names()
filequeue = queue.Queue()
for file in file_list:
filequeue.put(file)
threadslist = []
async with asyncio.TaskGroup() as tg:
for i in range(SETTINGS['max-concurrent-downloads']):
threadslist.append(tg.create_task(download_thread(filequeue)))
if __name__ == '__main__':
asyncio.run(download_all())