mckabue commited on
Commit
bd9ee95
·
1 Parent(s): 8729a8c

Add Instagram proxy route and custom script injection; enhance username validation

Browse files
Files changed (1) hide show
  1. app.py +58 -2
app.py CHANGED
@@ -6,15 +6,67 @@ import os
6
  import inspect
7
  from typing import Callable, Literal
8
  from quart import Quart, send_from_directory
 
9
  import requests
 
10
  import re
11
  import asyncio
12
  from typing import Any, Callable, Coroutine
13
  # from python_utils.get_browser import get_browser_page_async
14
  import re
 
15
 
16
  app = Quart(__name__)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  @app.route('/')
19
  async def index():
20
  """Route handler for the home page"""
@@ -72,14 +124,18 @@ def resolve_instagram_username(
72
  - Cannot start or end with a period
73
  - Cannot have consecutive periods
74
  - Cannot have periods next to underscores
 
75
  """
76
  # Regex pattern for Instagram username validation
77
- pattern = r'^(?!.*\.\.)(?!.*\._)(?!.*_\.)(?![\.])[a-zA-Z0-9](?!.*\.$)[a-zA-Z0-9._]{0,28}[a-zA-Z0-9]$'
 
 
 
78
  return re.match(pattern, username) is not None
79
  def resolve() -> bool:
80
  if not is_valid_instagram_username(username):
81
  raise Exception(f'"{username}" is not a valid instagram username')
82
- profile_uri = f"https://www.instagram.com/{username}/"
83
  profile_response = requests.get(profile_uri, allow_redirects = False)
84
  profile_response_username = get_json_value(profile_response.text, "username", "\w+") or ""
85
  logger("profile_response_username", profile_response_username)
 
6
  import inspect
7
  from typing import Callable, Literal
8
  from quart import Quart, send_from_directory
9
+ from quart import Quart, Response, request
10
  import requests
11
+ from bs4 import BeautifulSoup
12
  import re
13
  import asyncio
14
  from typing import Any, Callable, Coroutine
15
  # from python_utils.get_browser import get_browser_page_async
16
  import re
17
+ from requests_tor import RequestsTor
18
 
19
  app = Quart(__name__)
20
 
21
+
22
+ # Your custom JavaScript to inject
23
+ CUSTOM_SCRIPT = """
24
+ <script>
25
+ // Add your custom logic here
26
+ console.log('Custom script loaded in website B');
27
+ // Example: Send message to parent window
28
+ window.parent.postMessage('Hello from website B', '*');
29
+ </script>
30
+ """
31
+
32
+ @app.route('/instagram/<path:path>', methods=['GET'])
33
+ async def proxy(path: str):
34
+ # Construct the full URL for website B
35
+ site_b_url = f"https://www.instagram.com/{path}/" # f"https://websiteB.com/{path}" # Replace with actual domain
36
+
37
+ try:
38
+ # Forward the request to website B
39
+ response = requests.get(
40
+ site_b_url,
41
+ # headers={k: v for k, v in request.headers if k.lower() != 'host'},
42
+ # cookies=request.cookies,
43
+ allow_redirects=False
44
+ )
45
+ resp = Response(
46
+ response.content,
47
+ status=response.status_code
48
+ )
49
+
50
+ # Forward original headers while maintaining CORS
51
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
52
+ headers = [(k, v) for k, v in response.headers.items()
53
+ if k.lower() not in excluded_headers]
54
+
55
+ # Preserve CORS headers from original response
56
+ cors_headers = ['access-control-allow-origin',
57
+ 'access-control-allow-methods',
58
+ 'access-control-allow-headers',
59
+ 'access-control-allow-credentials']
60
+
61
+ for header in headers:
62
+ if header[0].lower() in cors_headers:
63
+ resp.headers[header[0]] = header[1]
64
+
65
+ return resp
66
+
67
+ except requests.RequestException as e:
68
+ return f"Error fetching content: {str(e)}", 500
69
+
70
  @app.route('/')
71
  async def index():
72
  """Route handler for the home page"""
 
124
  - Cannot start or end with a period
125
  - Cannot have consecutive periods
126
  - Cannot have periods next to underscores
127
+ - Can start or end with underscore
128
  """
129
  # Regex pattern for Instagram username validation
130
+ pattern = r'^(?!.*\.\.)(?!.*\._)(?!.*_\.)[a-zA-Z0-9._][a-zA-Z0-9._]{0,28}[a-zA-Z0-9._]$'
131
+ # Additional length check since regex alone might not handle it perfectly
132
+ if len(username) < 1 or len(username) > 30:
133
+ return False
134
  return re.match(pattern, username) is not None
135
  def resolve() -> bool:
136
  if not is_valid_instagram_username(username):
137
  raise Exception(f'"{username}" is not a valid instagram username')
138
+ profile_uri = f"https:/www.instagram.com/{username}/"
139
  profile_response = requests.get(profile_uri, allow_redirects = False)
140
  profile_response_username = get_json_value(profile_response.text, "username", "\w+") or ""
141
  logger("profile_response_username", profile_response_username)