make thumbnail code simpler

master
Aevann1 2022-08-25 17:24:32 +02:00
parent 6a21dd347b
commit 94e7457965
1 changed files with 8 additions and 36 deletions

View File

@ -489,9 +489,7 @@ def edit_post(pid, v):
return redirect(p.permalink)
def thumbnail_thread(pid):
db = db_session()
def thumbnail_thread(post):
def expand_url(post_url, fragment_url):
@ -507,14 +505,6 @@ def thumbnail_thread(pid):
else:
return f"{post_url}/{fragment_url}"
post = db.get(Submission, pid)
if not post or not post.url:
time.sleep(5)
post = db.get(Submission, pid)
if not post or not post.url: return
fetch_url = post.url
if fetch_url.startswith('/') and '\\' not in fetch_url:
@ -522,17 +512,10 @@ def thumbnail_thread(pid):
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36"}
try:
x=requests.get(fetch_url, headers=headers, timeout=5, proxies=proxies)
except:
db.close()
return
if x.status_code != 200:
db.close()
return
try: x=requests.get(fetch_url, headers=headers, timeout=5, proxies=proxies)
except: return
if x.status_code != 200: return
if x.headers.get("Content-Type","").startswith("text/html"):
soup=BeautifulSoup(x.content, 'lxml')
@ -593,24 +576,16 @@ def thumbnail_thread(pid):
break
else:
db.close()
return
else: return
elif x.headers.get("Content-Type","").startswith("image/"):
image_req=x
image = PILimage.open(BytesIO(x.content))
else:
db.close()
return
else: return
size = len(image.fp.read())
if size > 8 * 1024 * 1024:
db.close()
return
if size > 8 * 1024 * 1024: return
name = f'/images/{time.time()}'.replace('.','') + '.webp'
@ -619,9 +594,6 @@ def thumbnail_thread(pid):
file.write(chunk)
post.thumburl = process_image(name, resize=100)
db.add(post)
db.commit()
db.close()
stdout.flush()
return
@ -1005,7 +977,7 @@ def submit_post(v, sub=None):
abort(415)
if not post.thumburl and post.url:
gevent.spawn(thumbnail_thread, post.id)
gevent.spawn(thumbnail_thread, post)