From ee29a638ddf4d0e583708056ad84d3895fddb4a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Fri, 26 Jun 2020 12:07:25 +0200 Subject: [PATCH] new: API Endpoint - redirects --- website/web/__init__.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/website/web/__init__.py b/website/web/__init__.py index 45ac3c52..1d4b8b4f 100644 --- a/website/web/__init__.py +++ b/website/web/__init__.py @@ -8,7 +8,7 @@ from pathlib import Path from datetime import datetime, timedelta import json -from flask import Flask, render_template, request, send_file, redirect, url_for, Response, flash +from flask import Flask, render_template, request, send_file, redirect, url_for, Response, flash, jsonify from flask_bootstrap import Bootstrap # type: ignore from flask_httpauth import HTTPDigestAuth # type: ignore @@ -458,3 +458,26 @@ def index(): @auth.login_required def index_hidden(): return index_generic(show_hidden=True) + + +# Query API + +@app.route('/json//redirects', methods=['GET']) +def json_redirects(tree_uuid: str): + capture_dir = lookyloo.lookup_capture_dir(tree_uuid) + if not capture_dir: + return {'error': 'Unknown UUID, try again later.'} + cache = lookyloo.capture_cache(capture_dir) + if not cache: + return {'error': 'UUID missing in cache, try again later.'} + + to_return: Dict[str, list] = {'response': {'url': cache['url'], 'redirects': []}} + if not cache['redirects']: + to_return['response']['info'] = 'No redirects' + return to_return + if cache['incomplete_redirects']: + # Trigger tree build, get all redirects + lookyloo.load_tree(capture_dir) + cache = lookyloo.capture_cache(capture_dir) + to_return['response']['redirects'] = cache['redirects'] + return jsonify(to_return)