Kiuyha commited on
Commit
2c74f93
·
verified ·
1 Parent(s): 622c155

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, Response
2
+ import requests
3
+
4
+ app = Flask(__name__)
5
+
6
+ # Replace 'socio_secret_2026' with a strong, random password
7
+ SECRET_KEY = "socio_secret_2026"
8
+
9
+ @app.route('/api/proxy')
10
+ def proxy():
11
+ target_url = request.args.get('url')
12
+ key = request.args.get('key')
13
+
14
+ # 1. Security Check
15
+ if key != SECRET_KEY:
16
+ return "Unauthorized", 401
17
+
18
+ if not target_url:
19
+ return "Missing URL", 400
20
+
21
+ # 2. Browser-like Headers to minimize block probability
22
+ headers = {
23
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
24
+ }
25
+
26
+ try:
27
+ # 3. Fetch with allow_redirects=True to ensure you get the final page content
28
+ resp = requests.get(target_url, headers=headers, timeout=20, allow_redirects=True)
29
+
30
+ # 4. Return the content back to your Airflow scraper
31
+ return Response(
32
+ resp.text,
33
+ status=resp.status_code,
34
+ content_type=resp.headers.get('content-type', 'text/html')
35
+ )
36
+ except Exception as e:
37
+ return str(e), 500
38
+
39
+ if __name__ == '__main__':
40
+ app.run()