forked from parkun-by/parkun-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.py
32 lines (23 loc) · 877 Bytes
/
uploader.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
import aiohttp
import requests
import tempfile
class Uploader:
def __init__(self):
self._http_session = aiohttp.ClientSession()
def __del__(self):
self._http_session.close()
async def get_permanent_url(self, url):
filename = tempfile.gettempdir() + '/' + url.split('/')[-1]
async with self._http_session.get(url) as resp:
raw_file = await resp.content.read()
with open(filename, 'wb') as file:
file.write(raw_file)
with open(filename, 'rb') as file:
upload_url = 'https://telegra.ph/upload'
files = {'file': ('file', file, 'image/jpg')}
result = requests.post(upload_url, files=files).json()
try:
full_path = 'https://telegra.ph' + result[0]['src']
except Exception:
full_path = url
return full_path