-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphotos.py
37 lines (26 loc) · 1014 Bytes
/
photos.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
import os.path
def photo_path(avatar_id, extension):
return os.path.join("photos", "%d.%s" % (avatar_id, extension))
def get_photo_ext(avatar_id, extension):
try:
with open(photo_path(avatar_id, extension), "rb") as fh:
return fh.read()
except FileNotFoundError:
return None
async def get_photo(session, avatar_id, image_path):
return (
get_photo_ext(avatar_id, "jpeg")
or get_photo_ext(avatar_id, "png")
or await download_photo(session, avatar_id, image_path)
)
async def download_photo(session, avatar_id, image_path):
print("FETCH: ", image_path)
async with session.get(image_path) as response:
image_data = await response.read()
if response.content_type == "image/jpeg":
extension = "jpeg"
elif response.content_type == "image/png":
extension = "png"
with open(photo_path(avatar_id, extension), "wb") as fh:
fh.write(image_data)
return image_data