Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stable update #34

Merged
merged 37 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
08edbd4
DB and search update
Apr 27, 2019
31dc3f2
Rewrite Search query
Apr 27, 2019
6fc4b29
Fix search by module name
May 3, 2019
16beeeb
Thumbnails can be in any format now
ada-af May 29, 2019
78ef48e
Hotfix default thumbnail format
ada-af May 29, 2019
bd5fe20
Fixed
ada-af May 29, 2019
b4d15e7
Fix bad if condition
ada-af May 29, 2019
e35a959
FFmpeg set -loop option when converting to webp
ada-af May 29, 2019
90b1441
Minor changes
ada-af May 29, 2019
d145a31
Merge pull request #27 from mcilya/dbv3
ada-af May 29, 2019
1da6841
minor changes
ada-af May 31, 2019
60a644b
Loader rewrite (#28)
ada-af Jun 7, 2019
60f6d98
Minor changes
ada-af Jun 12, 2019
250ee1e
Fix typo
ada-af Jun 12, 2019
0dbfbdc
Fixed:
ada-af Jul 2, 2019
64a568c
Merge branch 'next' of github.com:anon-a/DBooru into next
ada-af Jul 2, 2019
b5e5cc9
Attempt 2 of fixing last tag bug
ada-af Jul 2, 2019
7ad3584
Last Tag bug Fix
ada-af Jul 8, 2019
a0183a5
Flask rewrite (#31)
ada-af Oct 11, 2019
2fd4c89
Night mode (#32)
ada-af Oct 14, 2019
130f0d5
Changes:
ada-af Oct 22, 2019
c8b8809
Hotfix
ada-af Oct 22, 2019
0b70c9a
Fix sideways scrolling for small screens
ada-af Oct 22, 2019
0199599
Changes:
ada-af Oct 22, 2019
0d5d92a
Added:
ada-af Oct 22, 2019
96e2675
Changes:
ada-af Oct 22, 2019
27efe02
Added:
ada-af Oct 27, 2019
2ba76f6
Minor proxy updates
ada-af Dec 8, 2019
a2e4c15
Hotfix
ada-af Dec 8, 2019
f534f52
Hotfix attempt 2
ada-af Dec 8, 2019
04a4e21
Merge pull request #33 from mcilya/next_next
ada-af Dec 8, 2019
6c7e5cc
Added:
ada-af Dec 13, 2019
51fb87c
Changes:
ada-af Dec 13, 2019
124dd75
Added:
ada-af Jan 14, 2020
be536cb
Minor fixes and updates
ada-af Feb 13, 2020
8f73fb6
Tag splitting hotfix
ada-af Feb 13, 2020
a8d35c9
Fixes:
ada-af Feb 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .TODO

This file was deleted.

243 changes: 243 additions & 0 deletions DBooru_web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
#!/usr/bin/env python3
import imghdr
import json
import math
import os
import subprocess
import sys
import socket
import time
import tempfile
from importlib import reload
from multiprocessing import Process
from threading import Thread

import jinja2
from flask import Flask, jsonify, redirect, render_template, request,\
send_from_directory, url_for, send_file, Response

import main
import settings_file
from dermod import db, threads
from dermod import input_parser as ip
from dermod import mime_types as mimes
from dermod import predict

try:
import PIL.Image as Image
except ImportError:
pass

try:
os.remove("update.lck")
except Exception:
pass

DBooru = Flask(__name__)
DBooru.config.from_pyfile("settings_file.py", silent=True)

@DBooru.route('/predict', methods=['GET'])
def predict_tag():
pred = predict.Predictor()
matched = pred.predict(request.args.get('phrase'))
return jsonify(matched)


@DBooru.route('/', methods=["GET"])
def index():
return render_template('index.html')


@DBooru.route('/search', methods=['GET'])
def search():
page = request.args.get('page', default=1, type=int)
query = request.args.get('q', default='', type=str)
db_search_list = ip.parser(query)
try:
results, total = db.search(db_search_list['search'],
db_search_list['remove'], page=page-1)
except (IndexError, KeyError):
pass
return render_template('results.html', search=query, page=page,
total_images=total, results=results, settings_file=settings_file, str=str,
ceil=math.ceil, max=max)


@DBooru.route('/image/<string:img_id>')
def image(img_id):
prefix, img_id = img_id.split("_")
image = db.search_by_id(img_id, prefix=prefix)
query = request.args.get('q')
if query is None:
query = ""
return render_template('image.html', image=image, query=query)


@DBooru.route("/raw/<string:fname>")
def raw(fname):
return send_file(settings_file.images_path+fname)


@DBooru.route("/update")
def update():
global THREAD_PORT
# Socket for thread communication
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if os.path.exists("update.lck") is False:
sock.sendto(b"UPDT", ('127.0.0.1', THREAD_PORT))
open("update.lck", 'w').write('1')
j = "DB Update started in background."
stat = 200
else:
j = "Update in process"
stat = 409
j = Response(j, status=stat)
return j


@DBooru.route("/random")
def random():
img = db.random_img()[0]
result = str("/image/"+img[-2]+str(img[-1]))
return redirect(result)


@DBooru.route("/random/<string:tags>")
def tagged_rand(tags):
tags_list = ip.parser(tags)
result = db.tagged_random(tags_list)
return redirect("/image/"+result[-2]+str(result[-1])+"?q="+tags)


@DBooru.route("/dl/<string:fname>")
def dl(fname):
return send_file(settings_file.images_path+fname, as_attachment=True)


def encode_PIL(fname, tf):
img = Image.open(settings_file.images_path+fname)
img.thumbnail((500, 500), Image.ANTIALIAS)
if fname.split('.')[-1] == 'gif':
img.save(tf.name, "GIF")
else:
img.save(tf.name, "JPEG")


def encode_FFMPEG(fname, tf):
add = "" + settings_file.ffmpeg_args
if fname.split('.')[-1] == 'gif':
form = "gif"
if settings_file.gif_to_webp == True:
form = "webp"
add = add + " -loop 0"
else:
form = settings_file.conv_format
cmd = "ffmpeg -i {fname} -vf scale=w=500:h=500:force_original_aspect_ratio=decrease {additions} -y -f {format} {tempname}"\
.format(fname=settings_file.images_path+fname, format=form, tempname=tf.name, additions=add)
os.system(cmd)


@DBooru.route("/thumbnail/<string:fname>")
def thumbnail(fname):
tf = tempfile.NamedTemporaryFile(mode="wb+", delete=False)
tf.close()
if settings_file.thumbnailer.lower() == 'ffmpeg':
encode_FFMPEG(fname, tf)
elif settings_file.thumbnailer.lower() == 'pil':
encode_PIL(fname, tf)
else:
os.remove(tf.name)
tf.name = settings_file.images_path+fname
return send_file(tf.name)


@DBooru.route("/next/<string:id>")
def next(id):
starting = int(id.split('_')[1])
query = request.args.get('q')
if query is None or query == "":
query = ""
data = db.get_next(starting)
else:
tag_list = ip.parser(query)
data = db.tagged_get_next(starting, tag_list)
if not data:
code = 404
data = ""
else:
code = 200
data = data[6]+str(data[7])

return Response(data+"?q={}".format(query), status=code)


@DBooru.route("/previous/<string:id>")
def previous(id):
starting = int(id.split('_')[1])
query = request.args.get('q')
if query is None or query == "":
query = ""
data = db.get_prev(starting)
else:
tag_list = ip.parser(query)
data = db.tagged_get_prev(starting, tag_list)
if not data:
code = 404
data = ""
else:
code = 200
data = data[6]+str(data[7])

return Response(data+"?q={}".format(query), status=code)


@DBooru.route("/json/search")
def api_search():
page = request.args.get('page', default=1, type=int)
query = request.args.get('q', default='', type=str)
db_search_list = ip.parser(query)
try:
results, total = db.search(db_search_list['search'],
db_search_list['remove'], page=page-1)
except (IndexError, KeyError):
pass
del total
result = {}
k = 0
for _ in results:
fname = {'filename': _[0]}
tags = {'tags': _[1].split(",,")[1:-2]}
height = {'height': _[2]}
width = {'widht': _[3]}
ratio = {'ratio': _[4]}
source_link = {'source_link': _[5]}
prefix = {'prefix': _[6]}
thumbnail = {'thumb': "//"+request.host +
"/thumbnail/"+prefix['prefix']+fname['filename']}
full = {'full': "//"+request.host+"/raw/" +
prefix['prefix']+fname['filename']}
__ = dict(fname, **tags, **height, **width, **ratio,
**source_link, **prefix, **thumbnail)
__.update(full)
result[k] = __
k += 1
result = json.dumps(result)
return Response(result, mimetype="application/json")


def start_background_tasks():
if settings_file.enable_polling:
mon_thread = threads.Settings_monitor()
mon_thread.start()
bg_thread = threads.BgTaskHost()
bg_thread.start()
time.sleep(1)
global THREAD_PORT
THREAD_PORT = bg_thread.port
print("Background_host thread running on 127.0.0.1:"+str(THREAD_PORT))


if __name__ == "__main__":
start_background_tasks()
DBooru.run(host=settings_file.web_ip,
port=settings_file.web_port)
Loading