-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixhost_img.py
More file actions
71 lines (56 loc) · 2.24 KB
/
pixhost_img.py
File metadata and controls
71 lines (56 loc) · 2.24 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
67
68
69
70
71
import time
import requests
from requests.adapters import HTTPAdapter, Retry
from bs4 import BeautifulSoup
class PiXhost:
"""
PiXhost image hosting API
https://pixhost.to/
"""
def __init__(self):
self.post_url = 'https://api.pixhost.to/images'
self.sess_pixhost = requests.session()
self.sess_pixhost.headers = {
# 'Content-Type': 'multipart/form-data; charset=utf-8',
'Accept': 'application/json',
}
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
self.sess_pixhost.mount('http://', adapter)
self.sess_pixhost.mount('https://', adapter)
def upload_img(self,
img_bin, # binary image, support formats: gif, png, jpeg
# other query parameters, referring to
# https://pixhost.to/api/index.html#images
adult=False, # corresponding to content_type, 0 for FS and 1 for NSFW
max_th_size=None,
gallery_hash=None,
gallery_upload_hash=None):
payload = {'content_type': int(adult)}
if max_th_size:
payload.update({'max_th_size': max_th_size})
if gallery_hash:
payload.update({'gallery_hash': gallery_hash})
if gallery_upload_hash:
payload.update({'gallery_upload_hash': gallery_upload_hash})
file = {
'img': ('rssengine', img_bin),
}
res = self.sess_pixhost.post(self.post_url, data=payload, files=file)
time.sleep(0.01)
if res.status_code == 200:
img_url = self.parse_img_url_from_response(res)
else:
raise Exception('PiXhost uploading failed! Status code: %d' % res.status_code)
return img_url
def parse_img_url_from_response(self, response):
show_url = response.json()['show_url']
html = requests.get(show_url).text
soup = BeautifulSoup(html, 'html.parser')
img_url = soup.find('img', id='image')['src']
return img_url
if __name__ == '__main__':
img_bin = requests.get('https://pixhost.to/api/images/logo.png').content
pix = PiXhost()
r = pix.upload_img(img_bin)
print(r)