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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +207 -385
app.py CHANGED
@@ -1,5 +1,5 @@
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,12 +7,8 @@ import time
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(
@@ -36,68 +32,83 @@ CACHE_DIR = "/tmp/instagram_cache"
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,
103
  request_timeout=30,
@@ -120,382 +131,211 @@ def get_instaloader(force_login=False) -> instaloader.Instaloader:
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(
178
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
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
 
254
  except instaloader.exceptions.ConnectionException as e:
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(
332
- status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
333
- detail="Internal server error"
334
- )
335
  return wrapper
336
 
337
  @app.get("/stories/{username}")
338
  @handle_instagram_errors
339
  async def get_stories(username: str, background_tasks: BackgroundTasks, cached: bool = False):
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)}")
459
  raise
460
 
461
  @app.get("/download/{url:path}")
462
  @handle_instagram_errors
463
  async def download_media(url: str):
464
  """Download and proxy media content"""
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")):
@@ -505,67 +345,49 @@ async def download_media(url: str):
505
  else:
506
  content_type = "application/octet-stream"
507
 
508
- logger.info(f"Media downloaded successfully: {content_type}")
509
 
510
- # Stream the response back
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,
526
  detail="Failed to download media"
527
  )
528
 
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)}")
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, status, BackgroundTasks
2
+ from fastapi.responses import StreamingResponse
3
  import instaloader
4
  import requests
5
  import os
 
7
  import logging
8
  import random
9
  from functools import wraps
 
10
  from datetime import datetime, timedelta
11
+ import json
 
 
 
12
 
13
  # Configure logging
14
  logging.basicConfig(
 
32
  os.makedirs(SESSION_DIR, exist_ok=True)
33
  os.makedirs(CACHE_DIR, exist_ok=True)
34
 
35
+ # Simple memory cache
36
+ STORY_CACHE = {}
37
+ CACHE_EXPIRY = {}
38
+ LAST_REQUEST = {}
39
+ COOLDOWN_UNTIL = {}
 
 
 
 
40
 
41
  USER_AGENTS = [
42
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
43
  "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",
44
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
 
 
45
  ]
46
 
47
+ def get_cache_key(username):
48
+ """Simple cache key generator"""
49
+ return username.lower()
50
 
51
+ def get_cached_stories(username):
52
+ """Get stories from cache if available"""
53
+ key = get_cache_key(username)
54
+ if key in STORY_CACHE and key in CACHE_EXPIRY:
55
+ if datetime.now() < CACHE_EXPIRY[key]:
56
+ logger.info(f"Cache hit for {username}")
57
+ return STORY_CACHE[key]
58
+ else:
59
+ # Clean expired cache
60
+ STORY_CACHE.pop(key, None)
61
+ CACHE_EXPIRY.pop(key, None)
 
 
 
 
62
  return None
63
 
64
+ def save_to_cache(username, data, minutes=30):
65
+ """Save stories to cache"""
66
+ key = get_cache_key(username)
67
+ STORY_CACHE[key] = data
68
+ CACHE_EXPIRY[key] = datetime.now() + timedelta(minutes=minutes)
69
 
70
+ # Save to disk
 
 
 
 
 
 
 
71
  try:
72
+ cache_file = os.path.join(CACHE_DIR, f"{key}.json")
73
  with open(cache_file, 'w') as f:
74
  json.dump({
75
+ "data": data,
76
+ "expires": CACHE_EXPIRY[key].isoformat()
77
  }, f)
78
+ logger.info(f"Cached {username} stories for {minutes} minutes")
79
  except Exception as e:
80
+ logger.warning(f"Failed to save cache: {str(e)}")
81
+
82
+ def is_rate_limited(username):
83
+ """Check if we should rate limit this request"""
84
+ # Check if in cooldown
85
+ if username in COOLDOWN_UNTIL:
86
+ if datetime.now() < COOLDOWN_UNTIL[username]:
87
+ seconds_left = (COOLDOWN_UNTIL[username] - datetime.now()).total_seconds()
88
+ logger.warning(f"{username} in cooldown for {int(seconds_left)}s")
89
+ return True
90
+ else:
91
+ # Cooldown expired
92
+ COOLDOWN_UNTIL.pop(username)
93
+
94
+ # Check request frequency
95
+ if username in LAST_REQUEST:
96
+ seconds_since = (datetime.now() - LAST_REQUEST[username]).total_seconds()
97
+ if seconds_since < 60: # 1 minute minimum between requests
98
+ logger.warning(f"Rate limiting {username}: only {int(seconds_since)}s since last request")
99
+ return True
100
+
101
+ # Update last request time
102
+ LAST_REQUEST[username] = datetime.now()
103
+ return False
104
 
105
+ def set_cooldown(username, minutes=30):
106
+ """Set a cooldown period for a username"""
107
+ COOLDOWN_UNTIL[username] = datetime.now() + timedelta(minutes=minutes)
108
+ logger.warning(f"Setting {minutes}m cooldown for {username}")
109
+
110
+ def get_instaloader():
111
+ """Create and configure Instaloader instance"""
112
  L = instaloader.Instaloader(
113
  sleep=True,
114
  request_timeout=30,
 
131
  )
132
 
133
  try:
134
+ if SESSION_FILE and os.path.exists(SESSION_FILE):
135
+ logger.info(f"Loading session from {SESSION_FILE}")
136
  try:
137
  L.load_session_from_file(INSTAGRAM_USERNAME, SESSION_FILE)
138
  logger.info("Session loaded successfully")
 
139
  return L
140
  except Exception as e:
141
+ logger.warning(f"Session load failed: {str(e)}")
 
 
 
142
 
143
+ logger.info("Starting fresh login")
144
+ time.sleep(random.uniform(1.0, 2.0)) # Small delay before login
145
  L.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  if SESSION_FILE:
148
  L.save_session_to_file(SESSION_FILE)
149
  logger.info(f"Saved new session to {SESSION_FILE}")
150
 
 
 
 
151
  return L
152
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  except Exception as e:
154
+ logger.error(f"Login failed: {str(e)}")
155
  raise HTTPException(
156
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
157
+ detail="Instagram login failed"
158
  )
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  def handle_instagram_errors(func):
161
  @wraps(func)
162
  async def wrapper(*args, **kwargs):
163
+ # Extract username if present
164
+ username = ""
165
  if len(args) > 1 and hasattr(args[1], '__str__'):
166
+ username = str(args[1])
167
 
168
  try:
169
  # Check for rate limiting
170
+ if username and is_rate_limited(username):
171
+ # Try to serve from cache
172
+ cached = get_cached_stories(username)
173
+ if cached:
174
+ return {**cached, "from_cache": True}
 
175
  else:
 
176
  raise HTTPException(
177
  status_code=status.HTTP_429_TOO_MANY_REQUESTS,
178
  detail="Rate limit exceeded. Please try again later."
179
  )
180
+
181
+ # Execute the handler
182
  response = await func(*args, **kwargs)
183
 
184
+ # Cache successful responses
185
+ if username and isinstance(response, dict) and "data" in response:
186
+ save_to_cache(username, response)
187
 
188
  return response
189
 
190
  except instaloader.exceptions.ConnectionException as e:
191
  error_message = str(e)
192
+ logger.error(f"Connection error: {error_message}")
193
 
194
+ # Handle rate limiting errors
 
195
  if "429 Too Many Requests" in error_message:
196
+ retry_mins = 30 # Default
197
  try:
198
+ # Try to extract minutes from error message
199
+ if "retried in" in error_message and "minutes" in error_message:
200
+ retry_text = error_message.split("retried in")[1].split("minutes")[0].strip()
201
+ retry_mins = int(retry_text)
202
+ except:
203
+ pass
 
 
 
 
 
 
 
 
204
 
205
+ if username:
206
+ set_cooldown(username, retry_mins)
 
 
 
 
 
207
 
208
+ # Try to serve from cache
209
+ if username:
210
+ cached = get_cached_stories(username)
211
+ if cached:
212
+ return {**cached, "from_cache": True}
213
+
 
214
  raise HTTPException(
215
  status_code=status.HTTP_429_TOO_MANY_REQUESTS,
216
+ detail=f"Instagram rate limit exceeded. Please try again in {retry_mins} minutes."
217
  )
218
+
219
  elif "404 Not Found" in error_message:
220
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
221
+ detail="Profile or stories not found")
 
 
222
  else:
223
+ raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
224
+ detail="Instagram service unavailable")
 
 
 
 
 
 
 
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  except Exception as e:
227
+ logger.error(f"Error: {str(e)}")
228
+ raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
229
+ detail="Internal server error")
 
 
230
  return wrapper
231
 
232
  @app.get("/stories/{username}")
233
  @handle_instagram_errors
234
  async def get_stories(username: str, background_tasks: BackgroundTasks, cached: bool = False):
235
+ """Retrieve Instagram stories for a user"""
236
+ logger.info(f"Request for @{username} stories")
237
 
238
+ # Return from cache if requested
239
  if cached:
240
+ cached_result = get_cached_stories(username)
241
+ if cached_result:
242
+ return {**cached_result, "from_cache": True}
243
 
244
  try:
245
+ # Get Instagram loader
246
+ L = get_instaloader()
247
+ logger.info("Instagram loader ready")
248
+
249
+ # Small delay
250
+ time.sleep(random.uniform(1.0, 2.0))
251
+
252
+ # Get profile
253
+ logger.info(f"Fetching profile for {username}")
254
+ try:
255
+ profile = instaloader.Profile.from_username(L.context, username)
256
+ except Exception as e:
257
+ logger.error(f"Profile fetch error: {str(e)}")
258
+ raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
 
260
+ # Check story access
261
+ if not profile.has_viewable_story:
262
+ logger.warning("No viewable story")
263
+ raise HTTPException(
264
+ status_code=status.HTTP_404_NOT_FOUND,
265
+ detail="No accessible stories for this user"
266
+ )
 
 
 
 
 
 
 
 
 
267
 
268
+ # Get stories
269
+ logger.info("Fetching stories")
270
+ stories = []
271
+ for story in L.get_stories(userids=[profile.userid]):
272
+ for item in story.get_items():
273
+ story_data = {
274
+ "id": str(item.mediaid),
275
+ "url": item.url,
276
+ "type": "video" if item.is_video else "image",
277
+ "timestamp": item.date_utc.isoformat()
278
+ }
279
+
280
+ # Safe attribute access
281
+ if item.is_video and hasattr(item, 'view_count'):
282
+ try:
283
+ story_data["views"] = item.view_count
284
+ except:
285
+ pass
 
 
 
 
 
 
 
 
286
 
287
+ stories.append(story_data)
288
+
289
+ if not stories:
290
+ logger.info("No stories found")
291
+ raise HTTPException(
292
+ status_code=status.HTTP_404_NOT_FOUND,
293
+ detail="No active stories available"
294
+ )
 
 
 
 
 
 
 
 
 
 
 
295
 
296
+ # Save session in background to not delay response
297
+ background_tasks.add_task(lambda: L.save_session_to_file(SESSION_FILE) if SESSION_FILE else None)
298
+
299
+ logger.info(f"Returning {len(stories)} stories")
300
+ result = {
301
+ "data": stories,
302
+ "count": len(stories),
303
+ "username": username,
304
+ "fetched_at": datetime.now().isoformat()
305
+ }
306
+
307
+ return result
308
 
 
 
 
309
  except Exception as e:
310
+ logger.error(f"Error: {str(e)}")
311
  raise
312
 
313
  @app.get("/download/{url:path}")
314
  @handle_instagram_errors
315
  async def download_media(url: str):
316
  """Download and proxy media content"""
317
+ logger.info(f"Download request for URL")
318
 
319
  try:
320
+ # Validate URL
321
  if not url.startswith(("https://instagram", "https://scontent")):
322
  raise HTTPException(
323
  status_code=status.HTTP_400_BAD_REQUEST,
324
  detail="Invalid URL format"
325
  )
326
 
327
+ # Configure request
 
 
 
328
  headers = {
329
  "User-Agent": random.choice(USER_AGENTS),
 
 
 
330
  "Referer": "https://www.instagram.com/",
331
+ "Accept": "*/*",
 
 
 
 
 
332
  }
333
 
334
+ # Get media
335
+ response = requests.get(url, headers=headers, stream=True, timeout=15)
 
336
  response.raise_for_status()
337
 
338
+ # Determine content type
339
  if "Content-Type" in response.headers:
340
  content_type = response.headers["Content-Type"]
341
  elif url.endswith((".jpg", ".jpeg")):
 
345
  else:
346
  content_type = "application/octet-stream"
347
 
348
+ logger.info(f"Media downloaded: {content_type}")
349
 
350
+ # Return the media
351
  return StreamingResponse(
352
  response.iter_content(chunk_size=8192),
353
  media_type=content_type,
354
+ headers={"Content-Disposition": f"attachment; filename={url.split('/')[-1]}"}
 
 
 
 
 
355
  )
356
 
357
+ except Exception as e:
358
  logger.error(f"Download failed: {str(e)}")
359
  raise HTTPException(
360
  status_code=status.HTTP_502_BAD_GATEWAY,
361
  detail="Failed to download media"
362
  )
363
 
364
+ # Load cache from disk at startup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365
  @app.on_event("startup")
366
+ def load_cache_from_disk():
367
  try:
368
+ count = 0
369
  for filename in os.listdir(CACHE_DIR):
370
  if filename.endswith('.json'):
 
371
  try:
372
+ with open(os.path.join(CACHE_DIR, filename), 'r') as f:
373
  cache_data = json.load(f)
374
+
375
  if "data" in cache_data and "expires" in cache_data:
376
+ # Convert ISO string to datetime
377
+ expires = datetime.fromisoformat(cache_data["expires"])
378
+ if expires > datetime.now():
379
+ key = filename.replace('.json', '')
380
+ STORY_CACHE[key] = cache_data["data"]
381
+ CACHE_EXPIRY[key] = expires
382
+ count += 1
383
  except Exception as e:
384
+ logger.warning(f"Couldn't load cache file {filename}: {e}")
385
 
386
+ logger.info(f"Loaded {count} items from cache")
387
  except Exception as e:
388
+ logger.error(f"Cache loading error: {e}")
389
+
390
+ # Health check endpoint
391
+ @app.get("/health")
392
+ async def health_check():
393
+ return {"status": "ok", "timestamp": datetime.now().isoformat()}