From 2965a1298ea81029cb53decd653d501021999fcb Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 30 Jun 2019 04:26:58 -0500 Subject: [PATCH] hidden.py merge into main, no longer .gitignore (oh god) --- .gitignore | 1 - app/__init__.py | 2 +- app/routes.py | 72 +++++++++++++++++++++++++++++++++++++-- app/templates/hidden.html | 56 ++++++++++++++++++++++++++++-- 4 files changed, 123 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 81ab30e..159c167 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,5 @@ key /key /migrations/* /venv/* -/app/hidden.py /app/__pycache__/* /__pycache__/* \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 34adbdd..71b02a7 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -15,4 +15,4 @@ login.login_view = 'login' db = SQLAlchemy(app) migrate = Migrate(app, db) -from app import routes, models, hidden \ No newline at end of file +from app import routes, models \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 6253f96..0910fe2 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,10 +1,12 @@ from app import app from app.models import User from app.forms import LoginForm -from app.hidden import trap from werkzeug.urls import url_parse from flask import render_template, redirect, url_for, flash, request, jsonify from flask_login import current_user, login_user, logout_user, login_required +import requests +import xmltodict +import base64 import random import string import faker @@ -12,8 +14,7 @@ import json fake = faker.Faker() -def strgen(length): return ''.join( - random.choices(list(string.ascii_letters), k=length)) +def strgen(length): return ''.join(random.choices(list(string.ascii_letters), k=length)) @app.route('/api') def api(): @@ -75,3 +76,68 @@ def login(): def logout(): logout_user() return redirect(url_for('index')) + +def boolparse(string, default=False): + # falses = ['false', '0'] + trues = ['true', '1'] + if string is None: + return default + elif string.lower() in trues: + return True + # elif string.lower() in falses: + # return False + else: + return False + +@app.route(hidden_value) +@login_required +def hidden(): + # Handled within request + tags = request.args.get('tags') or 'trap' + try: + page = int(request.args.get('page') or 1) - 1 + except (TypeError, ValueError): + return '\"page\" parameter must be Integer.
Invalid \"page\" parameter: \"{}\"'.format(request.args.get('page')) + # Handled within building + try: + count = int(request.args.get('count') or 50) + except (TypeError, ValueError): + return '\"count\" parameter must be Integer.
Invalid \"count\": \"{}\"'.format(request.args.get('count')) + base64 = boolparse(request.args.get('base64')) + # Handled within Jinja template + print(request.args.get('showsample')) + showsample = boolparse(request.args.get('showsample'), default=True) + showtags = boolparse(request.args.get('showtags')) + # Request, Parse & Build Data + data = trap(tags, page, count, base64, showsample) + print(showsample) + return render_template('hidden.html', data=data, base64=base64, showsample=showsample, showtags=showtags) + +def base64ify(url): + return base64.b64encode(requests.get(url).content).decode() + +gelbooru_url = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&tags={}&pid={}&limit={}" + +def trap(tags, page, count, base64, showsample): + # URL Building & Request + temp = gelbooru_url.format(tags, page, count) + response = requests.get(temp).text + # XML Parsing & Data Building + parse = xmltodict.parse(response) + build = [] + + for index, element in enumerate(parse['posts']['post'][:count]): + temp = { + 'index' : str(index + 1), + 'real_url' : element['@file_url'], + 'sample_url' : element['@preview_url'], + 'tags' : element['@tags'] + } + if base64: + if showsample: + temp['base64'] = base64ify(temp['sample_url']) + else: + temp['base64'] = base64ify(temp['real_url']) + + build.append(temp) + return build \ No newline at end of file diff --git a/app/templates/hidden.html b/app/templates/hidden.html index a1fb719..5805857 100644 --- a/app/templates/hidden.html +++ b/app/templates/hidden.html @@ -3,15 +3,65 @@ {{ super() }} {% endblock head %} {% block body %} +
+ +
+
+
+
+ Options +
+
+
+
+
+
+ +

+ + + + +

+
+ +
+ +

+ + + + +

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+
+
+ {% for image in data %}