-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
268 lines (214 loc) · 8 KB
/
app.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from flask import (
Flask,
request,
jsonify,
render_template,
make_response,
send_from_directory,
)
import os
from psycopg2 import pool
import shortuuid
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from datetime import datetime
load_dotenv()
app = Flask(__name__, static_folder="static")
records_per_page = int(os.environ.get("RECORDS_PER_PAGE", 10))
db_pool = pool.SimpleConnectionPool(
minconn=1,
maxconn=20,
host=os.environ.get("DATABASE_HOST"),
database=os.environ.get("DATABASE_NAME"),
user=os.environ.get("DATABASE_USER"),
password=os.environ.get("DATABASE_PASSWORD"),
sslmode="require",
)
def get_db_connection():
return db_pool.getconn()
def release_db_connection(conn):
db_pool.putconn(conn)
def close_db_pool():
db_pool.closeall()
@app.route("/fetch-metadata", methods=["POST"])
def fetch_metadata():
url = request.json.get("url")
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
og_title = soup.find("meta", property="og:title")["content"]
og_description = soup.find("meta", property="og:description")["content"]
og_image = soup.find("meta", property="og:image")["content"]
return jsonify(
{"ogTitle": og_title, "ogDescription": og_description, "ogImage": og_image}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/shorten", methods=["POST"])
def shorten_url():
url = request.json.get("url")
og_title = request.json.get("ogTitle")
og_description = request.json.get("ogDescription")
og_image = request.json.get("ogImage")
short_id = shortuuid.ShortUUID().random(length=7)
current_timestamp = datetime.now()
try:
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute(
"INSERT INTO links (short_id, original_url, og_title, og_description, og_image, created_at, updated_at) VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING *",
(
short_id,
url,
og_title,
og_description,
og_image,
current_timestamp,
current_timestamp,
),
)
conn.commit()
release_db_connection(conn)
return jsonify(
{
"shortId": short_id,
"originalUrl": url,
"ogTitle": og_title,
"ogDescription": og_description,
"ogImage": og_image,
}
)
except Exception as e:
if conn:
release_db_connection(conn)
return jsonify({"error": str(e)}), 500
from datetime import datetime
@app.route("/<short_id>", methods=["GET"])
def redirect_short_url(short_id):
try:
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute(
"SELECT original_url, og_title, og_description, og_image FROM links WHERE short_id = %s",
(short_id,),
)
result = cur.fetchone()
if result is None:
release_db_connection(conn)
return "Shortened URL not found", 404
original_url, og_title, og_description, og_image = result
current_timestamp = datetime.now()
cur.execute(
"UPDATE links SET clicks = clicks + 1, updated_at = %s WHERE short_id = %s",
(current_timestamp, short_id),
)
conn.commit()
html_response = f"""
<html>
<head>
<title>{og_title or 'Redirect'}</title>
<!-- Twitter Card metadata -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="{og_title or ''}" />
<meta name="twitter:description" content="{og_description or ''}" />
<meta name="twitter:image" content="{og_image or ''}" />
<!-- Open Graph (OG) metadata -->
<meta property="og:url" content="{original_url}" />
<meta property="og:title" content="{og_title or ''}" />
<meta property="og:description" content="{og_description or ''}" />
<meta property="og:image" content="{og_image or ''}" />
</head>
<body>
<p>Redirecting...</p>
<script>
setTimeout(function () {{
window.location.href = "{original_url}";
}}, 2000);
</script>
</body>
</html>
"""
release_db_connection(conn)
return html_response
except Exception as e:
if conn:
release_db_connection(conn)
return str(e), 500
@app.route("/delete/<short_id>", methods=["DELETE"])
def delete_short_url(short_id):
try:
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute(
"DELETE FROM links WHERE short_id = %s RETURNING *", (short_id,)
)
deleted_row = cur.fetchone()
conn.commit()
if deleted_row is None:
release_db_connection(conn)
return jsonify({"error": "Shortened URL not found"}), 404
release_db_connection(conn)
return jsonify({"message": "URL deleted successfully"}), 200
except Exception as e:
if conn:
release_db_connection(conn)
return jsonify({"error": str(e)}), 500
@app.route("/robots.txt")
def serve_robots_txt():
return send_from_directory(app.static_folder, "robots.txt")
def is_valid_passcode(passcode):
correct_passcode = os.environ.get("AUTH_PASSCODE")
return passcode == correct_passcode
@app.route("/validate-passcode", methods=["POST"])
def validate_passcode():
passcode = request.json.get("passcode")
if is_valid_passcode(passcode):
response = make_response(
jsonify({"message": "Passcode validated successfully"}), 200
)
response.set_cookie("passcode", passcode)
return response
else:
return jsonify({"error": "Incorrect passcode"}), 401
@app.route("/")
def index():
passcode = request.cookies.get("passcode")
if passcode == os.environ.get("AUTH_PASSCODE"):
try:
page = request.args.get("page", default=1, type=int)
conn = get_db_connection()
with conn.cursor() as cur:
offset = (page - 1) * records_per_page
cur.execute(
"SELECT * FROM links ORDER BY updated_at DESC LIMIT %s OFFSET %s",
(records_per_page, offset),
)
links = cur.fetchall()
cur.execute("SELECT COUNT(*) FROM links")
total_records = cur.fetchone()[0]
release_db_connection(conn)
if not links:
links = []
total_records = 0
page = 1
return render_template(
"index.html",
links=links,
total_records=total_records,
page=page,
records_per_page=records_per_page,
)
except Exception as e:
if conn:
release_db_connection(conn)
return render_template(
"index.html",
links=[],
total_records=0,
page=1,
records_per_page=records_per_page,
)
return render_template("password.html")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=os.environ.get("PORT", 3000))