Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import instaloader
|
|
@@ -5,8 +6,6 @@ import os
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import re
|
| 7 |
from typing import Optional, List
|
| 8 |
-
import logging
|
| 9 |
-
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
|
@@ -15,6 +14,10 @@ load_dotenv()
|
|
| 15 |
INSTAGRAM_USERNAME = os.getenv('INSTAGRAM_USERNAME')
|
| 16 |
INSTAGRAM_PASSWORD = os.getenv('INSTAGRAM_PASSWORD')
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
app = FastAPI()
|
| 19 |
|
| 20 |
class DownloadRequest(BaseModel):
|
|
@@ -75,18 +78,27 @@ async def download_media(request: DownloadRequest):
|
|
| 75 |
compress_json=False
|
| 76 |
)
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
if
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Get post
|
| 86 |
post = instaloader.Post.from_shortcode(loader.context, shortcode)
|
| 87 |
-
|
| 88 |
urls = []
|
| 89 |
-
|
| 90 |
if post.is_video:
|
| 91 |
media_type = "video"
|
| 92 |
urls.append(post.video_url)
|
|
@@ -96,9 +108,9 @@ async def download_media(request: DownloadRequest):
|
|
| 96 |
# Multiple images
|
| 97 |
for node in post.get_sidecar_nodes():
|
| 98 |
urls.append(node.display_url)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
return DownloadResponse(
|
| 104 |
success=True,
|
|
@@ -109,8 +121,12 @@ async def download_media(request: DownloadRequest):
|
|
| 109 |
)
|
| 110 |
|
| 111 |
except instaloader.exceptions.InstaloaderException as e:
|
|
|
|
|
|
|
| 112 |
raise HTTPException(status_code=400, detail=str(e))
|
| 113 |
except Exception as e:
|
|
|
|
|
|
|
| 114 |
raise HTTPException(status_code=500, detail=str(e))
|
| 115 |
|
| 116 |
@app.get("/api/health")
|
|
|
|
| 1 |
+
import logging
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import instaloader
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import re
|
| 8 |
from typing import Optional, List
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
|
|
|
| 14 |
INSTAGRAM_USERNAME = os.getenv('INSTAGRAM_USERNAME')
|
| 15 |
INSTAGRAM_PASSWORD = os.getenv('INSTAGRAM_PASSWORD')
|
| 16 |
|
| 17 |
+
# Configure logging
|
| 18 |
+
logging.basicConfig(level=logging.INFO)
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
app = FastAPI()
|
| 22 |
|
| 23 |
class DownloadRequest(BaseModel):
|
|
|
|
| 78 |
compress_json=False
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Load session
|
| 82 |
+
if os.path.exists(SESSION_FILE):
|
| 83 |
+
loader.load_session_from_file(INSTAGRAM_USERNAME, SESSION_FILE)
|
| 84 |
+
else:
|
| 85 |
+
# Log before login attempt
|
| 86 |
+
logger.info("Attempting to log in with username: %s", INSTAGRAM_USERNAME)
|
| 87 |
+
|
| 88 |
+
# Login
|
| 89 |
+
if not all([INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD]):
|
| 90 |
+
raise HTTPException(status_code=500, detail="Missing Instagram credentials")
|
| 91 |
+
loader.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
|
| 92 |
+
|
| 93 |
+
# Log after successful login
|
| 94 |
+
logger.info("Login successful for username: %s", INSTAGRAM_USERNAME)
|
| 95 |
+
loader.save_session_to_file(SESSION_FILE)
|
| 96 |
|
| 97 |
# Get post
|
| 98 |
post = instaloader.Post.from_shortcode(loader.context, shortcode)
|
| 99 |
+
|
| 100 |
urls = []
|
| 101 |
+
|
| 102 |
if post.is_video:
|
| 103 |
media_type = "video"
|
| 104 |
urls.append(post.video_url)
|
|
|
|
| 108 |
# Multiple images
|
| 109 |
for node in post.get_sidecar_nodes():
|
| 110 |
urls.append(node.display_url)
|
| 111 |
+
else:
|
| 112 |
+
# Single image
|
| 113 |
+
urls.append(post.url)
|
| 114 |
|
| 115 |
return DownloadResponse(
|
| 116 |
success=True,
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
except instaloader.exceptions.InstaloaderException as e:
|
| 124 |
+
# Log Instaloader exception
|
| 125 |
+
logger.error("InstaloaderException: %s", str(e))
|
| 126 |
raise HTTPException(status_code=400, detail=str(e))
|
| 127 |
except Exception as e:
|
| 128 |
+
# Log general exception
|
| 129 |
+
logger.error("Exception: %s", str(e))
|
| 130 |
raise HTTPException(status_code=500, detail=str(e))
|
| 131 |
|
| 132 |
@app.get("/api/health")
|