Chrunos commited on
Commit
d5fe959
·
verified ·
1 Parent(s): 51a907c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
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
- # Configure retry parameters
77
- max_retries = 3
78
- retry_delay = random.uniform(1, 3)
79
-
80
- for attempt in range(max_retries):
81
- try:
82
- # Login to Instagram
83
- L.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
84
- logger.info("Successfully logged in to Instagram")
85
-
86
- # Fetch post by shortcode
87
- post = instaloader.Post.from_shortcode(L.context, shortcode)
88
- logger.info(f"Fetched post: {post}")
89
- break
90
-
91
- except instaloader.exceptions.ConnectionException as e:
92
- logger.error(f"Connection error (attempt {attempt + 1}): {e}")
93
- if attempt == max_retries - 1:
94
- raise HTTPException(status_code=500, detail="Failed to connect to Instagram after multiple attempts")
95
- time.sleep(retry_delay)
96
- retry_delay *= 2 # Exponential backoff
 
 
 
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}")