forked from rDrama/rDrama
fdfd
parent
e8caeb1b2c
commit
689341d777
|
@ -30,40 +30,31 @@ def crop_and_resize(img, resize):
|
|||
return i.resize(resize, box=box)
|
||||
|
||||
|
||||
def upload_file(name, file, resize=None):
|
||||
def upload_file(file, resize=None):
|
||||
|
||||
if resize:
|
||||
tempname = name.replace("/", "_")
|
||||
print(type(file))
|
||||
file.save(tempname)
|
||||
if tempname.split('.')[-1] in ['jpg', 'jpeg']: piexif.remove(tempname)
|
||||
i = IImage.open(tempname)
|
||||
filename = "image.gif"
|
||||
file.save(filename)
|
||||
i = IImage.open(filename)
|
||||
i = crop_and_resize(i, resize)
|
||||
img = io.BytesIO()
|
||||
i.save(img, format='PNG')
|
||||
i.save(img, format='GIF')
|
||||
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(img.getvalue())})
|
||||
remove(tempname)
|
||||
remove(filename)
|
||||
try: resp = req.json()['data']
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print(req)
|
||||
print(req.text)
|
||||
return
|
||||
|
||||
else:
|
||||
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(file.read())})
|
||||
remove(name)
|
||||
try: resp = req.json()['data']
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print(req)
|
||||
print(req.text)
|
||||
return
|
||||
|
||||
try: url = resp['link'].replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") + "?maxwidth=9999"
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print(req)
|
||||
print(req.text)
|
||||
return
|
||||
|
||||
|
@ -76,16 +67,11 @@ def upload_file(name, file, resize=None):
|
|||
return(url)
|
||||
|
||||
|
||||
def upload_from_file(name, filename, resize=None):
|
||||
|
||||
tempname = name.replace("/", "_")
|
||||
|
||||
if filename.split('.')[-1] in ['jpg', 'jpeg']: piexif.remove(tempname)
|
||||
|
||||
i = IImage.open(tempname)
|
||||
def upload_from_file(filename, resize=None):
|
||||
i = IImage.open(filename)
|
||||
if resize: i = crop_and_resize(i, resize)
|
||||
img = io.BytesIO()
|
||||
i.save(img, format='PNG')
|
||||
i.save(img, format='GIF')
|
||||
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(img.getvalue())})
|
||||
remove(filename)
|
||||
try:
|
||||
|
|
|
@ -1131,9 +1131,7 @@ def multiple_plots(**kwargs):
|
|||
posts_chart.legend(loc='upper left', frameon=True)
|
||||
comments_chart.legend(loc='upper left', frameon=True)
|
||||
|
||||
name = "multiplot.png"
|
||||
|
||||
plt.savefig(name)
|
||||
plt.savefig("multiplot.png")
|
||||
plt.clf()
|
||||
|
||||
return upload_from_file(name, name)
|
||||
return upload_from_file("multiplot.png")
|
|
@ -390,7 +390,7 @@ def api_comment(v):
|
|||
return jsonify({"error": "That wasn't an image!"}), 400
|
||||
|
||||
name = f'comment/{c.base36id}/{secrets.token_urlsafe(8)}'
|
||||
url = upload_file(name, file)
|
||||
url = upload_file(file)
|
||||
|
||||
body = request.form.get("body") + f"\n![]({url})"
|
||||
body = body.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n")
|
||||
|
@ -745,7 +745,7 @@ def edit_comment(cid, v):
|
|||
if not file.content_type.startswith('image/'): return jsonify({"error": "That wasn't an image!"}), 400
|
||||
|
||||
name = f'comment/{c.base36id}/{secrets.token_urlsafe(8)}'
|
||||
url = upload_file(name, file)
|
||||
url = upload_file(file)
|
||||
|
||||
body += f"\n![]({url})"
|
||||
body = body.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n")
|
||||
|
|
|
@ -529,20 +529,14 @@ def thumbs(new_post):
|
|||
print(f'Unknown content type {x.headers.get("Content-Type")}')
|
||||
return False, f'Unknown content type {x.headers.get("Content-Type")} for submitted content'
|
||||
|
||||
name = f"posts/{post.base36id}/thumb.png"
|
||||
tempname = name.replace("/", "_")
|
||||
|
||||
with open(tempname, "wb") as file:
|
||||
with open("image.gif", "wb") as file:
|
||||
for chunk in image_req.iter_content(1024):
|
||||
file.write(chunk)
|
||||
|
||||
post.thumburl = upload_from_file(name, tempname, resize=(100, 100))
|
||||
post.thumburl = upload_from_file("image.gif", (100, 100))
|
||||
g.db.add(post)
|
||||
g.db.commit()
|
||||
|
||||
try: remove(tempname)
|
||||
except FileNotFoundError: pass
|
||||
|
||||
def archiveorg(url):
|
||||
try: requests.get(f'https://web.archive.org/save/{url}', headers={'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}, timeout=100)
|
||||
except Exception as e: print(e)
|
||||
|
@ -929,7 +923,7 @@ def submit_post(v):
|
|||
}
|
||||
|
||||
name = f'post/{new_post.base36id}/{secrets.token_urlsafe(8)}'
|
||||
new_post.url = upload_file(name, file)
|
||||
new_post.url = upload_file(file)
|
||||
new_post.domain_ref = 1 # id of i.ruqqus.ga domain
|
||||
g.db.add(new_post)
|
||||
g.db.add(new_post.submission_aux)
|
||||
|
|
|
@ -328,8 +328,8 @@ def settings_images_profile(v):
|
|||
g.db.rollback()
|
||||
abort(413)
|
||||
|
||||
highres = upload_file(name=f"profile.gif", file=request.files["profile"])
|
||||
imageurl = upload_file(name=f"profile.gif", file=request.files["profile"], resize=(100, 100))
|
||||
highres = upload_file(request.files["profile"])
|
||||
imageurl = upload_file(request.files["profile"], (100, 100))
|
||||
if imageurl:
|
||||
v.highres = highres
|
||||
v.profileurl = imageurl
|
||||
|
@ -348,7 +348,7 @@ def settings_images_banner(v):
|
|||
abort(413)
|
||||
|
||||
v.bannerurl = None
|
||||
imageurl = upload_file(name=f"banner.gif", file=request.files["banner"])
|
||||
imageurl = upload_file(request.files["banner"])
|
||||
if imageurl:
|
||||
v.bannerurl = imageurl
|
||||
g.db.add(v)
|
||||
|
|
Loading…
Reference in New Issue