File size: 27,564 Bytes
1f8ead3 226ea29 0efafe8 a1b5d59 887ec44 200c993 a1b5d59 59e1f2d a1b5d59 226ea29 59e1f2d 226ea29 d27a73d 887ec44 7a1252c 887ec44 7a1252c 449efc6 887ec44 7a1252c 887ec44 7a1252c 887ec44 7a1252c 887ec44 7a1252c 887ec44 59e1f2d 1b86650 59e1f2d 226ea29 59e1f2d 887ec44 59e1f2d 887ec44 7a1252c d27a73d d0e1f2a 59e1f2d 449efc6 59e1f2d d0e1f2a 59e1f2d d0e1f2a 59e1f2d d0e1f2a 59e1f2d 226ea29 d0e1f2a 59e1f2d d0e1f2a 59e1f2d 449efc6 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 226ea29 d0e1f2a 59e1f2d 449efc6 59e1f2d 118a06a 59e1f2d 449efc6 59e1f2d 226ea29 118a06a 226ea29 d0e1f2a 226ea29 d27a73d 59e1f2d d0e1f2a 59e1f2d 118a06a 449efc6 59e1f2d d0e1f2a 449efc6 59e1f2d 226ea29 d0e1f2a 449efc6 59e1f2d 449efc6 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 449efc6 59e1f2d 226ea29 59e1f2d d0e1f2a 59e1f2d d0e1f2a 226ea29 59e1f2d d0e1f2a 59e1f2d 226ea29 59e1f2d 449efc6 59e1f2d 226ea29 59e1f2d 449efc6 59e1f2d 449efc6 59e1f2d d0e1f2a 59e1f2d 449efc6 d0e1f2a 59e1f2d d0e1f2a 226ea29 59e1f2d 449efc6 59e1f2d 226ea29 59e1f2d 449efc6 d0e1f2a 449efc6 59e1f2d 449efc6 d0e1f2a 59e1f2d d27a73d b8bcfff 59e1f2d 226ea29 d27a73d 226ea29 d27a73d 59e1f2d 226ea29 59e1f2d 226ea29 d27a73d 226ea29 d27a73d 226ea29 d27a73d 226ea29 d27a73d 226ea29 d27a73d 59e1f2d 226ea29 d27a73d 59e1f2d 226ea29 59e1f2d d27a73d 59e1f2d 226ea29 d27a73d 226ea29 d27a73d 226ea29 d27a73d 226ea29 d27a73d 0efafe8 25ad852 1b86650 cdc2f62 2f953e3 25ad852 1b86650 59e1f2d 1b86650 449efc6 59e1f2d 449efc6 1b86650 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 226ea29 59e1f2d 1b86650 d0e1f2a 59e1f2d 1b86650 59e1f2d 1b86650 59e1f2d 226ea29 59e1f2d 1b86650 0efafe8 59e1f2d 0efafe8 59e1f2d 0efafe8 59e1f2d 0efafe8 59e1f2d 0efafe8 1b86650 0efafe8 887ec44 0efafe8 887ec44 0efafe8 887ec44 0efafe8 36e6458 d27a73d 59e1f2d 7a1252c 59e1f2d 7a1252c 887ec44 7a1252c 887ec44 7a1252c 887ec44 7a1252c 887ec44 7a1252c 887ec44 7a1252c 887ec44 7a1252c 887ec44 59e1f2d d27a73d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
from fastapi import FastAPI, Request, HTTPException, status, Form
from fastapi.responses import StreamingResponse, HTMLResponse, RedirectResponse
import requests
import os
import time
import logging
import random
import json
from datetime import datetime, timedelta
from collections import defaultdict
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
app = FastAPI(title="Instagram Stories API")
# Configuration
CACHE_DIR = "/tmp/instagram_cache"
COOKIES_FILE = "/tmp/instagram_cookies.json"
os.makedirs(CACHE_DIR, exist_ok=True)
# Instagram session state
IG_SESSION = None
IG_SESSION_USERNAME = None
IG_SESSION_EXPIRY = None
STORY_CACHE = {}
CACHE_EXPIRY = {}
LAST_REQUEST = {}
# User agents
USER_AGENTS = [
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
# Functions for cache management
def get_cache_key(username):
return username.lower()
def get_cached_stories(username):
key = get_cache_key(username)
if key in STORY_CACHE and key in CACHE_EXPIRY:
if datetime.now() < CACHE_EXPIRY[key]:
logger.info(f"Cache hit for {username}")
return STORY_CACHE[key]
else:
STORY_CACHE.pop(key, None)
CACHE_EXPIRY.pop(key, None)
return None
def save_to_cache(username, data, minutes=30):
key = get_cache_key(username)
STORY_CACHE[key] = data
CACHE_EXPIRY[key] = datetime.now() + timedelta(minutes=minutes)
try:
cache_file = os.path.join(CACHE_DIR, f"{key}.json")
with open(cache_file, 'w') as f:
json.dump({
"data": data,
"expires": CACHE_EXPIRY[key].isoformat()
}, f)
logger.info(f"Cached {username} stories for {minutes} minutes")
except Exception as e:
logger.warning(f"Failed to save cache: {str(e)}")
def should_rate_limit_request(username):
key = username.lower()
now = datetime.now()
if key in LAST_REQUEST:
seconds_since = (now - LAST_REQUEST[key]).total_seconds()
if seconds_since < 60:
logger.warning(f"Rate limiting {username}: {seconds_since:.1f}s since last request")
return True
LAST_REQUEST[key] = now
return False
# Session management functions
def get_instagram_session():
global IG_SESSION, IG_SESSION_EXPIRY
if IG_SESSION is None or IG_SESSION_EXPIRY is None or datetime.now() > IG_SESSION_EXPIRY:
if os.path.exists(COOKIES_FILE):
try:
with open(COOKIES_FILE, 'r') as f:
data = json.load(f)
if 'expires' in data and datetime.fromisoformat(data['expires']) > datetime.now():
IG_SESSION = requests.Session()
for cookie in data['cookies']:
IG_SESSION.cookies.set(
cookie['name'],
cookie['value'],
domain=cookie.get('domain', '.instagram.com')
)
IG_SESSION_EXPIRY = datetime.fromisoformat(data['expires'])
global IG_SESSION_USERNAME
IG_SESSION_USERNAME = data.get('username', 'unknown')
logger.info(f"Loaded Instagram session for {IG_SESSION_USERNAME}")
return IG_SESSION
else:
logger.warning("Saved session expired, needs new import")
except Exception as e:
logger.error(f"Failed to load cookies: {str(e)}")
if IG_SESSION is not None and IG_SESSION_EXPIRY is not None and datetime.now() < IG_SESSION_EXPIRY:
return IG_SESSION
return None
def save_instagram_session(cookies, username, days=30):
"""Save imported cookie data"""
global IG_SESSION, IG_SESSION_EXPIRY, IG_SESSION_USERNAME
IG_SESSION = requests.Session()
IG_SESSION_EXPIRY = datetime.now() + timedelta(days=days)
IG_SESSION_USERNAME = username
for cookie in cookies:
IG_SESSION.cookies.set(
cookie['name'],
cookie['value'],
domain=cookie.get('domain', '.instagram.com')
)
try:
with open(COOKIES_FILE, 'w') as f:
json.dump({
'cookies': cookies,
'expires': IG_SESSION_EXPIRY.isoformat(),
'username': username
}, f)
logger.info(f"Saved Instagram session for {username}")
return True
except Exception as e:
logger.error(f"Failed to save cookies: {str(e)}")
return False
def clear_instagram_session():
global IG_SESSION, IG_SESSION_EXPIRY, IG_SESSION_USERNAME
IG_SESSION = None
IG_SESSION_EXPIRY = None
IG_SESSION_USERNAME = None
if os.path.exists(COOKIES_FILE):
try:
os.remove(COOKIES_FILE)
logger.info("Removed Instagram session cookies")
except Exception as e:
logger.error(f"Failed to remove cookies file: {str(e)}")
def parse_cookie_string(cookie_str):
"""Parse cookie string into cookie objects"""
cookies = []
for cookie_part in cookie_str.split(';'):
cookie_part = cookie_part.strip()
if not cookie_part:
continue
if '=' in cookie_part:
name, value = cookie_part.split('=', 1)
cookies.append({
'name': name.strip(),
'value': value.strip(),
'domain': '.instagram.com'
})
return cookies
# Instagram API functions
def get_instagram_stories(username):
session = get_instagram_session()
if not session:
logger.warning("No valid Instagram session")
return None
try:
# First get the user ID
profile_url = f"https://www.instagram.com/api/v1/users/web_profile_info/?username={username}"
session.headers.update({
'User-Agent': random.choice(USER_AGENTS),
'X-IG-App-ID': '936619743392459',
'Referer': f'https://www.instagram.com/{username}/',
})
profile_response = session.get(profile_url)
if profile_response.status_code != 200:
logger.warning(f"Failed to get user profile: {profile_response.status_code}")
try:
resp_data = profile_response.json()
if 'message' in resp_data:
return {"error": "api_error", "message": resp_data['message']}
except:
pass
return {"error": "profile_error", "status": profile_response.status_code}
profile_data = profile_response.json()
if 'data' not in profile_data or 'user' not in profile_data['data']:
return {"error": "not_found", "message": "User profile not found"}
user_id = profile_data['data']['user']['id']
time.sleep(random.uniform(1, 2))
# Get the stories
stories_url = f"https://www.instagram.com/api/v1/feed/user/{user_id}/story/"
stories_response = session.get(stories_url)
if stories_response.status_code != 200:
return {"error": "stories_error", "status": stories_response.status_code}
stories_data = stories_response.json()
if 'reel' not in stories_data or not stories_data['reel'].get('items'):
return {"error": "no_stories", "message": "No stories found"}
stories = []
for item in stories_data['reel']['items']:
story = {
"id": item.get('pk', ''),
"type": "video" if item.get('media_type') == 2 else "image",
"timestamp": datetime.fromtimestamp(item.get('taken_at', 0)).isoformat()
}
# Get media URL
if story["type"] == "video" and 'video_versions' in item and item['video_versions']:
story["url"] = item['video_versions'][0]['url']
if 'view_count' in item:
story["views"] = item['view_count']
elif 'image_versions2' in item and 'candidates' in item['image_versions2'] and item['image_versions2']['candidates']:
story["url"] = item['image_versions2']['candidates'][0]['url']
else:
continue
stories.append(story)
if not stories:
return {"error": "no_valid_stories", "message": "No valid stories found"}
result = {
"data": stories,
"count": len(stories),
"username": username,
"fetched_at": datetime.now().isoformat()
}
return result
except Exception as e:
logger.error(f"Error getting stories: {str(e)}")
return {"error": "exception", "message": str(e)}
# Routes
#@app.get("/", response_class=HTMLResponse)
#async def root():
# return RedirectResponse(url="/status")
@app.get("/import", response_class=HTMLResponse)
async def import_page(message: str = None, success: bool = False):
status_class = "success" if success else "error"
status_div = f'<div class="status {status_class}">{message}</div>' if message else ""
html = """
<!DOCTYPE html>
<html>
<head>
<title>Import Instagram Session</title>
<style>
body { font-family: Arial; max-width: 700px; margin: 0 auto; padding: 20px; }
.container { background-color: #fff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 25px; }
h1 { color: #262626; text-align: center; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 10px; }
textarea { width: 100%; padding: 10px; border: 1px solid #dbdbdb; border-radius: 3px; height: 150px; }
input[type="text"] { width: 100%; padding: 10px; border: 1px solid #dbdbdb; border-radius: 3px; }
.btn { background-color: #0095f6; border: none; color: white; padding: 10px 15px; border-radius: 4px; cursor: pointer; font-weight: bold; width: 100%; }
.status { margin-top: 20px; padding: 10px; border-radius: 4px; }
.success { background-color: #e8f5e9; color: #388e3c; }
.error { background-color: #ffebee; color: #d32f2f; }
.instructions { background-color: #f8f9fa; padding: 15px; border-radius: 4px; margin-bottom: 20px; }
.code { font-family: monospace; background-color: #f1f1f1; padding: 2px 5px; border-radius: 3px; }
ol { padding-left: 20px; }
li { margin-bottom: 10px; }
</style>
</head>
<body>
<div class="container">
<h1>Import Instagram Session</h1>
<div class="instructions">
<h3>How to get your Instagram cookies:</h3>
<ol>
<li>Log in to Instagram in your browser</li>
<li>Open browser DevTools (F12 or right-click and select "Inspect")</li>
<li>Go to the "Application" or "Storage" tab</li>
<li>Under "Cookies", find "instagram.com"</li>
<li>Find the <span class="code">sessionid</span> cookie and copy its value</li>
<li>Also copy the <span class="code">ds_user_id</span> and <span class="code">csrftoken</span> cookies if available</li>
<li>Paste the cookies in the format: <span class="code">name=value; name2=value2;</span></li>
</ol>
</div>
""" + status_div + """
<form method="post" action="/import-cookies">
<div class="form-group">
<label for="cookies">Paste your Instagram cookies here:</label>
<textarea id="cookies" name="cookies" placeholder="sessionid=YOUR_SESSION_ID; ds_user_id=YOUR_USER_ID; csrftoken=YOUR_CSRF_TOKEN;" required></textarea>
</div>
<div class="form-group">
<label for="username">Your Instagram username:</label>
<input type="text" id="username" name="username" required>
</div>
<button type="submit" class="btn">Import Session</button>
</form>
</div>
</body>
</html>
"""
return HTMLResponse(content=html)
@app.post("/import-cookies")
async def import_cookies(cookies: str = Form(...), username: str = Form(...)):
logger.info(f"Importing cookies for {username}")
try:
cookie_objects = parse_cookie_string(cookies)
if not cookie_objects:
logger.warning("No valid cookies found in input")
return import_page(message="No valid cookies found in input")
# Check for essential cookies
has_sessionid = any(c['name'] == 'sessionid' for c in cookie_objects)
if not has_sessionid:
logger.warning("Missing essential sessionid cookie")
return import_page(message="Missing essential sessionid cookie")
# Save session
if save_instagram_session(cookie_objects, username):
# Verify session
session = get_instagram_session()
if session:
logger.info("Session imported successfully")
return RedirectResponse(url="/status", status_code=303)
else:
logger.error("Failed to get session after saving")
return import_page(message="Failed to create session")
else:
logger.error("Failed to save session")
return import_page(message="Failed to save session")
except Exception as e:
logger.error(f"Import error: {str(e)}")
return import_page(message=f"Import error: {str(e)}")
@app.get("/status", response_class=HTMLResponse)
async def status_page():
session = get_instagram_session()
session_valid = session is not None
cache_count = len(STORY_CACHE)
story_count = sum(len(data.get("data", [])) for data in STORY_CACHE.values())
status_badge = 'Connected' if session_valid else 'Disconnected'
status_class = 'connected' if session_valid else 'disconnected'
if session_valid:
status_info = f"✅ Logged in as <strong>{IG_SESSION_USERNAME}</strong><br>"
status_info += f"Session expires: {IG_SESSION_EXPIRY.strftime('%Y-%m-%d %H:%M:%S')}"
else:
status_info = "❌ No active Instagram session<br>Please import your cookies to use the API"
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>API Status</title>
<style>
body {{ font-family: Arial; max-width: 600px; margin: 0 auto; padding: 20px; }}
.container {{ background-color: #fff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 25px; }}
h1 {{ color: #262626; text-align: center; }}
.status-badge {{ display: inline-block; padding: 5px 10px; border-radius: 4px; font-weight: bold; margin-left: 10px; }}
.connected {{ background-color: #e8f5e9; color: #388e3c; }}
.disconnected {{ background-color: #ffebee; color: #d32f2f; }}
.info {{ background-color: #f5f5f5; border-radius: 4px; padding: 15px; margin: 15px 0; }}
.btn {{ background-color: #0095f6; border: none; color: white; padding: 10px 15px; border-radius: 4px; text-decoration: none; display: inline-block; margin-right: 10px; }}
.btn-danger {{ background-color: #f44336; }}
table {{ width: 100%; border-collapse: collapse; margin-top: 15px; }}
th, td {{ text-align: left; padding: 8px; border-bottom: 1px solid #ddd; }}
</style>
</head>
<body>
<div class="container">
<h1>Instagram API Status</h1>
<h2>Connection Status
<span class="status-badge {status_class}">{status_badge}</span>
</h2>
<div class="info">
{status_info}
</div>
<a href="/import" class="btn">{"Re-Import Cookies" if session_valid else "Import Cookies"}</a>
{"<a href='/logout' class='btn btn-danger'>Logout</a>" if session_valid else ""}
<h2>API Endpoints</h2>
<table>
<tr><th>Endpoint</th><th>Description</th></tr>
<tr><td><code>/stories/{{username}}</code></td><td>Get stories for a user</td></tr>
<tr><td><code>/download/{{url}}</code></td><td>Download media from a URL</td></tr>
<tr><td><code>/status</code></td><td>View API status</td></tr>
</table>
<h2>Cache Info</h2>
<div class="info">
<p>Cached users: {cache_count}</p>
<p>Total stories cached: {story_count}</p>
</div>
</div>
</body>
</html>
"""
return HTMLResponse(content=html)
@app.get("/logout")
async def logout():
clear_instagram_session()
return RedirectResponse(url="/status", status_code=303)
class RateLimiter:
def __init__(self, max_requests: int, time_window: timedelta):
self.max_requests = max_requests
self.time_window = time_window
self.requests: Dict[str, list] = defaultdict(list)
def _cleanup_old_requests(self, user_ip: str) -> None:
"""Remove requests that are outside the time window."""
current_time = time.time()
self.requests[user_ip] = [
timestamp for timestamp in self.requests[user_ip]
if current_time - timestamp < self.time_window.total_seconds()
]
def is_rate_limited(self, user_ip: str) -> bool:
"""Check if the user has exceeded their rate limit."""
self._cleanup_old_requests(user_ip)
# Get current count after cleanup
current_count = len(self.requests[user_ip])
# Add current request timestamp (incrementing the count)
current_time = time.time()
self.requests[user_ip].append(current_time)
# Check if user has exceeded the maximum requests
return (current_count + 1) > self.max_requests
def get_current_count(self, user_ip: str) -> int:
"""Get the current request count for an IP."""
self._cleanup_old_requests(user_ip)
return len(self.requests[user_ip])
# Initialize rate limiter with 100 requests per day
rate_limiter = RateLimiter(
max_requests=6,
time_window=timedelta(days=1)
)
def get_user_ip(request: Request) -> str:
"""Helper function to get user's IP address."""
forwarded = request.headers.get("X-Forwarded-For")
if forwarded:
return forwarded.split(",")[0]
return request.client.host
class ApiRotator:
def __init__(self, apis):
self.apis = apis
self.last_successful_index = None
def get_prioritized_apis(self):
if self.last_successful_index is not None:
# Move the last successful API to the front
rotated_apis = (
[self.apis[self.last_successful_index]] +
self.apis[:self.last_successful_index] +
self.apis[self.last_successful_index+1:]
)
return rotated_apis
return self.apis
def update_last_successful(self, index):
self.last_successful_index = index
@app.get("/stories/{username}")
async def get_stories(request: Request, username: str, cached: bool = False):
user_ip = get_user_ip(request)
if rate_limiter.is_rate_limited(user_ip):
current_count = rate_limiter.get_current_count(user_ip)
raise HTTPException(
status_code=429,
detail={
"error": "You have exceeded the maximum number of requests per day. Please try again tomorrow.",
"url": "https://t.me/chrunoss"
}
)
logger.info(f"Request for @{username} stories")
if cached:
cached_result = get_cached_stories(username)
if cached_result:
return {**cached_result, "from_cache": True}
try:
if should_rate_limit_request(username):
cached_result = get_cached_stories(username)
if cached_result:
return {**cached_result, "from_cache": True, "rate_limited": True}
else:
raise HTTPException(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
detail="Rate limit exceeded. Please try again later."
)
result = get_instagram_stories(username)
if not result:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="No valid Instagram session. Please import your cookies first."
)
if "error" in result:
if result["error"] == "api_error":
cached_result = get_cached_stories(username)
if cached_result:
return {**cached_result, "from_cache": True, "error_occurred": True}
raise HTTPException(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
detail=result.get("message", "Instagram API error")
)
elif result["error"] in ["profile_error", "not_found"]:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Profile not found"
)
elif result["error"] in ["no_stories", "no_valid_stories"]:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No stories found for this user"
)
else:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=result.get("message", "Failed to get stories")
)
save_to_cache(username, result)
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"Error: {str(e)}")
cached_result = get_cached_stories(username)
if cached_result:
return {**cached_result, "from_cache": True, "error_occurred": True}
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e)
)
@app.get("/download/{url:path}")
async def download_media(url: str):
logger.info(f"Download request for media")
try:
if not url.startswith(("https://", "http://")):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid URL format"
)
session = get_instagram_session() or requests.Session()
headers = {
"User-Agent": random.choice(USER_AGENTS),
"Referer": "https://www.instagram.com/",
}
response = session.get(url, headers=headers, stream=True, timeout=15)
response.raise_for_status()
if "Content-Type" in response.headers:
content_type = response.headers["Content-Type"]
elif url.endswith((".jpg", ".jpeg")):
content_type = "image/jpeg"
elif url.endswith(".mp4"):
content_type = "video/mp4"
else:
content_type = "application/octet-stream"
logger.info(f"Media downloaded: {content_type}")
return StreamingResponse(
response.iter_content(chunk_size=8192),
media_type=content_type,
headers={"Content-Disposition": f"attachment; filename={url.split('/')[-1]}"}
)
except Exception as e:
logger.error(f"Download failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail="Failed to download media"
)
# Health check endpoint
@app.get("/health")
async def health_check():
session = get_instagram_session()
return {
"status": "ok",
"authenticated": session is not None,
"username": IG_SESSION_USERNAME,
"session_expires": IG_SESSION_EXPIRY.isoformat() if IG_SESSION_EXPIRY else None,
"cache_items": len(STORY_CACHE),
"timestamp": datetime.now().isoformat()
}
# Load cache and session at startup
@app.on_event("startup")
def startup_event():
# Load cache
try:
count = 0
for filename in os.listdir(CACHE_DIR):
if filename.endswith('.json'):
try:
with open(os.path.join(CACHE_DIR, filename), 'r') as f:
cache_data = json.load(f)
if "data" in cache_data and "expires" in cache_data:
expires = datetime.fromisoformat(cache_data["expires"])
if expires > datetime.now():
key = filename.replace('.json', '')
STORY_CACHE[key] = cache_data["data"]
CACHE_EXPIRY[key] = expires
count += 1
except Exception as e:
logger.warning(f"Couldn't load cache file {filename}: {e}")
logger.info(f"Loaded {count} items from cache")
except Exception as e:
logger.error(f"Cache loading error: {e}")
# Try to load session
get_instagram_session() |