2024-04-08 17:16:07 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Blueprint Flask: crawler splash endpoints: dashboard, onion crawler ...
|
|
|
|
'''
|
|
|
|
|
2024-04-11 12:15:47 +02:00
|
|
|
import json
|
2024-04-08 17:16:07 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2024-04-11 12:15:47 +02:00
|
|
|
from io import BytesIO
|
|
|
|
|
2024-04-08 17:16:07 +02:00
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for, Response, abort, send_file, send_from_directory
|
2024-05-06 16:21:00 +02:00
|
|
|
from flask_login import login_required
|
2024-04-08 17:16:07 +02:00
|
|
|
|
|
|
|
# Import Role_Manager
|
2024-09-05 16:40:24 +02:00
|
|
|
from Role_Manager import login_admin, login_read_only, no_cache
|
2024-04-08 17:16:07 +02:00
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
2024-04-11 12:15:47 +02:00
|
|
|
from lib import Language
|
|
|
|
from lib import Tag
|
2024-04-08 17:16:07 +02:00
|
|
|
from lib.objects import Ocrs
|
|
|
|
|
2024-04-25 14:18:22 +02:00
|
|
|
from packages import Date
|
|
|
|
|
2024-04-08 17:16:07 +02:00
|
|
|
# ============ BLUEPRINT ============
|
|
|
|
objects_ocr = Blueprint('objects_ocr', __name__, template_folder=os.path.join(os.environ['AIL_FLASK'], 'templates/objects/ocr'))
|
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
bootstrap_label = ['primary', 'success', 'danger', 'warning', 'info']
|
|
|
|
|
2024-04-11 12:15:47 +02:00
|
|
|
def create_json_response(data, status_code):
|
|
|
|
return Response(json.dumps(data, indent=2, sort_keys=True), mimetype='application/json'), status_code
|
2024-04-08 17:16:07 +02:00
|
|
|
|
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
@objects_ocr.route('/ocr/<path:filename>')
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
@no_cache
|
|
|
|
def ocr_image(filename):
|
|
|
|
if not filename:
|
|
|
|
abort(404)
|
|
|
|
if not 64 <= len(filename) <= 70:
|
|
|
|
abort(404)
|
|
|
|
filename = filename.replace('/', '')
|
|
|
|
ocr = Ocrs.Ocr(filename)
|
|
|
|
return send_file(BytesIO(ocr.draw_bounding_boxs()), mimetype='image/png')
|
|
|
|
|
|
|
|
|
2024-04-25 14:18:22 +02:00
|
|
|
@objects_ocr.route("/objects/ocrs", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def objects_ocrs():
|
|
|
|
date_from = request.args.get('date_from')
|
|
|
|
date_to = request.args.get('date_to')
|
|
|
|
show_objects = request.args.get('show_objects')
|
|
|
|
date = Date.sanitise_date_range(date_from, date_to)
|
|
|
|
date_from = date['date_from']
|
|
|
|
date_to = date['date_to']
|
|
|
|
|
|
|
|
if show_objects:
|
|
|
|
dict_objects = Ocrs.Ocrs().api_get_meta_by_daterange(date_from, date_to)
|
|
|
|
else:
|
|
|
|
dict_objects = {}
|
|
|
|
|
|
|
|
return render_template("OcrDaterange.html", date_from=date_from, date_to=date_to,
|
|
|
|
dict_objects=dict_objects, show_objects=show_objects)
|
|
|
|
|
|
|
|
|
|
|
|
@objects_ocr.route("/objects/ocrs/post", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def objects_ocrs_post():
|
|
|
|
date_from = request.form.get('date_from')
|
|
|
|
date_to = request.form.get('date_to')
|
|
|
|
show_objects = request.form.get('show_objects')
|
|
|
|
return redirect(url_for('objects_ocr.objects_ocrs', date_from=date_from, date_to=date_to, show_objects=show_objects))
|
|
|
|
|
|
|
|
|
|
|
|
@objects_ocr.route("/objects/ocrs/range/json", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def objects_ocrs_range_json():
|
|
|
|
date_from = request.args.get('date_from')
|
|
|
|
date_to = request.args.get('date_to')
|
|
|
|
date = Date.sanitise_date_range(date_from, date_to)
|
|
|
|
date_from = date['date_from']
|
|
|
|
date_to = date['date_to']
|
|
|
|
return jsonify(Ocrs.Ocrs().api_get_chart_nb_by_daterange(date_from, date_to))
|
|
|
|
|
|
|
|
|
2024-04-11 12:15:47 +02:00
|
|
|
@objects_ocr.route("/objects/ocr", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def object_ocr():
|
|
|
|
obj_id = request.args.get('id')
|
|
|
|
target = request.args.get('target')
|
|
|
|
if target == "Don't Translate":
|
|
|
|
target = None
|
|
|
|
meta = Ocrs.api_get_ocr(obj_id, target)
|
|
|
|
if meta[1] != 200:
|
|
|
|
return create_json_response(meta[0], meta[1])
|
|
|
|
else:
|
|
|
|
meta = meta[0]
|
|
|
|
languages = Language.get_translation_languages()
|
|
|
|
return render_template("ShowOcr.html", meta=meta,
|
2024-04-26 11:52:17 +02:00
|
|
|
bootstrap_label=bootstrap_label,
|
2024-04-11 12:15:47 +02:00
|
|
|
ail_tags=Tag.get_modal_add_tags(meta['id'], meta['type'], meta['subtype']),
|
|
|
|
translation_languages=languages, translation_target=target)
|
|
|
|
|
|
|
|
|
2024-04-08 17:16:07 +02:00
|
|
|
# ============= ROUTES ==============
|
|
|
|
|