-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from birdhouses/23-implement-asyncio
23 implement asyncio
- Loading branch information
Showing
7 changed files
with
103 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .utils import get_client, load_config, get_user_id, get_followers, calculate_sleep_time | ||
from .utils import get_client, load_config, get_user_id, get_followers, calculate_sleep_time, logger, parse_time_string | ||
from .follow import follow_user_followers, unfollow_users | ||
from .like_media import like_recent_posts | ||
from .comment import comment_on_media |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,48 @@ | ||
import instabot | ||
import threading | ||
import asyncio | ||
from instabot import follow_user_followers, unfollow_users, comment_on_media | ||
from instabot import logger | ||
from threading import Thread | ||
|
||
def run_bot(account): | ||
async def main(account): | ||
username = account['username'] | ||
password = account['password'] | ||
|
||
cl = instabot.get_client(username, password) | ||
worker_threads = [] | ||
|
||
if account['follow_users']['enabled']: | ||
follow_thread = threading.Thread(target=instabot.follow_user_followers, args=(cl, account)) | ||
worker_threads.append(follow_thread) | ||
follow_thread.start() | ||
if account['unfollow_users']['enabled']: | ||
unfollow_after = account['unfollow_users']['unfollow_after'] | ||
unfollow_thread = threading.Thread(target=instabot.unfollow_users, args=(cl, unfollow_after)) | ||
worker_threads.append(unfollow_thread) | ||
unfollow_thread.start() | ||
if account['comment_on_media']['enabled']: | ||
comment_thread = threading.Thread(target=instabot.comment_on_media, args=(cl, account)) | ||
worker_threads.append(comment_thread) | ||
comment_thread.start() | ||
|
||
# Wait for all worker threads to finish | ||
for worker_thread in worker_threads: | ||
worker_thread.join() | ||
logger.info(f'Started process for {username}') | ||
|
||
async with asyncio.TaskGroup() as tg: | ||
if account['follow_users']['enabled']: | ||
cl = instabot.get_client(username, password) | ||
follow_task = tg.create_task( | ||
follow_user_followers(cl, account) | ||
) | ||
if account['unfollow_users']['enabled']: | ||
cl = instabot.get_client(username, password) | ||
unfollow_task = tg.create_task( | ||
unfollow_users(cl, account) | ||
) | ||
if account['comment_on_media']['enabled']: | ||
cl = instabot.get_client(username, password) | ||
comment_task = tg.create_task( | ||
comment_on_media(cl, account) | ||
) | ||
|
||
def run_account(account): | ||
asyncio.run(main(account)) | ||
|
||
if __name__ == "__main__": | ||
config = instabot.load_config('config.json') | ||
|
||
# Get the accounts from the configuration file | ||
accounts = config['accounts'] | ||
|
||
# Create a thread for each account and run the bot | ||
# Create a thread for each account and run the main function | ||
account_threads = [] | ||
for account in accounts: | ||
account_thread = threading.Thread(target=run_bot, args=([account])) | ||
account_thread = Thread(target=run_account, args=(account,)) | ||
account_threads.append(account_thread) | ||
account_thread.start() | ||
|
||
# Wait for all account threads to finish | ||
for account_thread in account_threads: | ||
account_thread.join() | ||
account_thread.join() |