-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinstagram_poster.py
More file actions
350 lines (300 loc) · 14.6 KB
/
Copy pathinstagram_poster.py
File metadata and controls
350 lines (300 loc) · 14.6 KB
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
import json
import logging
from pathlib import Path
from instagrapi import Client
from datetime import datetime, timedelta
from typing import Optional, Dict
from config import Config
from instagram_queue import InstagramQueue
import asyncio
import aiohttp
import os
class InstagramPoster:
def __init__(self, translator, database, llm_assistant=None):
self.logger = logging.getLogger(__name__)
self.translator = translator
self.llm_assistant = llm_assistant
self.database = database
# Instagram credentials from Config
self.instagram_username = Config.INSTAGRAM_USERNAME
self.instagram_password = Config.INSTAGRAM_PASSWORD
# Session file path
self.session_file = "instagram_session.json"
# Initialize Instagram client
self.instagram = Client()
# Track posts per hour for rate limiting with persistence
self.rate_limit_file = "instagram_rate_limits.json"
self.load_rate_limits()
# Track posts per hour for rate limiting
self.max_posts_per_hour = 10
self.posts_this_hour = 0
self.last_reset = datetime.now()
self.last_post_time = datetime.now() - timedelta(minutes=5) # Initialize to allow first post
self.min_post_interval = timedelta(minutes=5)
self.queue = InstagramQueue(self, database)
self.queue_task = None
def load_rate_limits(self):
"""Load rate limiting data from file"""
try:
if os.path.exists(self.rate_limit_file):
with open(self.rate_limit_file, 'r') as f:
data = json.load(f)
self.posts_this_hour = data['posts_this_hour']
self.last_reset = datetime.fromtimestamp(data['last_reset'])
self.last_post_time = datetime.fromtimestamp(data['last_post_time'])
else:
self.posts_this_hour = 0
self.last_reset = datetime.now()
self.last_post_time = datetime.now() - timedelta(minutes=5)
self.save_rate_limits()
except Exception as e:
self.logger.error(f"Error loading rate limits: {e}")
self.posts_this_hour = 0
self.last_reset = datetime.now()
self.last_post_time = datetime.now() - timedelta(minutes=5)
def save_rate_limits(self):
"""Save rate limiting data to file"""
try:
data = {
'posts_this_hour': self.posts_this_hour,
'last_reset': self.last_reset.timestamp(),
'last_post_time': self.last_post_time.timestamp()
}
with open(self.rate_limit_file, 'w') as f:
json.dump(data, f)
except Exception as e:
self.logger.error(f"Error saving rate limits: {e}")
async def login(self):
"""Login to Instagram with session persistence"""
try:
self.instagram = Client()
# Try to load existing session first
if os.path.exists(self.session_file):
try:
self.logger.info("Found existing session file")
self.instagram.load_settings(self.session_file)
# Verify existing session
if await self.verify_session():
self.logger.info("Successfully loaded existing session")
return True
else:
self.logger.warning("Existing session invalid, creating new login")
except Exception as e:
self.logger.warning(f"Error loading existing session: {e}")
# If we get here, we need a fresh login
self.logger.info("Creating new Instagram session")
# Set device settings before login
self.instagram.set_device({
"app_version": "269.0.0.18.75",
"android_version": 26,
"android_release": "8.0.0",
"dpi": "480dpi",
"resolution": "1080x1920",
"manufacturer": "OnePlus",
"device": "6T Dev",
"model": "devitron",
"cpu": "qcom",
"version_code": "314665256"
})
# Add delay before login
await asyncio.sleep(2)
self.instagram.login(
username=self.instagram_username,
password=self.instagram_password
)
# Add delay after login
await asyncio.sleep(2)
# Save the new session
self.instagram.dump_settings(self.session_file)
self.logger.info("New session saved successfully")
return await self.verify_session()
except Exception as e:
self.logger.error(f"Login error: {e}")
return False
async def verify_session(self):
"""Verify Instagram session is valid"""
try:
# Test the session with a simple request
self.instagram.get_timeline_feed()
return True
except Exception as e:
self.logger.error(f"Session verification failed: {e}")
return False
async def process_and_post(self, content: str, media_path: Optional[str] = None) -> bool:
success = False
try:
current_time = datetime.now()
# Check hourly rate limit
if (current_time - self.last_reset).total_seconds() > 3600:
self.posts_this_hour = 0
self.last_reset = current_time
self.save_rate_limits()
if self.posts_this_hour >= self.max_posts_per_hour:
self.logger.warning("Instagram hourly rate limit reached")
return False
# Check minimum time interval between posts
time_since_last_post = current_time - self.last_post_time
if time_since_last_post < self.min_post_interval:
self.logger.warning(f"Need to wait {(self.min_post_interval - time_since_last_post).seconds} more seconds before posting")
return False
# Summarize for Instagram
caption = self.translator.summarize_for_instagram(content, max_chars=2200)
if not caption:
return False
# Verify media file exists and is valid
if media_path:
if not os.path.exists(media_path):
self.logger.error(f"Media file not found: {media_path}")
return False
if not os.path.getsize(media_path):
self.logger.error("Empty media file")
return False
try:
# Add delay before posting
await asyncio.sleep(5)
# Attempt photo upload
try:
media = self.instagram.photo_upload(
path=media_path,
caption=caption
)
success = True
self.logger.info(f"Clean success! Media ID: {media.id}")
except Exception as e:
error_str = str(e)
self.logger.error(f"Photo Upload failed: {error_str}")
# Check if the exception has a 'response' attribute
if hasattr(e, 'response') and e.response:
response = e.response
self.logger.info(f"Response URL: {response.url}")
self.logger.info(f"Response Status: {response.status_code}")
# If it's the configure endpoint with 400, treat as success
if (response.status_code == 400 and
"media/configure" in response.url):
self.logger.info("✅ SUCCESS: 400 on configure endpoint is expected!")
success = True
elif "Handle is missing" in error_str or "feedback_required" in error_str:
self.logger.info("✅ SUCCESS: Handle is missing or feedback_required, treating as success!")
success = True
else:
self.logger.error(f"❌ FAIL: {response.status_code} on {response.url}")
return False
else:
# Check if the error message contains 'Handle is missing'
if "Handle is missing" in error_str or "feedback_required" in error_str:
self.logger.info("✅ SUCCESS: Handle is missing or feedback_required, treating as success!")
success = True
else:
self.logger.error(f"❌ FAIL: {error_str}")
return False
# Add delay after successful post
if success:
await asyncio.sleep(5)
except Exception as e:
self.logger.error(f"Fatal upload error: {e}")
return False
# Update tracking only if successful
if success:
self.posts_this_hour += 1
self.last_post_time = datetime.now()
self.save_rate_limits()
# Cleanup media file after successful post
if success and media_path and os.path.exists(media_path):
try:
os.remove(media_path)
self.logger.info(f"Cleaned up posted media file: {media_path}")
except Exception as e:
self.logger.error(f"Error cleaning up media file: {e}")
return success
except Exception as e:
self.logger.error(f"Error in process_and_post: {e}")
return False
async def start_queue_processing(self):
"""Start the queue processing worker"""
self.queue_task = asyncio.create_task(self.queue.start_processing())
self.logger.info("Started Instagram queue processing")
async def stop_queue_processing(self):
"""Stop the queue processing worker"""
if self.queue_task:
self.logger.info("Initiating queue shutdown")
await self.queue.stop_processing()
try:
await asyncio.wait_for(self.queue_task, timeout=10.0)
except asyncio.TimeoutError:
self.logger.warning("Timeout waiting for queue task to stop")
except Exception as e:
self.logger.error(f"Error during queue shutdown: {e}")
finally:
self.logger.info("Stopped Instagram queue processing")
async def queue_post(self, content: str, url: str, media_path: str) -> bool:
try:
# Use InstagramQueue's add_to_queue method to ensure proper item type and database handling
added = await self.queue.add_to_queue(content, url, media_path)
if added:
current_size = self.queue.queue.qsize()
self.logger.info(f"Added new item to Instagram queue. Queue size: {current_size}")
return True
else:
self.logger.error(f"Failed to add item to Instagram queue: {url}")
return False
except Exception as e:
self.logger.error(f"Error queuing Instagram post: {e}")
return False
async def process_queue(self):
"""Process the Instagram posting queue"""
while True:
try:
# Get item from queue
content, url, media_path = await self.queue.queue.get()
self.logger.info(f"Processing queue item. Remaining items: {self.queue.queue.qsize()}")
try:
# Process the post
success = await self._post_to_instagram(content, media_path)
if success:
self.logger.info(f"Successfully posted to Instagram: {url}")
self.db.mark_as_posted(url, 'instagram')
else:
self.logger.error(f"Failed to post to Instagram: {url}")
finally:
# Always mark task as done
self.queue.queue.task_done()
self.logger.info(f"Queue item processed. Remaining tasks: {self.queue.queue.unfinished_tasks}")
except asyncio.CancelledError:
break
except Exception as e:
self.logger.error(f"Error in queue processing: {e}")
async def check_health(self):
"""Check if Instagram connection is healthy"""
try:
# First verify session
if not await self.verify_session():
# Try one login attempt if session is invalid
if not await self.login():
return False
# Additional health checks - use get_timeline_feed() instead
# as it's a reliable way to check if we're properly authenticated
self.instagram.get_timeline_feed()
return True
except Exception as e:
self.logger.error(f"Instagram health check failed: {e}")
return False
async def can_post(self) -> bool:
"""Check if we can post based on rate limits"""
current_time = datetime.now()
# Check hourly rate limit
if (current_time - self.last_reset).total_seconds() > 3600:
self.posts_this_hour = 0
self.last_reset = current_time
self.save_rate_limits()
return True
if self.posts_this_hour >= self.max_posts_per_hour:
self.logger.warning("Instagram hourly rate limit reached")
return False
# Check minimum time interval between posts
time_since_last_post = current_time - self.last_post_time
if time_since_last_post < self.min_post_interval:
wait_time = (self.min_post_interval - time_since_last_post).seconds
if wait_time > 0:
self.logger.warning(f"Need to wait {wait_time} seconds before posting")
return False
return True