2018-05-04 11:53:29 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-12-09 07:46:37 +00:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Flask functions and routes for the trending modules page
|
|
|
|
'''
|
|
|
|
import redis
|
|
|
|
import json
|
|
|
|
import flask
|
2017-12-12 09:48:21 +00:00
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint, make_response
|
|
|
|
import difflib
|
2016-12-09 07:46:37 +00:00
|
|
|
|
|
|
|
import Paste
|
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
import Flask_config
|
|
|
|
|
|
|
|
app = Flask_config.app
|
|
|
|
cfg = Flask_config.cfg
|
|
|
|
r_serv_pasteName = Flask_config.r_serv_pasteName
|
2018-05-15 21:28:47 +00:00
|
|
|
r_serv_metadata = Flask_config.r_serv_metadata
|
2016-12-09 07:46:37 +00:00
|
|
|
max_preview_char = Flask_config.max_preview_char
|
|
|
|
max_preview_modal = Flask_config.max_preview_modal
|
2017-12-12 10:00:26 +00:00
|
|
|
DiffMaxLineLength = Flask_config.DiffMaxLineLength
|
2017-04-19 09:02:03 +00:00
|
|
|
|
|
|
|
showsavedpastes = Blueprint('showsavedpastes', __name__, template_folder='templates')
|
|
|
|
|
2016-12-09 07:46:37 +00:00
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
|
|
|
|
def showpaste(content_range):
|
|
|
|
requested_path = request.args.get('paste', '')
|
|
|
|
paste = Paste.Paste(requested_path)
|
|
|
|
p_date = str(paste._get_p_date())
|
|
|
|
p_date = p_date[6:]+'/'+p_date[4:6]+'/'+p_date[0:4]
|
|
|
|
p_source = paste.p_source
|
|
|
|
p_encoding = paste._get_p_encoding()
|
|
|
|
p_language = paste._get_p_language()
|
|
|
|
p_size = paste.p_size
|
|
|
|
p_mime = paste.p_mime
|
|
|
|
p_lineinfo = paste.get_lines_info()
|
2018-04-17 14:06:32 +00:00
|
|
|
p_content = paste.get_p_content()
|
2018-05-15 21:28:47 +00:00
|
|
|
p_duplicate_str_full_list = paste._get_p_duplicate()
|
|
|
|
|
|
|
|
p_duplicate_full_list = []
|
2016-12-09 07:46:37 +00:00
|
|
|
p_duplicate_list = []
|
|
|
|
p_simil_list = []
|
|
|
|
p_date_list = []
|
|
|
|
p_hashtype_list = []
|
|
|
|
|
|
|
|
|
2018-05-15 21:28:47 +00:00
|
|
|
for dup_list in p_duplicate_str_full_list:
|
|
|
|
dup_list = dup_list[1:-1].replace('\'', '').replace(' ', '').split(',')
|
2016-12-09 07:46:37 +00:00
|
|
|
if dup_list[0] == "tlsh":
|
2018-05-02 15:07:10 +00:00
|
|
|
dup_list[2] = 100 - int(dup_list[2])
|
2016-12-09 07:46:37 +00:00
|
|
|
else:
|
|
|
|
dup_list[2] = int(dup_list[2])
|
2018-05-15 21:28:47 +00:00
|
|
|
p_duplicate_full_list.append(dup_list)
|
2016-12-09 07:46:37 +00:00
|
|
|
|
2018-04-17 14:06:32 +00:00
|
|
|
#p_duplicate_full_list.sort(lambda x,y: cmp(x[2], y[2]), reverse=True)
|
2016-12-09 07:46:37 +00:00
|
|
|
|
|
|
|
# Combine multiple duplicate paste name and format for display
|
|
|
|
new_dup_list = []
|
|
|
|
dup_list_removed = []
|
|
|
|
for dup_list_index in range(0, len(p_duplicate_full_list)):
|
|
|
|
if dup_list_index in dup_list_removed:
|
|
|
|
continue
|
|
|
|
indices = [i for i, x in enumerate(p_duplicate_full_list) if x[1] == p_duplicate_full_list[dup_list_index][1]]
|
|
|
|
hash_types = []
|
|
|
|
comp_vals = []
|
|
|
|
for i in indices:
|
2018-05-02 15:07:10 +00:00
|
|
|
hash_types.append(p_duplicate_full_list[i][0])
|
2016-12-09 07:46:37 +00:00
|
|
|
comp_vals.append(p_duplicate_full_list[i][2])
|
|
|
|
dup_list_removed.append(i)
|
|
|
|
|
2018-05-15 21:28:47 +00:00
|
|
|
#hash_types = str(hash_types).replace("[","").replace("]","") if len(hash_types)==1 else str(hash_types)
|
|
|
|
#comp_vals = str(comp_vals).replace("[","").replace("]","") if len(comp_vals)==1 else str(comp_vals)
|
2018-05-02 15:07:10 +00:00
|
|
|
|
2016-12-09 07:46:37 +00:00
|
|
|
if len(p_duplicate_full_list[dup_list_index]) > 3:
|
|
|
|
try:
|
|
|
|
date_paste = str(int(p_duplicate_full_list[dup_list_index][3]))
|
|
|
|
date_paste = date_paste[0:4]+"-"+date_paste[4:6]+"-"+date_paste[6:8]
|
|
|
|
except ValueError:
|
|
|
|
date_paste = str(p_duplicate_full_list[dup_list_index][3])
|
|
|
|
else:
|
|
|
|
date_paste = "No date available"
|
2018-05-15 21:28:47 +00:00
|
|
|
new_dup_list.append([hash_types, p_duplicate_full_list[dup_list_index][1], comp_vals, date_paste])
|
2016-12-09 07:46:37 +00:00
|
|
|
|
|
|
|
# Create the list to pass to the webpage
|
|
|
|
for dup_list in new_dup_list:
|
|
|
|
hash_type, path, simil_percent, date_paste = dup_list
|
|
|
|
p_duplicate_list.append(path)
|
|
|
|
p_simil_list.append(simil_percent)
|
|
|
|
p_hashtype_list.append(hash_type)
|
|
|
|
p_date_list.append(date_paste)
|
|
|
|
|
|
|
|
if content_range != 0:
|
|
|
|
p_content = p_content[0:content_range]
|
|
|
|
|
|
|
|
return render_template("show_saved_paste.html", date=p_date, source=p_source, encoding=p_encoding, language=p_language, size=p_size, mime=p_mime, lineinfo=p_lineinfo, content=p_content, initsize=len(p_content), duplicate_list = p_duplicate_list, simil_list = p_simil_list, hashtype_list = p_hashtype_list, date_list=p_date_list)
|
|
|
|
|
|
|
|
# ============ ROUTES ============
|
|
|
|
|
2017-04-19 09:02:03 +00:00
|
|
|
@showsavedpastes.route("/showsavedpaste/") #completely shows the paste in a new tab
|
2016-12-09 07:46:37 +00:00
|
|
|
def showsavedpaste():
|
|
|
|
return showpaste(0)
|
|
|
|
|
2018-05-11 14:07:30 +00:00
|
|
|
@showsavedpastes.route("/showsavedrawpaste/") #shows raw
|
|
|
|
def showsavedrawpaste():
|
|
|
|
requested_path = request.args.get('paste', '')
|
|
|
|
paste = Paste.Paste(requested_path)
|
|
|
|
content = paste.get_p_content()
|
|
|
|
return content, 200, {'Content-Type': 'text/plain'}
|
2016-12-09 07:46:37 +00:00
|
|
|
|
2017-04-19 09:02:03 +00:00
|
|
|
@showsavedpastes.route("/showpreviewpaste/")
|
2016-12-09 07:46:37 +00:00
|
|
|
def showpreviewpaste():
|
|
|
|
num = request.args.get('num', '')
|
|
|
|
return "|num|"+num+"|num|"+showpaste(max_preview_modal)
|
|
|
|
|
|
|
|
|
2017-04-19 09:02:03 +00:00
|
|
|
@showsavedpastes.route("/getmoredata/")
|
2016-12-09 07:46:37 +00:00
|
|
|
def getmoredata():
|
|
|
|
requested_path = request.args.get('paste', '')
|
|
|
|
paste = Paste.Paste(requested_path)
|
2018-04-17 14:06:32 +00:00
|
|
|
p_content = paste.get_p_content()
|
2016-12-09 07:46:37 +00:00
|
|
|
to_return = p_content[max_preview_modal-1:]
|
|
|
|
return to_return
|
|
|
|
|
2017-12-12 09:48:21 +00:00
|
|
|
@showsavedpastes.route("/showDiff/")
|
|
|
|
def showDiff():
|
|
|
|
s1 = request.args.get('s1', '')
|
|
|
|
s2 = request.args.get('s2', '')
|
|
|
|
p1 = Paste.Paste(s1)
|
|
|
|
p2 = Paste.Paste(s2)
|
|
|
|
maxLengthLine1 = p1.get_lines_info()[1]
|
|
|
|
maxLengthLine2 = p2.get_lines_info()[1]
|
2017-12-12 10:00:26 +00:00
|
|
|
if maxLengthLine1 > DiffMaxLineLength or maxLengthLine2 > DiffMaxLineLength:
|
2017-12-12 09:48:21 +00:00
|
|
|
return "Can't make the difference as the lines are too long."
|
|
|
|
htmlD = difflib.HtmlDiff()
|
2018-04-17 14:06:32 +00:00
|
|
|
lines1 = p1.get_p_content().splitlines()
|
|
|
|
lines2 = p2.get_p_content().splitlines()
|
2017-12-12 09:48:21 +00:00
|
|
|
the_html = htmlD.make_file(lines1, lines2)
|
|
|
|
return the_html
|
|
|
|
|
2017-04-19 09:02:03 +00:00
|
|
|
# ========= REGISTRATION =========
|
|
|
|
app.register_blueprint(showsavedpastes)
|