master
Aevann1 2021-09-09 16:11:44 +02:00
parent edeefc3e34
commit 77dfd7ccd9
7 changed files with 89 additions and 87 deletions

View File

@ -11,12 +11,10 @@ CF_ZONE = environ.get("CLOUDFLARE_ZONE", "").strip()
IMGUR_KEY = environ.get("IMGUR_KEY", "").strip()
IBB_KEY = environ.get("IBB_KEY", "").strip()
def upload_ibb(file=None, resize=False, png=False):
def upload_ibb(file, resize=False):
if file: file.save("image.gif")
if resize:
i = IImage.open("image.gif")
i = IImage.open(file)
size = 100, 100
frames = ImageSequence.Iterator(i)
@ -30,30 +28,28 @@ def upload_ibb(file=None, resize=False, png=False):
om = next(frames)
om.info = i.info
try: om.save("image.gif", save_all=True, append_images=list(frames), loop=0)
except: return
if png: filedir = "image.png"
else: filedir = "image.gif"
try: om.save(f"image.{om.format}", save_all=True, append_images=list(frames), loop=0, optimize=True, quality=30)
except Exception as e:
print(e)
return
try:
with open(filedir, 'rb') as f:
with open(file, 'rb') as f:
data={'image': base64.b64encode(f.read())}
req = requests.post(f'https://api.imgbb.com/1/upload?key={IBB_KEY}', data=data)
resp = req.json()['data']
url = resp['url']
except:
except Exception as e:
if req: print(req.json())
else: print(e)
return
return(url)
def upload_imgur(file=None, resize=False, png=False):
def upload_imgur(file, resize=False):
if file: file.save("image.gif")
if resize:
i = IImage.open("image.gif")
i = IImage.open(file)
size = 100, 100
frames = ImageSequence.Iterator(i)
@ -67,13 +63,12 @@ def upload_imgur(file=None, resize=False, png=False):
om = next(frames)
om.info = i.info
try: om.save("image.gif", save_all=True, append_images=list(frames), loop=0)
except: return(None)
if png: filedir = "image.png"
else: filedir = "image.gif"
try: om.save(f"image.{om.format}", save_all=True, append_images=list(frames), loop=0, optimize=True, quality=30)
except Exception as e:
print(e)
return
try:
with open(filedir, 'rb') as f:
with open(file, 'rb') as f:
data={'image': base64.b64encode(f.read())}
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}, data=data)
resp = req.json()['data']
@ -81,8 +76,9 @@ def upload_imgur(file=None, resize=False, png=False):
if not "_d." in url:
url = url.replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg")
if "_d." in url: url += "?maxwidth=9999"
except:
except Exception as e:
if req: print(req.json())
else: print(e)
return
new_image = Image(text=url, deletehash=resp["deletehash"])

View File

@ -1220,4 +1220,5 @@ def multiple_plots(**kwargs):
plt.savefig("image.png")
plt.clf()
return upload_imgur(png=True)
if "pcmemes.net" in request.host: return upload_ibb("image.png")
else: return upload_imgur("image.png")

View File

@ -46,7 +46,7 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None):
if not pid:
if comment.parent_submission: pid = comment.parent_submission
elif "rdrama" in request.host: pid = 6489
elif "pcm" in request.host: pid = 382
elif 'pcmemes.net' in request.host: pid = 382
else: pid = 1
try: pid = int(pid)
@ -278,8 +278,9 @@ def api_comment(v):
file=request.files["file"]
if not file.content_type.startswith('image/'): return {"error": "That wasn't an image!"}, 400
if 'pcm' in request.host: url = upload_ibb(file)
else: url = upload_imgur(file)
file.save(f"image.{file.format}", optimize=True, quality=30)
if 'pcmemes.net' in request.host: url = upload_ibb(f"image.{file.format}")
else: url = upload_imgur(f"image.{file.format}")
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").replace("\n\n\n", "\n\n")
@ -297,7 +298,7 @@ def api_comment(v):
g.db.add(c_aux)
g.db.flush()
if "pcm" in request.host and c_aux.body.lower().startswith("based"):
if 'pcmemes.net' in request.host and c_aux.body.lower().startswith("based"):
pill = re.match("based and (.{1,20}?)(-| )pilled", body, re.IGNORECASE)
c_based = Comment(author_id=BASEDBOT_ACCOUNT,
@ -693,8 +694,9 @@ def edit_comment(cid, v):
file=request.files["file"]
if not file.content_type.startswith('image/'): return {"error": "That wasn't an image!"}, 400
if 'pcm' in request.host: url = upload_ibb(file)
else: url = upload_imgur(file)
file.save(f"image.{file.format}", optimize=True, quality=30)
if 'pcmemes.net' in request.host: url = upload_ibb(f"image.{file.format}")
else: url = upload_imgur(f"image.{file.format}")
body += f"\n![]({url})"
with CustomRenderer(post_id=c.parent_submission) as renderer:

View File

@ -121,7 +121,7 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='
cutoff = now - 31536000
posts = posts.filter(Submission.created_utc >= cutoff)
posts = posts.filter_by(is_banned=False,stickied=False,private=False).filter(Submission.deleted_utc == 0)
posts = posts.filter_by(is_banned=False,stickied=False,private=False,deleted_utc = 0)
if v:
posts = posts.filter(or_(Submission.processing == False, Submission.author_id == v.id))
@ -157,6 +157,12 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='
if lt:
posts = posts.filter(Submission.created_utc < lt)
if not (v and v.shadowbanned):
#posts=posts.join(Submission.author)
posts=posts.filter(Submission.author.shadowbanned == False)
posts = [x for x in posts if not (x.author and x.author.shadowbanned) or (v and v.id == x.author_id)][:51]
if sort == "hot":
posts = sorted(posts.all(), key=lambda x: x.hotscore, reverse=True)
elif sort == "new":
@ -178,18 +184,9 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='
abort(400)
firstrange = 50 * (page - 1)
secondrange = firstrange+200
secondrange = firstrange+51
posts = posts[firstrange:secondrange]
words = ['captainmeta4', ' cm4 ', 'dissident001', 'ladine']
for post in posts:
if post.author and post.author.admin_level == 0:
for word in words:
if word in post.title.lower():
posts.remove(post)
break
if random.random() < 0.004:
for post in posts:
if post.author and post.author.shadowbanned:
@ -207,8 +204,6 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='
post.views = post.views + random.randint(7,10)
g.db.add(post)
posts = [x for x in posts if not (x.author and x.author.shadowbanned) or (v and v.id == x.author_id)][:51]
next_exists = (len(posts) == 51)
posts = posts[:50]

View File

@ -501,12 +501,12 @@ 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'
with open("image.gif", "wb") as file:
with open("image.png", "wb") as file:
for chunk in image_req.iter_content(1024):
file.write(chunk)
if 'pcm' in request.host: post.thumburl = upload_ibb(resize=True)
else: post.thumburl = upload_imgur(resize=True)
if 'pcmemes.net' in request.host: post.thumburl = upload_ibb(f"image.png", True)
else: post.thumburl = upload_imgur(f"image.png", True)
g.db.add(post)
@ -533,40 +533,40 @@ def filter_title(title):
IMGUR_KEY = environ.get("IMGUR_KEY", "").strip()
# def check_processing_thread(v, post, link, db):
def check_processing_thread(v, post, link, db):
# image_id = link.split('/')[-1].rstrip('.mp4')
# headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}
image_id = link.split('/')[-1].rstrip('.mp4')
headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}
# while True:
# # break on error to prevent zombie threads
# try:
# time.sleep(15)
while True:
# break on error to prevent zombie threads
try:
time.sleep(15)
# req = requests.get(f"https://api.imgur.com/3/image/{image_id}", headers=headers)
req = requests.get(f"https://api.imgur.com/3/image/{image_id}", headers=headers)
# status = req.json()['data']['processing']['status']
# if status == 'completed':
# post.processing = False
# db.add(post)
status = req.json()['data']['processing']['status']
if status == 'completed':
post.processing = False
db.add(post)
# send_notification(
# NOTIFICATIONS_ACCOUNT,
# v,
# f"Your video has finished processing and your [post](/post/{post.id}) is now live.",
# db=db
# )
send_notification(
NOTIFICATIONS_ACCOUNT,
v,
f"Your video has finished processing and your [post](/post/{post.id}) is now live.",
db=db
)
# db.commit()
# break
# # just in case
# elif status == 'failed':
# print(f"video upload for post {post.id} failed")
# break
# except Exception as e:
# traceback.print_exc()
# print("retard. aborting thread")
# break
db.commit()
break
# just in case
elif status == 'failed':
print(f"video upload for post {post.id} failed")
break
except Exception as e:
traceback.print_exc()
print("retard. aborting thread")
break
@app.post("/submit")
@ -891,9 +891,10 @@ def submit_post(v):
body=request.form.get("body", "")
), 403
if 'pcm' in request.host:
if 'pcmemes.net' in request.host:
if file.content_type.startswith('image/'):
new_post.url = upload_ibb(file)
file.save(f"image.{file.format}", optimize=True, quality=30)
new_post.url = upload_ibb(f"image.{file.format}")
else:
try:
post_url = upload_video(file)
@ -917,7 +918,8 @@ def submit_post(v):
), 400
else:
if file.content_type.startswith('image/'):
new_post.url = upload_imgur(file)
file.save(f"image.{file.format}", optimize=True, quality=30)
new_post.url = upload_imgur(f"image.{file.format}")
else:
try:
post_url = upload_video(file)

View File

@ -113,8 +113,9 @@ def settings_profile_post(v):
if request.headers.get("Authorization"): return {"error": f"Image files only"}, 400
else: return render_template("settings_profile.html", v=v, error=f"Image files only."), 400
if 'pcm' in request.host: url = upload_ibb(file)
else: url = upload_imgur(file)
file.save(f"image.{file.format}", optimize=True, quality=30)
if 'pcmemes.net' in request.host: url = upload_ibb(f"image.{file.format}")
else: url = upload_imgur(f"image.{file.format}")
bio += f"\n\n![]({url})"
@ -204,7 +205,7 @@ def settings_profile_post(v):
return {"message": "Theme changed!"}
quadrant = request.values.get("quadrant")
if quadrant and "pcm" in request.host.lower():
if quadrant and 'pcmemes.net' in request.host.lower():
v.quadrant = quadrant
v.customtitle = quadrant
if quadrant=="Centrist":
@ -496,13 +497,15 @@ def settings_images_profile(v):
if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403
if 'pcm' in request.host: highres = upload_ibb(request.files["profile"])
else: highres = upload_imgur(request.files["profile"])
file = request.files["profile"]
file.save(f"image.{file.format}", optimize=True, quality=30)
if 'pcmemes.net' in request.host: highres = upload_ibb(f"image.{file.format}")
else: highres = upload_imgur(f"image.{file.format}")
if not highres: abort(400)
if 'pcm' in request.host: imageurl = upload_ibb(resize=True)
else: imageurl = upload_imgur(resize=True)
if 'pcmemes.net' in request.host: imageurl = upload_ibb(f"image.{file.format}", True)
else: imageurl = upload_imgur(f"image.{file.format}", True)
if not imageurl: abort(400)
@ -523,8 +526,11 @@ def settings_images_banner(v):
if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403
if 'pcm' in request.host: imageurl = upload_ibb(request.files["banner"])
else: imageurl = upload_imgur(request.files["banner"])
file = request.files["banner"]
file.save(f"image.{file.format}", optimize=True, quality=30)
if 'pcmemes.net' in request.host: imageurl = upload_ibb(f"image.{file.format}")
else: imageurl = upload_imgur(f"image.{file.format}")
if imageurl:
v.bannerurl = imageurl

View File

@ -144,7 +144,7 @@ def leaderboard(v):
users3 = users.order_by(User.post_count.desc()).limit(10).all()
users4 = users.order_by(User.comment_count.desc()).limit(10).all()
users5 = users.order_by(User.received_award_count.desc()).limit(10).all()
if "pcm" in request.host:
if 'pcmemes.net' in request.host:
users6 = users.order_by(User.basedcount.desc()).limit(10).all()
return render_template("leaderboard.html", v=v, users1=users1, users2=users2, users3=users3, users4=users4, users5=users5, users6=users6)
return render_template("leaderboard.html", v=v, users1=users1, users2=users2, users3=users3, users4=users4, users5=users5)
@ -291,7 +291,7 @@ def mfa_qr(secret, v):
mem = io.BytesIO()
img.save(mem, format="PNG")
img.save(mem, format="PNG", optimize=True, quality=30)
mem.seek(0, 0)
return send_file(mem, mimetype="image/png", as_attachment=False)