-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_improved.py
More file actions
376 lines (310 loc) · 15.2 KB
/
run_improved.py
File metadata and controls
376 lines (310 loc) · 15.2 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
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
#!/usr/bin/env python3
"""
Run Script for Instagram Automation Tool (Improved)
-------------------------------------------------
This script provides a command-line interface to run the Instagram Automation Tool.
It uses the improved InstagramAutomationTool class with better error handling and
consistent API.
"""
import os
import sys
import argparse
import logging
import json
from datetime import datetime
from instagram_automation_tool_improved import InstagramAutomationTool
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('instagram_automation.log')
]
)
logger = logging.getLogger(__name__)
def parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description='Instagram Automation Tool')
# Main command
subparsers = parser.add_subparsers(dest='command', help='Command to run')
# Account commands
account_parser = subparsers.add_parser('account', help='Account management commands')
account_subparsers = account_parser.add_subparsers(dest='account_command', help='Account command to run')
# Create account
create_account_parser = account_subparsers.add_parser('create', help='Create a new Instagram account')
create_account_parser.add_argument('--username', required=True, help='Username for the account')
create_account_parser.add_argument('--password', required=True, help='Password for the account')
create_account_parser.add_argument('--email', required=True, help='Email for verification')
create_account_parser.add_argument('--phone', help='Phone number for verification (optional)')
# Setup profile
setup_profile_parser = account_subparsers.add_parser('setup-profile', help='Set up an Instagram profile')
setup_profile_parser.add_argument('--account-id', required=True, help='ID of the account')
setup_profile_parser.add_argument('--bio', help='Biography text')
setup_profile_parser.add_argument('--profile-pic', help='Path to profile picture')
setup_profile_parser.add_argument('--external-link', help='External link for bio')
# Check account health
health_parser = account_subparsers.add_parser('health', help='Check account health')
health_parser.add_argument('--account-id', required=True, help='ID of the account')
# Messaging commands
message_parser = subparsers.add_parser('message', help='Messaging commands')
message_subparsers = message_parser.add_subparsers(dest='message_command', help='Messaging command to run')
# Create campaign
create_campaign_parser = message_subparsers.add_parser('create-campaign', help='Create a messaging campaign')
create_campaign_parser.add_argument('--account-ids', required=True, help='Comma-separated list of account IDs')
create_campaign_parser.add_argument('--target-source', required=True, choices=['follower_list', 'text_file'], help='Source of target users')
create_campaign_parser.add_argument('--target-details', required=True, help='JSON string with target details')
create_campaign_parser.add_argument('--message-templates', required=True, help='JSON string with message templates')
create_campaign_parser.add_argument('--use-cupidbot', action='store_true', help='Use CupidBot for AI conversations')
# Start campaign
start_campaign_parser = message_subparsers.add_parser('start-campaign', help='Start a messaging campaign')
start_campaign_parser.add_argument('--campaign-id', required=True, help='ID of the campaign')
start_campaign_parser.add_argument('--start-time', help='Start time in ISO format (e.g., 2025-04-28T12:00:00)')
# Send message
send_message_parser = message_subparsers.add_parser('send', help='Send a direct message')
send_message_parser.add_argument('--account-id', required=True, help='ID of the account to send from')
send_message_parser.add_argument('--target-username', required=True, help='Username of the target user')
send_message_parser.add_argument('--message-template', required=True, help='JSON string with message template')
send_message_parser.add_argument('--use-cupidbot', action='store_true', help='Use CupidBot for AI conversations')
# Post commands
post_parser = subparsers.add_parser('post', help='Posting commands')
post_subparsers = post_parser.add_subparsers(dest='post_command', help='Posting command to run')
# Create post
create_post_parser = post_subparsers.add_parser('create', help='Create a post')
create_post_parser.add_argument('--account-id', required=True, help='ID of the account')
create_post_parser.add_argument('--media-paths', required=True, help='Comma-separated list of media paths')
create_post_parser.add_argument('--caption', required=True, help='Post caption')
create_post_parser.add_argument('--hashtags', help='Comma-separated list of hashtags')
create_post_parser.add_argument('--tagged-users', help='Comma-separated list of tagged users')
create_post_parser.add_argument('--location', help='Location tag')
create_post_parser.add_argument('--scheduled-time', help='Scheduled time in ISO format (e.g., 2025-04-28T12:00:00)')
# Publish post
publish_post_parser = post_subparsers.add_parser('publish', help='Publish a post')
publish_post_parser.add_argument('--post-id', required=True, help='ID of the post to publish')
# Track post performance
track_post_parser = post_subparsers.add_parser('track', help='Track post performance')
track_post_parser.add_argument('--post-id', required=True, help='ID of the post to track')
# Run command
run_parser = subparsers.add_parser('run', help='Run the tool')
run_parser.add_argument('--continuous', action='store_true', help='Run in continuous mode')
run_parser.add_argument('--interval', type=int, default=60, help='Interval in seconds between checks in continuous mode')
run_parser.add_argument('--max-runtime', type=int, help='Maximum runtime in seconds for continuous mode')
args = parser.parse_args()
# Check if a command was provided
if not args.command:
parser.print_help()
sys.exit(1)
# Check if a subcommand was provided for commands that require one
if args.command in ['account', 'message', 'post']:
subcommand_attr = f"{args.command}_command"
if not getattr(args, subcommand_attr, None):
# Get the appropriate subparser
if args.command == 'account':
subparser = account_parser
elif args.command == 'message':
subparser = message_parser
elif args.command == 'post':
subparser = post_parser
# Print help for the specific command
subparser.print_help()
sys.exit(1)
return args
def main():
"""Run the Instagram Automation Tool."""
args = parse_arguments()
# Initialize the tool
tool = InstagramAutomationTool()
# Process commands
if args.command == 'account':
process_account_commands(args, tool)
elif args.command == 'message':
process_message_commands(args, tool)
elif args.command == 'post':
process_post_commands(args, tool)
elif args.command == 'run':
run_tool(tool, args)
else:
logger.error(f"Unknown command: {args.command}")
sys.exit(1)
def process_account_commands(args, tool):
"""Process account management commands."""
if args.account_command == 'create':
result = tool.create_account(
username=args.username,
password=args.password,
email=args.email,
phone=args.phone
)
if result.get("status") == "error":
logger.error(f"Error creating account: {result.get('message')}")
sys.exit(1)
logger.info(f"Account created: {result['id']}")
elif args.account_command == 'setup-profile':
result = tool.setup_profile(
account_id=args.account_id,
bio=args.bio or "",
profile_pic=args.profile_pic or "",
external_link=args.external_link or ""
)
if result.get("status") == "error":
logger.error(f"Error setting up profile: {result.get('message')}")
sys.exit(1)
logger.info(f"Profile setup completed for account: {args.account_id}")
elif args.account_command == 'health':
result = tool.check_account_health(
account_id=args.account_id
)
if result.get("status") == "error":
logger.error(f"Error checking account health: {result.get('message')}")
sys.exit(1)
logger.info(f"Account health status: {result['health']['status']}")
else:
logger.error(f"Unknown account command: {args.account_command}")
logger.info("Available account commands: create, setup-profile, health")
sys.exit(1)
def process_message_commands(args, tool):
"""Process messaging commands."""
if args.message_command == 'create-campaign':
try:
account_ids = args.account_ids.split(',')
target_details = json.loads(args.target_details)
message_templates = json.loads(args.message_templates)
except json.JSONDecodeError as e:
logger.error(f"Error parsing JSON: {str(e)}")
sys.exit(1)
result = tool.create_messaging_campaign(
account_ids=account_ids,
target_source=args.target_source,
target_details=target_details,
message_templates=message_templates,
use_cupidbot=args.use_cupidbot
)
if result.get("status") == "error":
logger.error(f"Error creating campaign: {result.get('message')}")
sys.exit(1)
logger.info(f"Campaign created: {result['id']}")
elif args.message_command == 'start-campaign':
start_time = None
if args.start_time:
try:
start_time = datetime.fromisoformat(args.start_time)
except ValueError:
logger.error(f"Invalid datetime format: {args.start_time}. Expected format: YYYY-MM-DDTHH:MM:SS")
sys.exit(1)
result = tool.start_campaign(
campaign_id=args.campaign_id,
start_time=start_time
)
if result.get("status") == "error":
logger.error(f"Error starting campaign: {result.get('message')}")
sys.exit(1)
if start_time:
logger.info(f"Campaign scheduled to start at {args.start_time}")
else:
logger.info(f"Campaign started: {args.campaign_id}")
elif args.message_command == 'send':
try:
message_template = json.loads(args.message_template)
except json.JSONDecodeError as e:
logger.error(f"Error parsing JSON: {str(e)}")
sys.exit(1)
result = tool.send_message(
account_id=args.account_id,
target_username=args.target_username,
message_template=message_template,
use_cupidbot=args.use_cupidbot
)
if result.get("status") == "error":
logger.error(f"Error sending message: {result.get('message')}")
sys.exit(1)
logger.info(f"Message sent to {args.target_username}")
else:
logger.error(f"Unknown message command: {args.message_command}")
logger.info("Available message commands: create-campaign, start-campaign, send")
sys.exit(1)
def process_post_commands(args, tool):
"""Process posting commands."""
if args.post_command == 'create':
try:
media_paths = args.media_paths.split(',')
hashtags = args.hashtags.split(',') if args.hashtags else []
tagged_users = args.tagged_users.split(',') if args.tagged_users else []
except Exception as e:
logger.error(f"Error parsing arguments: {str(e)}")
sys.exit(1)
scheduled_time = None
if args.scheduled_time:
try:
scheduled_time = datetime.fromisoformat(args.scheduled_time)
except ValueError:
logger.error(f"Invalid datetime format: {args.scheduled_time}. Expected format: YYYY-MM-DDTHH:MM:SS")
sys.exit(1)
result = tool.create_post(
account_id=args.account_id,
media_paths=media_paths,
caption=args.caption,
hashtags=hashtags,
tagged_users=tagged_users,
location=args.location or "",
scheduled_time=scheduled_time
)
if result.get("status") == "error":
logger.error(f"Error creating post: {result.get('message')}")
sys.exit(1)
if scheduled_time:
logger.info(f"Post scheduled: {result['id']} for {args.scheduled_time}")
else:
logger.info(f"Post created: {result['id']}")
elif args.post_command == 'publish':
result = tool.publish_post(
post_id=args.post_id
)
if result.get("status") == "error":
logger.error(f"Error publishing post: {result.get('message')}")
sys.exit(1)
logger.info(f"Post published: {args.post_id}")
elif args.post_command == 'track':
result = tool.track_post_performance(
post_id=args.post_id
)
if result.get("status") == "error":
logger.error(f"Error tracking post performance: {result.get('message')}")
sys.exit(1)
logger.info(f"Post performance tracked: {args.post_id}")
logger.info(f"Likes: {result['performance']['likes']}, Comments: {result['performance']['comments']}")
else:
logger.error(f"Unknown post command: {args.post_command}")
logger.info("Available post commands: create, publish, track")
sys.exit(1)
def run_tool(tool, args):
"""Run the tool in continuous or one-shot mode."""
logger.info("Starting Instagram Automation Tool")
# Extract run parameters
continuous = getattr(args, 'continuous', False)
interval = getattr(args, 'interval', 60)
max_runtime = getattr(args, 'max_runtime', None)
# Run mode info
if continuous:
logger.info(f"Running in continuous mode with {interval}s interval")
if max_runtime:
logger.info(f"Will stop after {max_runtime}s")
else:
logger.info("Running in one-shot mode")
# Run the tool
result = tool.run(
continuous=continuous,
interval=interval,
max_runtime=max_runtime
)
# Process result
if result.get("status") == "error":
logger.error(f"Error running tool: {result.get('message')}")
sys.exit(1)
elif result.get("status") == "stopped":
logger.info(f"Tool was stopped: {result.get('message')}")
else:
logger.info(f"Tool completed successfully: {result.get('message')}")
if continuous:
logger.info(f"Completed {result.get('cycles', 0)} cycles")
if __name__ == "__main__":
main()