-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepvpimg.py
46 lines (36 loc) · 1.51 KB
/
epvpimg.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
"""
epvpimg uploader
"""
__title__ = 'epvpimg uploader'
__version__ = '0.1.0'
__build__ = "0000"
__author__ = 'sharpshadow ([email protected])'
__license__ = 'Apache 2.0'
import json
import os
import requests
class Epvpimg:
"""Upload a single image to epvpimg.com and return the values in a dict"""
def __init__(self):
self.allowed_filetypes = ['BMP', 'GIF', 'JPE', 'JPG', 'JPEG', 'PNG', 'SVG', 'TIF', 'TIFF']
self.max_filesize = 10000000
self.max_filesize_gif = 2000000
def upload(self, path, directlink=None):
"""directlink: return only image url"""
if os.path.exists(path) is True:
filesize = os.path.getsize(path)
filename = path.split('\\').pop()
filetype = filename.split('.').pop()
if (filetype.upper() not in self.allowed_filetypes or
filesize > self.max_filesize or
filetype.upper() == 'GIF' and filesize > self.max_filesize_gif
):
raise Exception('Image does not match the terms of epvpimg.com.')
files = {'files[]': (filename, open(path, 'rb'), 'image/{}'.format(filetype.lower()))}
response = requests.post('http://epvpimg.com/upload/', files=files)
if directlink is True:
return json.loads(response.text).pop()['thumbnail_url']
else:
return json.loads(response.text).pop()
else:
raise Exception('File does not exists.')