-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
46 lines (34 loc) · 1.3 KB
/
app.py
File metadata and controls
46 lines (34 loc) · 1.3 KB
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
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
def extract_shorturl(short_url):
try:
# GET request to the short URL
response = requests.get(short_url, allow_redirects=False)
# All redirection links
redirection_links = []
# Follow redirections
while 'Location' in response.headers:
redirect_url = response.headers['Location']
redirection_links.append(redirect_url)
response = requests.get(redirect_url, allow_redirects=False)
final_link = response.url
redirection_links.append(final_link)
return final_link, redirection_links
except requests.exceptions.RequestException as e:
return None, [f"Error fetching the URL: {e}"]
@app.route('/extract', methods=['GET'])
def extract():
short_url = request.args.get('url')
if not short_url:
return jsonify({"error": "No URL provided"}), 400
full_link, all_links = extract_shorturl(short_url)
if full_link is None:
return jsonify({"error": "Failed to expand the URL", "details": all_links}), 500
return jsonify({
"Original URL": short_url,
"Full Link": full_link,
"All Possible Redirections": all_links
})
if __name__ == '__main__':
app.run()