Chrunos commited on
Commit
7a1252c
·
verified ·
1 Parent(s): fc58599

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +379 -130
app.py CHANGED
@@ -1,5 +1,5 @@
1
- from fastapi import FastAPI, HTTPException, status, BackgroundTasks
2
- from fastapi.responses import StreamingResponse
3
  import instaloader
4
  import requests
5
  import os
@@ -7,7 +7,12 @@ import time
7
  import logging
8
  import random
9
  from functools import wraps
 
10
  from datetime import datetime, timedelta
 
 
 
 
11
 
12
  # Configure logging
13
  logging.basicConfig(
@@ -25,21 +30,73 @@ INSTAGRAM_PASSWORD = os.getenv('INSTAGRAM_PASSWORD')
25
  # Session configuration
26
  SESSION_DIR = "/tmp/instagram_sessions"
27
  SESSION_FILE = os.path.join(SESSION_DIR, f"session-{INSTAGRAM_USERNAME}") if INSTAGRAM_USERNAME else None
 
28
 
29
- # Create session directory if not exists
30
  os.makedirs(SESSION_DIR, exist_ok=True)
 
31
 
32
- # Simple memory cache
33
- STORY_CACHE = {}
34
- CACHE_EXPIRY = {}
 
 
 
 
 
 
35
 
36
  USER_AGENTS = [
37
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
38
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15",
39
- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
 
 
40
  ]
41
 
42
- def get_instaloader() -> instaloader.Instaloader:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  """Create and configure Instaloader instance with proper parameters"""
44
  L = instaloader.Instaloader(
45
  sleep=True,
@@ -63,27 +120,58 @@ def get_instaloader() -> instaloader.Instaloader:
63
  )
64
 
65
  try:
66
- if SESSION_FILE and os.path.exists(SESSION_FILE):
67
  logger.info(f"Attempting to load session from {SESSION_FILE}")
68
  try:
69
  L.load_session_from_file(INSTAGRAM_USERNAME, SESSION_FILE)
70
  logger.info("Session loaded successfully")
 
71
  return L
72
  except Exception as e:
73
  logger.warning(f"Session load failed: {str(e)}, performing fresh login")
74
 
75
- # Add delay before login
76
  time.sleep(random.uniform(1.0, 3.0))
77
 
78
  logger.info("Starting fresh login process")
79
  L.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  if SESSION_FILE:
82
  L.save_session_to_file(SESSION_FILE)
83
  logger.info(f"Saved new session to {SESSION_FILE}")
84
 
 
 
 
85
  return L
86
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  except Exception as e:
88
  logger.error("Login failed: %s", str(e))
89
  raise HTTPException(
@@ -91,46 +179,75 @@ def get_instaloader() -> instaloader.Instaloader:
91
  detail="Instagram login service unavailable"
92
  )
93
 
94
- # Check for cached stories
95
- def get_cached_stories(username):
96
- if username in STORY_CACHE and username in CACHE_EXPIRY:
97
- if datetime.now() < CACHE_EXPIRY[username]:
98
- logger.info(f"Cache hit for {username}")
99
- return STORY_CACHE[username]
100
- else:
101
- # Expired cache
102
- logger.info(f"Cache expired for {username}")
103
- STORY_CACHE.pop(username, None)
104
- CACHE_EXPIRY.pop(username, None)
105
- return None
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
- # Save stories to cache
108
- def cache_stories(username, stories_data, ttl_minutes=15):
109
- STORY_CACHE[username] = stories_data
110
- CACHE_EXPIRY[username] = datetime.now() + timedelta(minutes=ttl_minutes)
111
- logger.info(f"Cached stories for {username} for {ttl_minutes} minutes")
 
 
 
 
 
 
 
 
 
 
112
 
113
  def handle_instagram_errors(func):
114
  @wraps(func)
115
  async def wrapper(*args, **kwargs):
 
 
 
 
 
116
  try:
117
- # Extract username if present
118
- request_username = ""
119
- if len(args) > 1 and hasattr(args[1], '__str__'):
120
- request_username = str(args[1])
 
 
 
 
 
 
 
 
 
121
 
122
- # Check cache if it's a username request
123
- if request_username:
124
- cached_data = get_cached_stories(request_username)
125
- if cached_data:
126
- return {**cached_data, "from_cache": True}
127
-
128
- # If not in cache, proceed with request
129
  response = await func(*args, **kwargs)
130
 
131
- # Cache successful username responses
132
  if request_username and isinstance(response, dict) and "data" in response:
133
- cache_stories(request_username, response)
134
 
135
  return response
136
 
@@ -138,31 +255,77 @@ def handle_instagram_errors(func):
138
  error_message = str(e)
139
  logger.error("Connection error: %s", error_message)
140
 
141
- # Check for rate limit error
 
142
  if "429 Too Many Requests" in error_message:
143
- # Try to extract retry time
144
- retry_minutes = 30 # Default
145
  try:
146
  retry_text = error_message.split("retried in")[1].split("minutes")[0].strip()
147
- retry_minutes = int(retry_text)
148
- except:
149
- pass
 
 
 
 
 
 
 
 
 
 
150
 
151
  raise HTTPException(
152
  status_code=status.HTTP_429_TOO_MANY_REQUESTS,
153
- detail=f"Instagram rate limit exceeded. Please try again in {retry_minutes} minutes."
154
  )
155
- elif "401 Unauthorized" in error_message:
 
 
 
 
 
 
 
 
 
 
156
  raise HTTPException(
157
  status_code=status.HTTP_429_TOO_MANY_REQUESTS,
158
  detail="Instagram rate limit exceeded. Please try again later."
159
  )
 
 
 
 
 
160
  else:
161
  raise HTTPException(
162
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
163
  detail="Instagram service unavailable"
164
  )
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  except Exception as e:
167
  logger.error("Unexpected error: %s", str(e))
168
  raise HTTPException(
@@ -177,86 +340,119 @@ async def get_stories(username: str, background_tasks: BackgroundTasks, cached:
177
  """Retrieve stories with enhanced anti-detection measures"""
178
  logger.info(f"Processing request for @{username}")
179
 
180
- # If cached param is true, only return from cache
181
  if cached:
182
- cached_response = get_cached_stories(username)
183
  if cached_response:
184
  return {**cached_response, "from_cache": True}
185
- else:
186
- logger.info("No cache available for requested username")
187
 
188
  try:
189
- L = get_instaloader()
190
- logger.info("Instaloader instance configured")
191
-
192
- # Randomized delay before profile request
193
- time.sleep(random.uniform(1.0, 2.0))
194
-
195
- # Profile lookup
196
- logger.info(f"Fetching profile for {username}")
197
- try:
198
- profile = instaloader.Profile.from_username(L.context, username)
199
- except Exception as e:
200
- logger.error(f"Profile fetch error: {str(e)}")
201
- raise
202
-
203
- logger.info(f"Profile found: {profile.username}")
204
- if not profile.has_viewable_story:
205
- logger.warning("No viewable story")
206
- raise HTTPException(
207
- status.HTTP_404_NOT_FOUND,
208
- "No accessible stories for this profile"
209
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
- # Additional delay before story fetch
212
- time.sleep(random.uniform(1.0, 2.0))
213
-
214
- logger.info("Fetching stories")
215
- stories = []
216
- try:
217
- for story in L.get_stories(userids=[profile.userid]):
218
- for item in story.get_items():
219
- # Create a story dict with safe attribute access
220
- story_data = {
221
- "id": str(item.mediaid),
222
- "url": item.url,
223
- "type": "video" if item.is_video else "image",
224
- "timestamp": item.date_utc.isoformat(),
225
- }
226
-
227
- # Only try to add view_count if it's a video
228
- if item.is_video:
229
- try:
230
- if hasattr(item, 'view_count'):
231
- story_data["views"] = item.view_count
232
- except AttributeError:
233
- pass
234
-
235
- stories.append(story_data)
236
-
237
- except Exception as e:
238
- logger.error(f"Story fetch error: {str(e)}")
239
- raise
240
 
241
- if not stories:
242
- logger.info("No active stories found")
243
- raise HTTPException(
244
- status.HTTP_404_NOT_FOUND,
245
- "No active stories available"
246
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
- # Save session in background
249
- background_tasks.add_task(lambda: L.save_session_to_file(SESSION_FILE) if SESSION_FILE else None)
250
-
251
- logger.info(f"Returning {len(stories)} stories")
252
- result = {
253
- "data": stories,
254
- "count": len(stories),
255
- "username": username,
256
- "fetched_at": datetime.now().isoformat()
257
- }
 
 
 
 
 
 
 
 
 
 
258
 
259
- return result
 
260
 
261
  except Exception as e:
262
  logger.error(f"Critical failure: {str(e)}")
@@ -269,28 +465,37 @@ async def download_media(url: str):
269
  logger.info(f"Download request for: {url}")
270
 
271
  try:
272
- # Basic validation
273
  if not url.startswith(("https://instagram", "https://scontent")):
274
  raise HTTPException(
275
  status_code=status.HTTP_400_BAD_REQUEST,
276
  detail="Invalid URL format"
277
  )
278
 
279
- # Random delay
280
- time.sleep(random.uniform(0.5, 1.0))
281
 
282
- # Configure headers
283
  headers = {
284
  "User-Agent": random.choice(USER_AGENTS),
285
  "Accept": "*/*",
286
- "Referer": "https://www.instagram.com/"
 
 
 
 
 
 
 
 
287
  }
288
 
289
- # Request the media
290
- response = requests.get(url, headers=headers, stream=True, timeout=10)
 
291
  response.raise_for_status()
292
 
293
- # Determine content type
294
  if "Content-Type" in response.headers:
295
  content_type = response.headers["Content-Type"]
296
  elif url.endswith((".jpg", ".jpeg")):
@@ -306,10 +511,15 @@ async def download_media(url: str):
306
  return StreamingResponse(
307
  response.iter_content(chunk_size=8192),
308
  media_type=content_type,
309
- headers={"Content-Disposition": f"attachment; filename={url.split('/')[-1]}"}
 
 
 
 
 
310
  )
311
 
312
- except Exception as e:
313
  logger.error(f"Download failed: {str(e)}")
314
  raise HTTPException(
315
  status_code=status.HTTP_502_BAD_GATEWAY,
@@ -319,4 +529,43 @@ async def download_media(url: str):
319
  # Add a health check endpoint
320
  @app.get("/health")
321
  async def health_check():
322
- return {"status": "ok", "timestamp": datetime.now().isoformat()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, status, BackgroundTasks, Request, Depends
2
+ from fastapi.responses import StreamingResponse, JSONResponse
3
  import instaloader
4
  import requests
5
  import os
 
7
  import logging
8
  import random
9
  from functools import wraps
10
+ import json
11
  from datetime import datetime, timedelta
12
+ from typing import Dict, List, Optional
13
+ import hashlib
14
+ import asyncio
15
+ from starlette.concurrency import run_in_threadpool
16
 
17
  # Configure logging
18
  logging.basicConfig(
 
30
  # Session configuration
31
  SESSION_DIR = "/tmp/instagram_sessions"
32
  SESSION_FILE = os.path.join(SESSION_DIR, f"session-{INSTAGRAM_USERNAME}") if INSTAGRAM_USERNAME else None
33
+ CACHE_DIR = "/tmp/instagram_cache"
34
 
35
+ # Create required directories
36
  os.makedirs(SESSION_DIR, exist_ok=True)
37
+ os.makedirs(CACHE_DIR, exist_ok=True)
38
 
39
+ # Rate limiting state
40
+ LAST_REQUEST_TIME = {}
41
+ COOLDOWN_PERIODS = {}
42
+ RATE_LIMIT_LOCK = asyncio.Lock()
43
+
44
+ # Cache state
45
+ STORY_CACHE: Dict[str, Dict] = {}
46
+ CACHE_EXPIRY: Dict[str, datetime] = {}
47
+ CACHE_LOCK = asyncio.Lock()
48
 
49
  USER_AGENTS = [
50
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
51
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15",
52
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
53
+ "Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1",
54
+ "Mozilla/5.0 (iPad; CPU OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1"
55
  ]
56
 
57
+ # Calculate cache key for a username
58
+ def get_cache_key(username: str) -> str:
59
+ return hashlib.md5(username.lower().encode()).hexdigest()
60
+
61
+ # Check if we have a valid cache for a username
62
+ async def get_cached_stories(username: str) -> Optional[Dict]:
63
+ cache_key = get_cache_key(username)
64
+
65
+ async with CACHE_LOCK:
66
+ if cache_key in STORY_CACHE and cache_key in CACHE_EXPIRY:
67
+ if datetime.now() < CACHE_EXPIRY[cache_key]:
68
+ logger.info(f"Cache hit for {username}")
69
+ return STORY_CACHE[cache_key]
70
+ else:
71
+ # Expired cache
72
+ logger.info(f"Cache expired for {username}")
73
+ STORY_CACHE.pop(cache_key, None)
74
+ CACHE_EXPIRY.pop(cache_key, None)
75
+
76
+ return None
77
+
78
+ # Save stories to cache
79
+ async def cache_stories(username: str, stories_data: Dict, ttl_minutes: int = 15):
80
+ cache_key = get_cache_key(username)
81
+
82
+ async with CACHE_LOCK:
83
+ STORY_CACHE[cache_key] = stories_data
84
+ CACHE_EXPIRY[cache_key] = datetime.now() + timedelta(minutes=ttl_minutes)
85
+
86
+ logger.info(f"Cached stories for {username} for {ttl_minutes} minutes")
87
+
88
+ # Save to disk for persistence
89
+ cache_file = os.path.join(CACHE_DIR, f"{cache_key}.json")
90
+ try:
91
+ with open(cache_file, 'w') as f:
92
+ json.dump({
93
+ "data": stories_data,
94
+ "expires": CACHE_EXPIRY[cache_key].isoformat()
95
+ }, f)
96
+ except Exception as e:
97
+ logger.warning(f"Failed to save cache to disk: {str(e)}")
98
+
99
+ def get_instaloader(force_login=False) -> instaloader.Instaloader:
100
  """Create and configure Instaloader instance with proper parameters"""
101
  L = instaloader.Instaloader(
102
  sleep=True,
 
120
  )
121
 
122
  try:
123
+ if not force_login and SESSION_FILE and os.path.exists(SESSION_FILE):
124
  logger.info(f"Attempting to load session from {SESSION_FILE}")
125
  try:
126
  L.load_session_from_file(INSTAGRAM_USERNAME, SESSION_FILE)
127
  logger.info("Session loaded successfully")
128
+ # Test session with a lightweight call if possible
129
  return L
130
  except Exception as e:
131
  logger.warning(f"Session load failed: {str(e)}, performing fresh login")
132
 
133
+ # Add delay before login to mimic human behavior
134
  time.sleep(random.uniform(1.0, 3.0))
135
 
136
  logger.info("Starting fresh login process")
137
  L.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
138
 
139
+ # Set realistic headers
140
+ L.context._session.headers.update({
141
+ 'Accept-Language': 'en-US,en;q=0.9',
142
+ 'Accept-Encoding': 'gzip, deflate, br',
143
+ 'Referer': 'https://www.instagram.com/',
144
+ 'Origin': 'https://www.instagram.com',
145
+ 'DNT': '1',
146
+ 'Connection': 'keep-alive',
147
+ 'Sec-Fetch-Dest': 'empty',
148
+ 'Sec-Fetch-Mode': 'cors',
149
+ 'Sec-Fetch-Site': 'same-origin',
150
+ 'Pragma': 'no-cache',
151
+ 'Cache-Control': 'no-cache',
152
+ })
153
+
154
  if SESSION_FILE:
155
  L.save_session_to_file(SESSION_FILE)
156
  logger.info(f"Saved new session to {SESSION_FILE}")
157
 
158
+ # Add delay after login
159
+ time.sleep(random.uniform(2.0, 4.0))
160
+
161
  return L
162
 
163
+ except instaloader.exceptions.BadCredentialsException as e:
164
+ logger.error("Invalid credentials: %s", str(e))
165
+ raise HTTPException(
166
+ status_code=status.HTTP_401_UNAUTHORIZED,
167
+ detail="Invalid Instagram credentials"
168
+ )
169
+ except instaloader.exceptions.TwoFactorAuthRequiredException as e:
170
+ logger.error("2FA required: %s", str(e))
171
+ raise HTTPException(
172
+ status_code=status.HTTP_401_UNAUTHORIZED,
173
+ detail="Two-factor authentication required"
174
+ )
175
  except Exception as e:
176
  logger.error("Login failed: %s", str(e))
177
  raise HTTPException(
 
179
  detail="Instagram login service unavailable"
180
  )
181
 
182
+ async def is_rate_limited(username: str) -> bool:
183
+ """Check if a given username is currently rate limited"""
184
+ async with RATE_LIMIT_LOCK:
185
+ # If in cooldown period, check if it's expired
186
+ if username in COOLDOWN_PERIODS:
187
+ if datetime.now() < COOLDOWN_PERIODS[username]:
188
+ remaining = (COOLDOWN_PERIODS[username] - datetime.now()).seconds
189
+ logger.warning(f"User {username} in cooldown for {remaining} more seconds")
190
+ return True
191
+ else:
192
+ # Cooldown expired
193
+ del COOLDOWN_PERIODS[username]
194
+
195
+ # Check time since last request
196
+ if username in LAST_REQUEST_TIME:
197
+ elapsed = (datetime.now() - LAST_REQUEST_TIME[username]).seconds
198
+ min_interval = 60 # Minimum 60 seconds between requests
199
+ if elapsed < min_interval:
200
+ logger.warning(f"Rate limiting {username}: {elapsed}s elapsed, need {min_interval}s")
201
+ return True
202
+
203
+ # Update last request time
204
+ LAST_REQUEST_TIME[username] = datetime.now()
205
+ return False
206
 
207
+ async def handle_rate_limit_error(username: str, retry_after: Optional[int] = None):
208
+ """Handle rate limit by setting cooldown period"""
209
+ async with RATE_LIMIT_LOCK:
210
+ # Set cooldown period based on retry_after or default logic
211
+ if retry_after:
212
+ # Use server-provided retry duration if available
213
+ cooldown_minutes = max(retry_after / 60, 1) # At least 1 minute
214
+ else:
215
+ # Default exponential backoff
216
+ cooldown_minutes = 5
217
+ if username in COOLDOWN_PERIODS:
218
+ cooldown_minutes = min(cooldown_minutes * 2, 30) # Up to 30 minutes
219
+
220
+ COOLDOWN_PERIODS[username] = datetime.now() + timedelta(minutes=cooldown_minutes)
221
+ logger.warning(f"Setting cooldown for {username} for {cooldown_minutes} minutes")
222
 
223
  def handle_instagram_errors(func):
224
  @wraps(func)
225
  async def wrapper(*args, **kwargs):
226
+ # Extract username from request path
227
+ request_username = ""
228
+ if len(args) > 1 and hasattr(args[1], '__str__'):
229
+ request_username = str(args[1])
230
+
231
  try:
232
+ # Check for rate limiting
233
+ if request_username and await is_rate_limited(request_username):
234
+ cached_response = await get_cached_stories(request_username)
235
+ if cached_response:
236
+ # Return cached response with cache note
237
+ logger.info(f"Returning cached response for rate-limited user {request_username}")
238
+ return {**cached_response, "from_cache": True}
239
+ else:
240
+ # No cache available
241
+ raise HTTPException(
242
+ status_code=status.HTTP_429_TOO_MANY_REQUESTS,
243
+ detail="Rate limit exceeded. Please try again later."
244
+ )
245
 
 
 
 
 
 
 
 
246
  response = await func(*args, **kwargs)
247
 
248
+ # Cache successful responses if they contain story data
249
  if request_username and isinstance(response, dict) and "data" in response:
250
+ await cache_stories(request_username, response)
251
 
252
  return response
253
 
 
255
  error_message = str(e)
256
  logger.error("Connection error: %s", error_message)
257
 
258
+ # Check for rate limit related messages
259
+ retry_after = None
260
  if "429 Too Many Requests" in error_message:
261
+ # Extract retry timer from error message if possible
 
262
  try:
263
  retry_text = error_message.split("retried in")[1].split("minutes")[0].strip()
264
+ retry_after = int(retry_text) * 60 # Convert to seconds
265
+ except (IndexError, ValueError):
266
+ retry_after = 1800 # Default 30 minutes
267
+
268
+ if request_username:
269
+ await handle_rate_limit_error(request_username, retry_after)
270
+
271
+ # Try to serve from cache if available
272
+ if request_username:
273
+ cached_response = await get_cached_stories(request_username)
274
+ if cached_response:
275
+ logger.info(f"Returning cached response for rate-limited request")
276
+ return {**cached_response, "from_cache": True}
277
 
278
  raise HTTPException(
279
  status_code=status.HTTP_429_TOO_MANY_REQUESTS,
280
+ detail=f"Instagram rate limit exceeded. Please try again in {retry_after//60} minutes."
281
  )
282
+ elif "401 Unauthorized" in error_message and "Please wait a few minutes" in error_message:
283
+ if request_username:
284
+ await handle_rate_limit_error(request_username)
285
+
286
+ # Try to serve from cache if available
287
+ if request_username:
288
+ cached_response = await get_cached_stories(request_username)
289
+ if cached_response:
290
+ logger.info(f"Returning cached response for rate-limited request")
291
+ return {**cached_response, "from_cache": True}
292
+
293
  raise HTTPException(
294
  status_code=status.HTTP_429_TOO_MANY_REQUESTS,
295
  detail="Instagram rate limit exceeded. Please try again later."
296
  )
297
+ elif "404 Not Found" in error_message:
298
+ raise HTTPException(
299
+ status_code=status.HTTP_404_NOT_FOUND,
300
+ detail="Profile not found or no stories available"
301
+ )
302
  else:
303
  raise HTTPException(
304
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
305
  detail="Instagram service unavailable"
306
  )
307
 
308
+ except instaloader.exceptions.QueryReturnedBadRequestException as e:
309
+ logger.error("API error 400: %s", str(e))
310
+ if request_username:
311
+ await handle_rate_limit_error(request_username)
312
+
313
+ # Try to serve from cache if available
314
+ if request_username:
315
+ cached_response = await get_cached_stories(request_username)
316
+ if cached_response:
317
+ logger.info(f"Returning cached response for rate-limited request")
318
+ return {**cached_response, "from_cache": True}
319
+
320
+ raise HTTPException(
321
+ status_code=status.HTTP_429_TOO_MANY_REQUESTS,
322
+ detail="Instagram rate limit exceeded"
323
+ )
324
+
325
+ except HTTPException:
326
+ # Re-raise HTTP exceptions without modification
327
+ raise
328
+
329
  except Exception as e:
330
  logger.error("Unexpected error: %s", str(e))
331
  raise HTTPException(
 
340
  """Retrieve stories with enhanced anti-detection measures"""
341
  logger.info(f"Processing request for @{username}")
342
 
343
+ # Check for cache first if requested
344
  if cached:
345
+ cached_response = await get_cached_stories(username)
346
  if cached_response:
347
  return {**cached_response, "from_cache": True}
 
 
348
 
349
  try:
350
+ # Run the Instagram operations in a thread pool to not block the event loop
351
+ async def fetch_stories():
352
+ # Get loader with session
353
+ L = get_instaloader()
354
+ logger.info("Instaloader instance configured")
355
+
356
+ # Randomized delay before profile request (more natural)
357
+ delay = random.uniform(1.5, 3.0)
358
+ logger.debug(f"Applying initial delay of {delay:.2f}s")
359
+ time.sleep(delay)
360
+
361
+ # Profile lookup with retry
362
+ profile = None
363
+ for attempt in range(3):
364
+ try:
365
+ logger.info(f"Fetching profile (attempt {attempt+1}/3)")
366
+ profile = instaloader.Profile.from_username(L.context, username)
367
+ break
368
+ except instaloader.exceptions.QueryReturnedBadRequestException:
369
+ wait_time = (attempt + 1) * random.uniform(3.0, 5.0)
370
+ logger.warning(f"Rate limited, waiting {wait_time:.2f}s")
371
+ time.sleep(wait_time)
372
+ except instaloader.exceptions.ConnectionException as e:
373
+ if "401 Unauthorized" in str(e) and "Please wait a few minutes" in str(e):
374
+ # Try with a fresh login if session might be expired
375
+ if attempt < 2: # Only try this once
376
+ logger.warning("Session may be expired, trying with fresh login")
377
+ L = get_instaloader(force_login=True)
378
+ time.sleep(random.uniform(4.0, 6.0))
379
+ continue
380
+ raise
381
+
382
+ if profile is None:
383
+ raise HTTPException(
384
+ status.HTTP_429_TOO_MANY_REQUESTS,
385
+ "Too many attempts to access Instagram"
386
+ )
387
 
388
+ logger.info(f"Access check for @{username}")
389
+ if not profile.has_viewable_story:
390
+ logger.warning("No viewable story")
391
+ raise HTTPException(
392
+ status.HTTP_404_NOT_FOUND,
393
+ "No accessible stories for this profile"
394
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
 
396
+ # Additional delay before story fetch
397
+ time.sleep(random.uniform(1.0, 2.5))
398
+
399
+ logger.info("Fetching stories")
400
+ stories = []
401
+ try:
402
+ for story in L.get_stories(userids=[profile.userid]):
403
+ for item in story.get_items():
404
+ # Create a story dict with safe attribute access
405
+ story_data = {
406
+ "id": str(item.mediaid),
407
+ "url": item.url,
408
+ "type": "video" if item.is_video else "image",
409
+ "timestamp": item.date_utc.isoformat(),
410
+ }
411
+
412
+ # Add any available metadata safely
413
+ if hasattr(item, 'owner_username'):
414
+ story_data["username"] = item.owner_username
415
+
416
+ # Only try to add view_count if it's a video
417
+ if item.is_video:
418
+ try:
419
+ if hasattr(item, 'view_count'):
420
+ story_data["views"] = item.view_count
421
+ except AttributeError:
422
+ pass
423
+
424
+ stories.append(story_data)
425
+
426
+ except instaloader.exceptions.QueryReturnedNotFoundException:
427
+ logger.error("Stories not found")
428
+ raise HTTPException(
429
+ status.HTTP_404_NOT_FOUND,
430
+ "Stories not available"
431
+ )
432
 
433
+ if not stories:
434
+ logger.info("No active stories found")
435
+ raise HTTPException(
436
+ status.HTTP_404_NOT_FOUND,
437
+ "No active stories available"
438
+ )
439
+
440
+ # Queue session save in background
441
+ background_tasks.add_task(lambda: L.save_session_to_file(SESSION_FILE) if SESSION_FILE else None)
442
+
443
+ # Final randomized delay
444
+ time.sleep(random.uniform(0.3, 0.7))
445
+
446
+ logger.info(f"Returning {len(stories)} stories")
447
+ return {
448
+ "data": stories,
449
+ "count": len(stories),
450
+ "username": username,
451
+ "fetched_at": datetime.now().isoformat()
452
+ }
453
 
454
+ # Execute in thread pool to not block the event loop
455
+ return await run_in_threadpool(fetch_stories)
456
 
457
  except Exception as e:
458
  logger.error(f"Critical failure: {str(e)}")
 
465
  logger.info(f"Download request for: {url}")
466
 
467
  try:
468
+ # Basic validation to prevent arbitrary URL access
469
  if not url.startswith(("https://instagram", "https://scontent")):
470
  raise HTTPException(
471
  status_code=status.HTTP_400_BAD_REQUEST,
472
  detail="Invalid URL format"
473
  )
474
 
475
+ # Random delay to avoid detection patterns
476
+ time.sleep(random.uniform(0.5, 1.5))
477
 
478
+ # Configure headers to mimic a browser
479
  headers = {
480
  "User-Agent": random.choice(USER_AGENTS),
481
  "Accept": "*/*",
482
+ "Accept-Language": "en-US,en;q=0.9",
483
+ "Accept-Encoding": "gzip, deflate, br",
484
+ "Referer": "https://www.instagram.com/",
485
+ "Origin": "https://www.instagram.com",
486
+ "Sec-Fetch-Dest": "empty",
487
+ "Sec-Fetch-Mode": "cors",
488
+ "Sec-Fetch-Site": "cross-site",
489
+ "Pragma": "no-cache",
490
+ "Cache-Control": "no-cache",
491
  }
492
 
493
+ # Request the media with a session
494
+ session = requests.Session()
495
+ response = session.get(url, headers=headers, stream=True, timeout=10)
496
  response.raise_for_status()
497
 
498
+ # Determine content type from response or URL
499
  if "Content-Type" in response.headers:
500
  content_type = response.headers["Content-Type"]
501
  elif url.endswith((".jpg", ".jpeg")):
 
511
  return StreamingResponse(
512
  response.iter_content(chunk_size=8192),
513
  media_type=content_type,
514
+ headers={
515
+ "Content-Disposition": f"attachment; filename={url.split('/')[-1]}",
516
+ "Cache-Control": "no-cache, no-store, must-revalidate",
517
+ "Pragma": "no-cache",
518
+ "Expires": "0",
519
+ }
520
  )
521
 
522
+ except requests.exceptions.RequestException as e:
523
  logger.error(f"Download failed: {str(e)}")
524
  raise HTTPException(
525
  status_code=status.HTTP_502_BAD_GATEWAY,
 
529
  # Add a health check endpoint
530
  @app.get("/health")
531
  async def health_check():
532
+ return {"status": "ok", "timestamp": datetime.now().isoformat()}
533
+
534
+ # Add middleware to handle rate limit headers
535
+ @app.middleware("http")
536
+ async def add_rate_limit_headers(request: Request, call_next):
537
+ response = await call_next(request)
538
+
539
+ # Add custom headers for rate limiting info
540
+ username = request.path_params.get("username", None)
541
+ if username and username in COOLDOWN_PERIODS:
542
+ remaining = max(0, int((COOLDOWN_PERIODS[username] - datetime.now()).total_seconds()))
543
+ response.headers["X-RateLimit-Reset"] = str(remaining)
544
+ response.headers["X-RateLimit-Remaining"] = "0" if remaining > 0 else "1"
545
+
546
+ return response
547
+
548
+ # Startup event to load cache from disk
549
+ @app.on_event("startup")
550
+ async def load_cache_from_disk():
551
+ try:
552
+ for filename in os.listdir(CACHE_DIR):
553
+ if filename.endswith('.json'):
554
+ file_path = os.path.join(CACHE_DIR, filename)
555
+ try:
556
+ with open(file_path, 'r') as f:
557
+ cache_data = json.load(f)
558
+
559
+ if "data" in cache_data and "expires" in cache_data:
560
+ expire_time = datetime.fromisoformat(cache_data["expires"])
561
+ if expire_time > datetime.now():
562
+ cache_key = filename[:-5] # Remove .json
563
+ async with CACHE_LOCK:
564
+ STORY_CACHE[cache_key] = cache_data["data"]
565
+ CACHE_EXPIRY[cache_key] = expire_time
566
+ except Exception as e:
567
+ logger.warning(f"Failed to load cache file {filename}: {str(e)}")
568
+
569
+ logger.info(f"Loaded {len(STORY_CACHE)} items from cache")
570
+ except Exception as e:
571
+ logger.error(f"Error loading cache: {str(e)}")