Update app.py
Browse files
app.py
CHANGED
@@ -72,28 +72,31 @@ async def download_post(request: PostRequest):
|
|
72 |
raise HTTPException(status_code=400, detail=str(e))
|
73 |
|
74 |
L = instaloader.Instaloader()
|
75 |
-
|
76 |
-
#
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
97 |
|
98 |
except instaloader.exceptions.InvalidArgumentException:
|
99 |
logger.error(f"Post not found for shortcode: {shortcode}")
|
|
|
72 |
raise HTTPException(status_code=400, detail=str(e))
|
73 |
|
74 |
L = instaloader.Instaloader()
|
75 |
+
try:
|
76 |
+
# Login to Instagram
|
77 |
+
L.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
|
78 |
+
logger.info("Successfully logged in to Instagram")
|
79 |
+
except instaloader.exceptions.InstaloaderException as e:
|
80 |
+
logger.error(f"Login error: {e}")
|
81 |
+
raise HTTPException(status_code=500, detail="Error logging in to Instagram")
|
82 |
+
|
83 |
+
# Configure retry parameters
|
84 |
+
max_retries = 3
|
85 |
+
retry_delay = random.uniform(30, 60)
|
86 |
+
|
87 |
+
for attempt in range(max_retries):
|
88 |
+
try:
|
89 |
+
# Fetch post by shortcode
|
90 |
+
post = instaloader.Post.from_shortcode(L.context, shortcode)
|
91 |
+
logger.info(f"Fetched post: {post}")
|
92 |
+
break
|
93 |
+
|
94 |
+
except instaloader.exceptions.ConnectionException as e:
|
95 |
+
logger.error(f"Connection error (attempt {attempt + 1}): {e}")
|
96 |
+
if attempt == max_retries - 1:
|
97 |
+
raise HTTPException(status_code=500, detail="Failed to connect to Instagram after multiple attempts")
|
98 |
+
time.sleep(retry_delay)
|
99 |
+
retry_delay *= 2 # Exponential backoff
|
100 |
|
101 |
except instaloader.exceptions.InvalidArgumentException:
|
102 |
logger.error(f"Post not found for shortcode: {shortcode}")
|