2023-05-25 12:33:12 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Blueprint Flask: crawler splash endpoints: dashboard, onion crawler ...
|
|
|
|
'''
|
|
|
|
|
2023-06-13 13:29:11 +00:00
|
|
|
import json
|
2023-05-25 12:33:12 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for, Response, abort, send_file
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
|
|
|
# Import Role_Manager
|
|
|
|
from Role_Manager import login_admin, login_analyst, login_read_only
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib.objects import Titles
|
|
|
|
from packages import Date
|
|
|
|
|
|
|
|
# ============ BLUEPRINT ============
|
|
|
|
objects_title = Blueprint('objects_title', __name__, template_folder=os.path.join(os.environ['AIL_FLASK'], 'templates/objects/title'))
|
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
bootstrap_label = ['primary', 'success', 'danger', 'warning', 'info']
|
|
|
|
|
|
|
|
# ============ FUNCTIONS ============
|
2023-06-13 13:29:11 +00:00
|
|
|
def create_json_response(data, status_code):
|
|
|
|
return Response(json.dumps(data, indent=2, sort_keys=True), mimetype='application/json'), status_code
|
|
|
|
|
|
|
|
# ============= ROUTES ==============
|
2023-05-25 12:33:12 +00:00
|
|
|
@objects_title.route("/objects/title", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def objects_titles():
|
|
|
|
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 = Titles.Titles().api_get_meta_by_daterange(date_from, date_to)
|
|
|
|
else:
|
|
|
|
dict_objects = {}
|
|
|
|
|
|
|
|
return render_template("TitleDaterange.html", date_from=date_from, date_to=date_to,
|
|
|
|
dict_objects=dict_objects, show_objects=show_objects)
|
|
|
|
|
|
|
|
@objects_title.route("/objects/title/post", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def objects_titles_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_title.objects_titles', date_from=date_from, date_to=date_to, show_objects=show_objects))
|
|
|
|
|
|
|
|
@objects_title.route("/objects/title/range/json", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def objects_title_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(Titles.Titles().api_get_chart_nb_by_daterange(date_from, date_to))
|
|
|
|
|
|
|
|
@objects_title.route("/objects/title/search", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def objects_title_search():
|
2023-06-13 13:29:11 +00:00
|
|
|
to_search = request.form.get('to_search')
|
|
|
|
type_to_search = request.form.get('search_type', 'id')
|
|
|
|
case_sensitive = request.form.get('case_sensitive')
|
|
|
|
case_sensitive = bool(case_sensitive)
|
|
|
|
titles = Titles.Titles()
|
|
|
|
|
|
|
|
if type_to_search == 'id':
|
|
|
|
if len(type_to_search) == 64:
|
|
|
|
title = Titles.Title(to_search)
|
|
|
|
if not title.exists():
|
|
|
|
abort(404)
|
|
|
|
else:
|
|
|
|
return redirect(title.get_link(flask_context=True))
|
|
|
|
else:
|
|
|
|
search_result = titles.search_by_id(to_search, r_pos=True, case_sensitive=case_sensitive)
|
|
|
|
elif type_to_search == 'content':
|
|
|
|
search_result = titles.search_by_content(to_search, r_pos=True, case_sensitive=case_sensitive)
|
2023-05-25 12:33:12 +00:00
|
|
|
else:
|
2023-06-13 13:29:11 +00:00
|
|
|
return create_json_response({'error': 'Unknown search type'}, 400)
|
2023-05-25 12:33:12 +00:00
|
|
|
|
2023-06-13 13:29:11 +00:00
|
|
|
if search_result:
|
|
|
|
dict_objects = titles.get_metas(search_result.keys(), options={'sparkline'})
|
|
|
|
else:
|
|
|
|
dict_objects = {}
|
2023-05-25 12:33:12 +00:00
|
|
|
|
2023-06-13 13:29:11 +00:00
|
|
|
return render_template("search_title_result.html", dict_objects=dict_objects, search_result=search_result,
|
|
|
|
to_search=to_search, case_sensitive=case_sensitive, type_to_search=type_to_search)
|