Skip to content

Commit

Permalink
Add configurable captions
Browse files Browse the repository at this point in the history
  • Loading branch information
birdhouses committed Jul 31, 2024
1 parent 084ec89 commit f14e6bc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
12 changes: 7 additions & 5 deletions src/gui/main_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_default_value(keys):
('enabled', 'checkbox', 'enabled', get_default_value(['comment_on_media', 'enabled'])),
('comment on tag', 'entry', 'comment_on_tag', get_default_value(['comment_on_media', 'comment_on_tag'])),
('amount per day', 'entry', 'amount_per_day', get_default_value(['comment_on_media', 'amount_per_day'])),
('comments (seperated by newline)', 'textarea', 'comments', get_default_value(['comment_on_media', 'comments']))
('comments (separated by newline)', 'textarea', 'comments', get_default_value(['comment_on_media', 'comments']))
])
self.comment_on_media_frame.grid(row=4, column=0, padx=10, pady=(10, 0), sticky="ew")

Expand All @@ -63,7 +63,8 @@ def get_default_value(keys):
('amount per day', 'entry', 'amount_per_day', get_default_value(['upload_posts', 'amount_per_day'])),
('posts_dir', 'entry', 'posts_dir', get_default_value(['upload_posts', 'posts_dir'])),
('delete file after upload', 'checkbox', 'delete_after_upload', get_default_value(['upload_posts', 'delete_after_upload'])),
('caption (seperated by newline)', 'textarea', 'caption', get_default_value(['upload_posts', 'caption']))
('caption (separated by newline)', 'textarea', 'caption', get_default_value(['upload_posts', 'caption'])),
('captions file', 'entry', 'captions_file', get_default_value(['upload_posts', 'captions_file'])) # New field
])
self.upload_posts_frame.grid(row=6, column=0, padx=10, pady=(10, 0), sticky="ew")

Expand All @@ -72,7 +73,8 @@ def get_default_value(keys):
('amount per day', 'entry', 'amount_per_day', get_default_value(['upload_stories', 'amount_per_day'])),
('posts_dir', 'entry', 'posts_dir', get_default_value(['upload_stories', 'posts_dir'])),
('delete file after upload', 'checkbox', 'delete_after_upload', get_default_value(['upload_stories', 'delete_after_upload'])),
('caption (seperated by newline)', 'textarea', 'caption', get_default_value(['upload_stories', 'caption']))
('caption (separated by newline)', 'textarea', 'caption', get_default_value(['upload_stories', 'caption'])),
('captions file', 'entry', 'captions_file', get_default_value(['upload_stories', 'captions_file'])) # New field
])
self.upload_stories_frame.grid(row=7, column=0, padx=10, pady=(10, 0), sticky="ew")

Expand All @@ -94,7 +96,7 @@ def get_default_value(keys):
fields=[
('enabled', 'checkbox', 'enabled', get_default_value(['dm_accounts_from_list', 'enabled'])),
('message', 'textarea', 'message', get_default_value(['dm_accounts_from_list', 'message'])),
('accounts (seperated by newline)', 'textarea', 'accounts', get_default_value(['dm_accounts_from_list', 'accounts'])),
('accounts (separated by newline)', 'textarea', 'accounts', get_default_value(['dm_accounts_from_list', 'accounts'])),
('request timeout', 'entry', 'timeout', get_default_value(['dm_accounts_from_list','timeout']))
])
self.dm_accounts_from_list_frame.grid(row=9, column=0, padx=10, pady=(10, 0), sticky="ew")
Expand Down Expand Up @@ -128,4 +130,4 @@ def __init__(self, account=None, parent=None):
self.scrollable_frame = ScrollableFrame(self, width=frame_width, height=frame_height, account=account, parent=parent)
self.scrollable_frame.grid(row=0, column=0, padx=10, pady=(10,0))

self.mainloop()
self.mainloop()
70 changes: 46 additions & 24 deletions src/instabot/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def upload_media(cl, account):

username = account['account_details']['username']
amount = account['upload_posts']['amount_per_day']
captions = account['upload_posts']['caption']
captions_file = account['upload_posts'].get('captions_file') # Captions file path
posts_dir = account['upload_posts']['posts_dir']
delete_after_upload = account['upload_posts']['delete_after_upload']
medias = os.listdir(posts_dir)
Expand All @@ -23,14 +23,14 @@ async def upload_media(cl, account):
random.shuffle(medias)
##############################

captions = load_captions(captions_file)

for media in medias:
sleep_time = utils.calculate_sleep_time(amount)

TimeKeeper(username, 'upload_media', sleep_time)

await asyncio.sleep(sleep_time)

caption = random.choice(captions)
caption = captions.get(media, random.choice(list(captions.values()))) # Get caption or random
if is_post(media):
upload_post(cl, media, posts_dir, caption, delete_after_upload)
elif is_album(posts_dir, media):
Expand Down Expand Up @@ -69,11 +69,7 @@ def is_album(posts_dir: str, path: str) -> bool:
def is_post(path: str) -> bool:
valid_images = [".jpg", ".webp", ".png"]
ext = os.path.splitext(path)[1]
if ext.lower() not in valid_images:

return False

return True
return ext.lower() in valid_images

def upload_album(cl, album: str, posts_dir, caption: str, delete_after_upload: bool):
path_to_album = posts_dir + '/' + album
Expand Down Expand Up @@ -101,14 +97,17 @@ def upload_album(cl, album: str, posts_dir, caption: str, delete_after_upload: b
utils.logger.info(f"Uploaded {album}")

def upload_post(cl, path, posts_dir, caption, delete_after_upload):
path_to_post = posts_dir + '/' + path

path_to_post = posts_dir + '/' + path
try:
cl.photo_upload(path_to_post, caption)
except Exception as e:
utils.logger.error(f"Error uploading {path}: {e}")
return

if delete_after_upload:
os.remove(path_to_post)
if delete_after_upload:
os.remove(path_to_post)

utils.logger.info(f"Uploaded {path}")
utils.logger.info(f"Uploaded {path}")

def get_posts(directory: str) -> List[str]:
posts = []
Expand Down Expand Up @@ -148,7 +147,7 @@ async def upload_stories(cl, account):
utils.logger.info("Uploading story...")
username = account['account_details']['username']
amount = account['upload_stories']['amount_per_day']
captions = account['upload_stories']['caption']
captions_file = account['upload_stories'].get('captions_file') # Captions file path
posts_dir = account['upload_stories']['posts_dir']
delete_after_upload = account['upload_stories']['delete_after_upload']
medias = os.listdir(posts_dir)
Expand All @@ -157,14 +156,14 @@ async def upload_stories(cl, account):
random.shuffle(medias)
##############################

captions = load_captions(captions_file)

for media in medias:
sleep_time = utils.calculate_sleep_time(amount)

TimeKeeper(username, 'upload_story', sleep_time)

await asyncio.sleep(sleep_time)

caption = random.choice(captions)
caption = captions.get(media, random.choice(list(captions.values()))) # Get caption or random
if is_post(media):
upload_story_post(cl, media, posts_dir, caption, delete_after_upload)
elif is_video(media):
Expand All @@ -177,25 +176,48 @@ async def upload_stories(cl, account):
continue

def upload_story_post(cl, path, posts_dir, caption, delete_after_upload):
path_to_post = posts_dir + '/' + path
path_to_post = posts_dir + '/' + path

try:
cl.photo_upload_to_story(path_to_post, caption)
except Exception as e:
utils.logger.error(f"Error uploading story post {path}: {e}")
return

if delete_after_upload:
os.remove(path_to_post)
if delete_after_upload:
os.remove(path_to_post)

utils.logger.info(f"Uploaded {path}")
utils.logger.info(f"Uploaded {path}")

def upload_story_video(cl, path, posts_dir, caption, delete_after_upload):
path_to_post = posts_dir + '/' + path

try:
cl.video_upload_to_story(path_to_post, caption)
except Exception as e:
utils.logger.error(f"Error uploading {path}: {e}")
utils.logger.error(f"Error uploading story video {path}: {e}")
return

if delete_after_upload:
os.remove(path_to_post)

utils.logger.info(f"Uploaded {path}")
utils.logger.info(f"Uploaded {path}")

def load_captions(captions_file: str) -> dict:
captions = {}
if not captions_file:
return captions

try:
with open(captions_file, 'r', encoding='utf-8') as f:
for line in f:
parts = line.strip().split('|')
if len(parts) == 2:
media, caption = parts
captions[media] = caption
except FileNotFoundError:
utils.logger.warning(f"Captions file {captions_file} not found.")
except Exception as e:
utils.logger.error(f"Error reading captions file {captions_file}: {e}")

return captions

0 comments on commit f14e6bc

Please sign in to comment.