@pieromqwerty/

PieShort = URL Shortener

Python

No description

fork
loading
Files
  • main.py
  • templates
  • requirements.txt
  • urls.json
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from flask import Flask, render_template, send_file, flash, request, redirect
from flask_restful import Api, Resource, reqparse
import random
import json
from numpy import *

#setting up flask
app = Flask(__name__)
api = Api(app)

with open('urls.json', 'r') as filehandle:  
    urls = json.load(filehandle)
print(len(urls))

class getUrl(Resource):
	def get(self, id):
		for data in urls:
			string = int(id)
			if(id == data["id"]):
				url = data["url"]
				return redirect(url, code=302)
		return "Id not found. Please contact me at [email protected] for assistance", 404

#@app.route('/api/link/new/', methods = ['POST'])
#def like_Post():
#	if request.method == 'POST':
#		temp = request.headers['url']
#		print(temp)
#		json = {}
#		json["id"] = len(urls)
#		json["url"] = temp
#		print(json)
#		urls.append(json)
#		return("https://short.piemadd.com/id/" + str(len(urls)))
#		with open('urls.json', 'w') as outfile:
#			json.dump(data, outfile)
#	else:
#		return 'POST method only at this endpoint'

@app.route('/')
def hello_world():
    return render_template('index.html')

api.add_resource(getUrl, "/id/<string:id>")

app.run(host='0.0.0.0',port=8080)