Skip to content
Snippets Groups Projects
main.py 2.53 KiB
Newer Older
lordjbs's avatar
yes
lordjbs committed
# shortnex v2.0
# made by jbs (https://github.com/lordjbs/)
lordjbs's avatar
lordjbs committed
# Copyright (C) 2018-2020 jbs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
lordjbs's avatar
yes
lordjbs committed

lordjbs's avatar
lordjbs committed
from flask import Flask, request, redirect, render_template
lordjbs's avatar
yes
lordjbs committed
import utils
from database import Database
import json
import time

lordjbs's avatar
lordjbs committed
VERSION = "v2.0"
print("shortnex " + VERSION + "\nmade by jbs")

print("shortnex | Loading config and flask")
lordjbs's avatar
yes
lordjbs committed
with open('config.json') as _config:
    data = json.load(_config)
lordjbs's avatar
lordjbs committed
config = {"port": data["port"], "database": data["database"], "url": data["url"]}
lordjbs's avatar
yes
lordjbs committed
db = Database(config.get("database"))

app = Flask(__name__, static_url_path='/static/')

lordjbs's avatar
lordjbs committed

lordjbs's avatar
yes
lordjbs committed
@app.route('/')
def index():
    return render_template("index.html")


lordjbs's avatar
lordjbs committed
# curl --header "Content-Type: application/json, charset=utf-8" --request POST --data '{"url":"https://example.org"}' http://localhost:5000/shorten
lordjbs's avatar
yes
lordjbs committed
@app.route("/shorten", methods=['POST'])
def shorten():
    if request.method != "POST":
        return {"success": "false", "error": "This route is POST only."}

    content = request.get_json()
    if not "url" in content:
        return {"success": False, "error": "The parameter 'url' does not exist.", "code": 1}

    if utils.checkIfProperURL(content.get("url")) is None:
        return {"success": False, "error": "That is not a proper url.", "code": 1}

    url = utils.returnProperURL(content.get("url"))
lordjbs's avatar
lordjbs committed
    # TODO: Make the url config thing better lol
lordjbs's avatar
yes
lordjbs committed
    try:
        id = utils.createID()
lordjbs's avatar
lordjbs committed
        db.addURL(id, content.get("url"), int(time.time()))
lordjbs's avatar
lordjbs committed
        return {"success": True, "id": id, "url": config.get("url")}
lordjbs's avatar
yes
lordjbs committed
    except Exception:
lordjbs's avatar
lordjbs committed
        return {"success": False, "error": "Error has occurred while shortening. Please contact an administrator."}
lordjbs's avatar
yes
lordjbs committed


@app.route("/<id>")
def goto(id):
lordjbs's avatar
lordjbs committed
    url = db.getURL(id)
    if not url:
lordjbs's avatar
yes
lordjbs committed
        return {"success": False, "error": "Invalid ID"}
    else:
lordjbs's avatar
lordjbs committed
        return redirect(utils.returnProperURL(url))
lordjbs's avatar
yes
lordjbs committed


lordjbs's avatar
lordjbs committed
if __name__ == "__main__":
lordjbs's avatar
lordjbs committed
    app.run(host='0.0.0.0', port=config.get("port"))