jerrrycans commited on
Commit
0e2f7b8
·
verified ·
1 Parent(s): 042687a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -50
app.py CHANGED
@@ -15,38 +15,42 @@ import socket
15
 
16
  app = Flask(__name__)
17
 
18
- # Mobile emulation settings
19
  mobile_emulation = {
20
  "deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
21
  "userAgent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36"
22
  }
23
 
 
 
 
 
 
 
 
 
 
 
24
  def get_video_stream(target_url):
25
  chrome_options = Options()
26
 
27
- # --- CRITICAL DOCKER FLAGS ---
 
 
 
 
 
 
 
28
  chrome_options.add_argument('--headless=new')
29
  chrome_options.add_argument('--no-sandbox')
30
  chrome_options.add_argument('--disable-dev-shm-usage')
31
  chrome_options.add_argument('--disable-gpu')
32
- chrome_options.add_argument('--disable-software-rasterizer')
33
- chrome_options.add_argument('--disable-extensions')
34
 
35
- # --- NETWORK STABILITY FLAGS ---
36
- # This specific combo helps resolving DNS in strict containers
37
  chrome_options.add_argument('--dns-prefetch-disable')
38
  chrome_options.add_argument('--disable-features=NetworkService')
39
- chrome_options.add_argument('--force-device-scale-factor=1')
40
 
41
- # --- CERTIFICATE & SECURITY BYPASS ---
42
- chrome_options.add_argument('--ignore-certificate-errors')
43
- chrome_options.add_argument('--allow-running-insecure-content')
44
- chrome_options.add_argument('--disable-web-security')
45
-
46
- # --- DEBUGGING ---
47
- chrome_options.add_argument('--remote-debugging-port=9222')
48
-
49
- # User data dir
50
  user_data_dir = f'/tmp/chrome-user-data-{int(time.time())}'
51
  chrome_options.add_argument(f'--user-data-dir={user_data_dir}')
52
 
@@ -56,33 +60,9 @@ def get_video_stream(target_url):
56
  driver = webdriver.Chrome(service=service, options=chrome_options)
57
 
58
  try:
59
- # Debug 1: Check Python DNS
60
- try:
61
- ip = socket.gethostbyname("instagram.com")
62
- print(f"DEBUG: Python resolved instagram.com to {ip}")
63
- except Exception as e:
64
- print(f"DEBUG: Python DNS Failed: {e}")
65
-
66
- # Debug 2: Check Python HTTP
67
- try:
68
- r = requests.get("https://www.instagram.com", timeout=5)
69
- print(f"DEBUG: Python HTTP Connection Status: {r.status_code}")
70
- except Exception as e:
71
- print(f"DEBUG: Python HTTP Failed: {e}")
72
-
73
  print(f"Navigating to: {target_url}")
74
-
75
- # Set page load timeout
76
- driver.set_page_load_timeout(30)
77
-
78
- try:
79
- driver.get(target_url)
80
- except Exception as e:
81
- # If navigation fails, try one more time with simple refresh strategy
82
- print(f"Navigation error: {e}. Retrying...")
83
- driver.get(target_url)
84
 
85
- # Wait for video
86
  wait = WebDriverWait(driver, 20)
87
  video_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "video")))
88
 
@@ -90,23 +70,21 @@ def get_video_stream(target_url):
90
  print(f"Found Video SRC: {video_src}")
91
 
92
  if not video_src:
93
- raise Exception("Video element found but no src attribute")
94
 
95
- # Use Session for download to mimic the browser context
96
  session = requests.Session()
97
  headers = {
98
  "User-Agent": mobile_emulation["userAgent"],
99
  "Referer": "https://www.instagram.com/",
100
- "Range": "bytes=0-" # Ask for whole file
101
  }
102
 
103
- # Sometimes src is a blob: or similar, but for IG Reels mobile it's usually a URL
104
  response = session.get(video_src, headers=headers, stream=True)
105
 
106
  if response.status_code in [200, 206]:
107
  return io.BytesIO(response.content)
108
  else:
109
- raise Exception(f"Failed to download. Status: {response.status_code}")
110
 
111
  finally:
112
  try:
@@ -122,7 +100,7 @@ def download_route(link):
122
  link = f'https://{link}'
123
 
124
  try:
125
- print(f"Received Request: {link}")
126
  video_stream = get_video_stream(link)
127
 
128
  return send_file(
@@ -132,12 +110,12 @@ def download_route(link):
132
  download_name=f'insta_{int(time.time())}.mp4'
133
  )
134
  except Exception as e:
135
- print(f"SERVER ERROR: {e}")
136
- return jsonify({"error": str(e), "logs": "Check Space logs for DNS debug info"}), 500
137
 
138
  @app.route('/')
139
  def home():
140
- return "Instagram Downloader Ready. Use /download/YOUR_LINK"
141
 
142
  if __name__ == '__main__':
143
  app.run(host='0.0.0.0', port=7860)
 
15
 
16
  app = Flask(__name__)
17
 
 
18
  mobile_emulation = {
19
  "deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
20
  "userAgent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36"
21
  }
22
 
23
+ def get_instagram_ip():
24
+ try:
25
+ # Resolve instagram.com to IP using Python (which usually works better in containers)
26
+ ip = socket.gethostbyname("www.instagram.com")
27
+ print(f"DEBUG: Resolved www.instagram.com to {ip}")
28
+ return ip
29
+ except Exception as e:
30
+ print(f"DEBUG: DNS Resolution failed: {e}")
31
+ return None
32
+
33
  def get_video_stream(target_url):
34
  chrome_options = Options()
35
 
36
+ # 1. Resolve IP manually
37
+ ig_ip = get_instagram_ip()
38
+ if ig_ip:
39
+ # Force Chrome to map www.instagram.com to the resolved IP
40
+ # This bypasses Chrome's internal DNS resolver
41
+ chrome_options.add_argument(f'--host-resolver-rules=MAP www.instagram.com {ig_ip}, MAP .instagram.com {ig_ip}')
42
+
43
+ # 2. Standard Docker Flags
44
  chrome_options.add_argument('--headless=new')
45
  chrome_options.add_argument('--no-sandbox')
46
  chrome_options.add_argument('--disable-dev-shm-usage')
47
  chrome_options.add_argument('--disable-gpu')
 
 
48
 
49
+ # 3. Network Flags
 
50
  chrome_options.add_argument('--dns-prefetch-disable')
51
  chrome_options.add_argument('--disable-features=NetworkService')
 
52
 
53
+ # 4. User Data
 
 
 
 
 
 
 
 
54
  user_data_dir = f'/tmp/chrome-user-data-{int(time.time())}'
55
  chrome_options.add_argument(f'--user-data-dir={user_data_dir}')
56
 
 
60
  driver = webdriver.Chrome(service=service, options=chrome_options)
61
 
62
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  print(f"Navigating to: {target_url}")
64
+ driver.get(target_url)
 
 
 
 
 
 
 
 
 
65
 
 
66
  wait = WebDriverWait(driver, 20)
67
  video_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "video")))
68
 
 
70
  print(f"Found Video SRC: {video_src}")
71
 
72
  if not video_src:
73
+ raise Exception("No video src found")
74
 
75
+ # Use Python requests to download (it handles DNS better)
76
  session = requests.Session()
77
  headers = {
78
  "User-Agent": mobile_emulation["userAgent"],
79
  "Referer": "https://www.instagram.com/",
 
80
  }
81
 
 
82
  response = session.get(video_src, headers=headers, stream=True)
83
 
84
  if response.status_code in [200, 206]:
85
  return io.BytesIO(response.content)
86
  else:
87
+ raise Exception(f"Download failed: {response.status_code}")
88
 
89
  finally:
90
  try:
 
100
  link = f'https://{link}'
101
 
102
  try:
103
+ print(f"Processing: {link}")
104
  video_stream = get_video_stream(link)
105
 
106
  return send_file(
 
110
  download_name=f'insta_{int(time.time())}.mp4'
111
  )
112
  except Exception as e:
113
+ print(f"ERROR: {e}")
114
+ return jsonify({"error": str(e), "logs": "Check logs"}), 500
115
 
116
  @app.route('/')
117
  def home():
118
+ return "Instagram Downloader (DNS Bypass Mode)"
119
 
120
  if __name__ == '__main__':
121
  app.run(host='0.0.0.0', port=7860)