From ca1cec69946f9f3afdbfca7f24114f34212dde58 Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 14:50:49 +0200 Subject: [PATCH 1/8] Add media discovery functionality with auto-discovery and post filtering based on user-defined requirements --- src/instabot/__init__.py | 5 ++-- src/instabot/media_discovery.py | 53 +++++++++++++++++++++++++++++++++ src/main.py | 35 ++++++++++++---------- 3 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 src/instabot/media_discovery.py diff --git a/src/instabot/__init__.py b/src/instabot/__init__.py index 5726e35..534644d 100644 --- a/src/instabot/__init__.py +++ b/src/instabot/__init__.py @@ -1,4 +1,5 @@ -from .utils import get_client, load_config, get_user_id, get_followers, calculate_sleep_time, parse_time_string, logger +from .utils import get_client, load_config, get_user_id, get_followers, calculate_sleep_time, parse_time_string, logger, get_unauthenticated_client from .follow import follow_user_followers, unfollow_users, save_followed_user, load_followed_users, filter_users_to_unfollow, remove_unfollowed_user, mark_unfollowed_user, user_not_followed_before from .like_media import like_recent_posts -from .comment import comment_on_media \ No newline at end of file +from .comment import comment_on_media +from .media_discovery import media_auto_discovery \ No newline at end of file diff --git a/src/instabot/media_discovery.py b/src/instabot/media_discovery.py new file mode 100644 index 0000000..17fca99 --- /dev/null +++ b/src/instabot/media_discovery.py @@ -0,0 +1,53 @@ +import instabot +import asyncio +import langdetect +from instagrapi import Client + +async def media_auto_discovery(account): + config = account['media_auto_discovery'] + get_posts_cl = Client() + posts = get_posts_cl.hashtag_medias_top(config['from_tag']) + + for post in posts: + reqs_check_client = Client() + if passes_requirements(reqs_check_client, post, config): + save_post(account, post) + +async def passes_requirements(cl, post, config): + post = await cl.media_info(post.id) + if post.like_count < config['post_requirements']['min_likes']: + instabot.logger.info(f"Post {post.id} has less than {config['post_requirements']['min_likes']} likes") + return False + if post.comment_count < config['post_requirements']['min_comments']: + instabot.logger.info(f"Post {post.id} has less than {config['post_requirements']['min_comments']} comments") + return False + if config['post_requirements']['detect_caption_language']: + detected_language = langdetect.detect(post.caption_text) + if detected_language not in config['post_requirements']['languages']: + instabot.logger.info(f"Post {post.id} has language {detected_language} not in {config['post_requirements']['languages']}") + return False + if config['author_requirements']['enabled']: + author = await cl.user_info_by_username(post.user.username) + if config['author_requirements']['min_followers'] >= author.follower_count: + instabot.logger.info(f"Author has less than {config['author_requirements']['min_followers']} followers") + return False + if config['author_requirements']['max_following'] <= author.following_count: + instabot.logger.info(f"Author has more than {config['author_requirements']['max_following']} following") + return False + if config['author_requirements']['detect_biography_keywords']: + if not has_keywords(author.biography, config['author_requirements']['biography_keywords']): + instabot.logger.info(f"Author biography does not have keywords in {config['author_requirements']['biography_keywords']}") + return False + if config['author_requirements']['detect_biography_language']: + detected_language = langdetect.detect(author.biography) + if detected_language not in config['author_requirements']['languages']: + instabot.logger.info(f"Author biography has language {detected_language} not in {config['author_requirements']['languages']}") + return False + return True + +def has_keywords(biography, keywords): + biography_lower = biography.lower() + return any(keyword.lower() in biography_lower for keyword in keywords) + +def save_post(account, post): + print("To be implemented") \ No newline at end of file diff --git a/src/main.py b/src/main.py index 13d8209..8b9e638 100644 --- a/src/main.py +++ b/src/main.py @@ -1,8 +1,9 @@ import instabot import asyncio -from instabot import follow_user_followers, unfollow_users, comment_on_media +from instabot import follow_user_followers, unfollow_users, comment_on_media, media_auto_discovery, logger, get_unauthenticated_client from instabot import logger from threading import Thread +from instagrapi import Client async def main(account): username = account['username'] @@ -11,20 +12,24 @@ async def main(account): 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) + # 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) + # ) + if account['media_auto_discovery']['enabled']: + media_task = tg.create_task( + media_auto_discovery(account) ) def run_account(account): From 8f29e5a1aede8ceea8d71ca14e84f7e6c5ea13d3 Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 16:40:57 +0200 Subject: [PATCH 2/8] Refactor media discovery to allow filtering by post type in addition to other requirements --- src/instabot/__init__.py | 2 +- src/instabot/media_discovery.py | 33 ++++++++++++++++++++++++++++++--- src/main.py | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/instabot/__init__.py b/src/instabot/__init__.py index 534644d..cc47d26 100644 --- a/src/instabot/__init__.py +++ b/src/instabot/__init__.py @@ -1,4 +1,4 @@ -from .utils import get_client, load_config, get_user_id, get_followers, calculate_sleep_time, parse_time_string, logger, get_unauthenticated_client +from .utils import get_client, load_config, get_user_id, get_followers, calculate_sleep_time, parse_time_string, logger from .follow import follow_user_followers, unfollow_users, save_followed_user, load_followed_users, filter_users_to_unfollow, remove_unfollowed_user, mark_unfollowed_user, user_not_followed_before from .like_media import like_recent_posts from .comment import comment_on_media diff --git a/src/instabot/media_discovery.py b/src/instabot/media_discovery.py index 17fca99..307e30d 100644 --- a/src/instabot/media_discovery.py +++ b/src/instabot/media_discovery.py @@ -11,7 +11,7 @@ async def media_auto_discovery(account): for post in posts: reqs_check_client = Client() if passes_requirements(reqs_check_client, post, config): - save_post(account, post) + store_post(account, post) async def passes_requirements(cl, post, config): post = await cl.media_info(post.id) @@ -26,6 +26,9 @@ async def passes_requirements(cl, post, config): if detected_language not in config['post_requirements']['languages']: instabot.logger.info(f"Post {post.id} has language {detected_language} not in {config['post_requirements']['languages']}") return False + if not post_type_check(post, config): + instabot.logger.info(f"Post {post.id} has type {post.media_type} not in {config['post_requirements']['allowed_post_types']}") + return False if config['author_requirements']['enabled']: author = await cl.user_info_by_username(post.user.username) if config['author_requirements']['min_followers'] >= author.follower_count: @@ -45,9 +48,33 @@ async def passes_requirements(cl, post, config): return False return True +def get_post_type(post): + if post.media_type == '1': + return 'photo' + elif post.media_type == '2' & post.product_type == 'feed': + return 'video' + elif post.media_type == '2' & post.product_type == 'igtv': + return 'igtv' + elif post.media_type == '8': + return 'album' + + return None + +def post_type_check(post, config): + post_type = get_post_type(post) + allowed_post_types = config['post_requirements']['allowed_post_types'] + allowed_post_types_lower = [post_type.lower() for post_type in allowed_post_types] + if post_type != None: + if allowed_post_types_lower is None: + return True + if post.type not in allowed_post_types_lower: + instabot.logger.info(f"Post {post.id} has type {post.media_type} not in {config['post_requirements']['types']}") + return False + return True + def has_keywords(biography, keywords): biography_lower = biography.lower() return any(keyword.lower() in biography_lower for keyword in keywords) -def save_post(account, post): - print("To be implemented") \ No newline at end of file +def store_post(account, post): + print(post) \ No newline at end of file diff --git a/src/main.py b/src/main.py index 8b9e638..f101484 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,6 @@ import instabot import asyncio -from instabot import follow_user_followers, unfollow_users, comment_on_media, media_auto_discovery, logger, get_unauthenticated_client +from instabot import follow_user_followers, unfollow_users, comment_on_media, media_auto_discovery, logger from instabot import logger from threading import Thread from instagrapi import Client From 9176d3c7b29765d0ef02d9a78db0b9a46d676b45 Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 16:41:03 +0200 Subject: [PATCH 3/8] Update example.config.json --- example.config.json | 81 +++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/example.config.json b/example.config.json index 2b69669..bd0d4ba 100644 --- a/example.config.json +++ b/example.config.json @@ -1,30 +1,63 @@ { - "accounts": [ - { - "username": "username", - "password": "password", - "follow_users": { - "enabled": true, - "follows_per_day": 100, - "source_account": "instagram", - "engagement": { - "like_recent_posts": true, - "like_count": 3 - } - }, - "unfollow_users": { - "enabled": true, - "unfollow_after": "days-hours-minutes-seconds" + "accounts": [ + { + "username": "username", + "password": "password", + "follow_users": { + "enabled": true, + "follows_per_day": 20, + "source_account": "instagram", + "engagement": { + "like_recent_posts": true, + "like_count": 3 + } + }, + "unfollow_users": { + "enabled": true, + "unfollow_after": "0-12-0-0" + }, + "comment_on_media": { + "enabled": true, + "comment_on_tag": "instagram", + "amount_per_day": 50, + "comments": [ + "Comments ..." + ] + }, + "media_auto_discovery": { + "enabled": true, + "from_tag": "instagram", + "amount_per_day": 2, + "save_captions": true, + "avoid_duplicates": true, + "post_requirements": { + "min_likes": 1, + "min_comments": 1, + "detect_caption_language": true, + "languages": ["en"], + "allowed_post_types": [ + /** Empty array means all types **/ + "photo", + "video", + "igtv", + "reel", + "album" + ] }, - "comment_on_media": { + "author_requirements": { "enabled": true, - "comment_on_tag": "hashtag", - "amount_per_day": 5, - "comments": [ - "Comment 1.." - ] + "detect_biography_keywords": false, + "biography_keywords": [ + "keyword", + "another one" + ], + "detect_biography_language": true, + "languages": ["en"], + "min_followers": 1, + "max_following": 1000 } } - ] - } + } + ] +} From 3fa3ba02f4daeb8b9f0f790d741fb14681da397b Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 18:38:00 +0200 Subject: [PATCH 4/8] Update post_discovery to run indefinitely --- .gitignore | 1 + example.config.json | 1 - src/instabot/__init__.py | 2 +- src/instabot/media_discovery.py | 79 +++++++++++++++++++++++---------- 4 files changed, 58 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index e7001e6..129c572 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ session.json /followed_users /comments /src/artifacts/* +/src/saved_posts/ # C extensions *.so diff --git a/example.config.json b/example.config.json index bd0d4ba..f67ce69 100644 --- a/example.config.json +++ b/example.config.json @@ -36,7 +36,6 @@ "detect_caption_language": true, "languages": ["en"], "allowed_post_types": [ - /** Empty array means all types **/ "photo", "video", "igtv", diff --git a/src/instabot/__init__.py b/src/instabot/__init__.py index cc47d26..1fe5445 100644 --- a/src/instabot/__init__.py +++ b/src/instabot/__init__.py @@ -1,4 +1,4 @@ -from .utils import get_client, load_config, get_user_id, get_followers, calculate_sleep_time, parse_time_string, logger +from .utils import next_proxy, get_client, load_config, get_user_id, get_followers, calculate_sleep_time, parse_time_string, logger from .follow import follow_user_followers, unfollow_users, save_followed_user, load_followed_users, filter_users_to_unfollow, remove_unfollowed_user, mark_unfollowed_user, user_not_followed_before from .like_media import like_recent_posts from .comment import comment_on_media diff --git a/src/instabot/media_discovery.py b/src/instabot/media_discovery.py index 307e30d..d492366 100644 --- a/src/instabot/media_discovery.py +++ b/src/instabot/media_discovery.py @@ -2,19 +2,24 @@ import asyncio import langdetect from instagrapi import Client +import os async def media_auto_discovery(account): config = account['media_auto_discovery'] - get_posts_cl = Client() - posts = get_posts_cl.hashtag_medias_top(config['from_tag']) - for post in posts: - reqs_check_client = Client() - if passes_requirements(reqs_check_client, post, config): - store_post(account, post) + client = instabot.get_client(account['username'], account['password']) + + while True: + posts = client.hashtag_medias_top(config['from_tag']) + sleep_time = instabot.calculate_sleep_time(config['amount_per_day']) + for post in posts: + if await passes_requirements(client, post, config): + store_post(client, account, post) + asyncio.sleep(sleep_time) + async def passes_requirements(cl, post, config): - post = await cl.media_info(post.id) + post = cl.media_info(post.id) if post.like_count < config['post_requirements']['min_likes']: instabot.logger.info(f"Post {post.id} has less than {config['post_requirements']['min_likes']} likes") return False @@ -22,15 +27,19 @@ async def passes_requirements(cl, post, config): instabot.logger.info(f"Post {post.id} has less than {config['post_requirements']['min_comments']} comments") return False if config['post_requirements']['detect_caption_language']: - detected_language = langdetect.detect(post.caption_text) - if detected_language not in config['post_requirements']['languages']: - instabot.logger.info(f"Post {post.id} has language {detected_language} not in {config['post_requirements']['languages']}") - return False + try: + detected_language = langdetect.detect(post.caption_text) + if detected_language not in config['post_requirements']['languages']: + instabot.logger.info(f"Post {post.id} has language {detected_language} not in {config['post_requirements']['languages']}") + return False + except: + instabot.logger.info(f"Post {post.id} caption has no language") + return True if not post_type_check(post, config): instabot.logger.info(f"Post {post.id} has type {post.media_type} not in {config['post_requirements']['allowed_post_types']}") return False if config['author_requirements']['enabled']: - author = await cl.user_info_by_username(post.user.username) + author = cl.user_info_by_username(post.user.username) if config['author_requirements']['min_followers'] >= author.follower_count: instabot.logger.info(f"Author has less than {config['author_requirements']['min_followers']} followers") return False @@ -42,20 +51,24 @@ async def passes_requirements(cl, post, config): instabot.logger.info(f"Author biography does not have keywords in {config['author_requirements']['biography_keywords']}") return False if config['author_requirements']['detect_biography_language']: - detected_language = langdetect.detect(author.biography) - if detected_language not in config['author_requirements']['languages']: - instabot.logger.info(f"Author biography has language {detected_language} not in {config['author_requirements']['languages']}") - return False + try: + detected_language = langdetect.detect(author.biography) + if detected_language not in config['author_requirements']['languages']: + instabot.logger.info(f"Author biography has language {detected_language} not in {config['author_requirements']['languages']}") + return False + except: + instabot.logger.info(f"Author biography has no language") + return True return True def get_post_type(post): - if post.media_type == '1': + if post.media_type == 1: return 'photo' - elif post.media_type == '2' & post.product_type == 'feed': + elif post.media_type == 2 and post.product_type == 'feed': return 'video' - elif post.media_type == '2' & post.product_type == 'igtv': + elif post.media_type == 2 and post.product_type == 'igtv': return 'igtv' - elif post.media_type == '8': + elif post.media_type == 8: return 'album' return None @@ -67,7 +80,7 @@ def post_type_check(post, config): if post_type != None: if allowed_post_types_lower is None: return True - if post.type not in allowed_post_types_lower: + if post_type not in allowed_post_types_lower: instabot.logger.info(f"Post {post.id} has type {post.media_type} not in {config['post_requirements']['types']}") return False return True @@ -76,5 +89,25 @@ def has_keywords(biography, keywords): biography_lower = biography.lower() return any(keyword.lower() in biography_lower for keyword in keywords) -def store_post(account, post): - print(post) \ No newline at end of file +def store_post(cl, account, post): + photo_download_path = f"./saved_posts/{account['username']}/photo_downloads" + video_download_path = f"./saved_posts/{account['username']}/video_downloads" + album_download_path = f"./saved_posts/{account['username']}/album_downloads" + if post.media_type == 1: + os.makedirs(photo_download_path, exist_ok=True) + try: + cl.photo_download(post.pk, photo_download_path) + except: + instabot.logger.info(f"Failed to download photo for post {post.id}") + elif post.media_type == 2: + os.makedirs(video_download_path, exist_ok=True) + try: + cl.video_download(post.pk, video_download_path) + except: + instabot.logger.info(f"Failed to download video for post {post.id}") + elif post.media_type == 8: + os.makedirs(album_download_path, exist_ok=True) + try: + cl.album_download(post.pk, album_download_path) + except: + instabot.logger.info(f"Failed to download album for post {post.id}") From 01f4c4e0a856891ef76c0853fb86efb86a010996 Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 18:41:46 +0200 Subject: [PATCH 5/8] Update media_discovery.py --- src/instabot/media_discovery.py | 137 +++++++++++++++++--------------- 1 file changed, 74 insertions(+), 63 deletions(-) diff --git a/src/instabot/media_discovery.py b/src/instabot/media_discovery.py index d492366..31e7a3f 100644 --- a/src/instabot/media_discovery.py +++ b/src/instabot/media_discovery.py @@ -4,6 +4,12 @@ from instagrapi import Client import os +MIN_LIKES = "min_likes" +MIN_COMMENTS = "min_comments" +DETECT_CAPTION_LANGUAGE = "detect_caption_language" +LANGUAGES = "languages" +ALLOWED_POST_TYPES = "allowed_post_types" + async def media_auto_discovery(account): config = account['media_auto_discovery'] @@ -17,50 +23,58 @@ async def media_auto_discovery(account): store_post(client, account, post) asyncio.sleep(sleep_time) - async def passes_requirements(cl, post, config): + return (await check_post_requirements(cl, post, config) and + await check_author_requirements(cl, post, config)) + +async def check_post_requirements(cl, post, config): + post_req = config['post_requirements'] post = cl.media_info(post.id) - if post.like_count < config['post_requirements']['min_likes']: - instabot.logger.info(f"Post {post.id} has less than {config['post_requirements']['min_likes']} likes") + + if post.like_count < post_req[MIN_LIKES] or post.comment_count < post_req[MIN_COMMENTS]: return False - if post.comment_count < config['post_requirements']['min_comments']: - instabot.logger.info(f"Post {post.id} has less than {config['post_requirements']['min_comments']} comments") + + if post_req[DETECT_CAPTION_LANGUAGE]: + if not is_language_allowed(post.caption_text, post_req[LANGUAGES]): + return False + + if not is_post_type_allowed(post, post_req[ALLOWED_POST_TYPES]): return False - if config['post_requirements']['detect_caption_language']: - try: - detected_language = langdetect.detect(post.caption_text) - if detected_language not in config['post_requirements']['languages']: - instabot.logger.info(f"Post {post.id} has language {detected_language} not in {config['post_requirements']['languages']}") - return False - except: - instabot.logger.info(f"Post {post.id} caption has no language") - return True - if not post_type_check(post, config): - instabot.logger.info(f"Post {post.id} has type {post.media_type} not in {config['post_requirements']['allowed_post_types']}") + + return True + +async def check_author_requirements(cl, post, config): + author_req = config['author_requirements'] + if not author_req['enabled']: + return True + + author = cl.user_info_by_username(post.user.username) + + if (author_req['min_followers'] >= author.follower_count or + author_req['max_following'] <= author.following_count): return False - if config['author_requirements']['enabled']: - author = cl.user_info_by_username(post.user.username) - if config['author_requirements']['min_followers'] >= author.follower_count: - instabot.logger.info(f"Author has less than {config['author_requirements']['min_followers']} followers") - return False - if config['author_requirements']['max_following'] <= author.following_count: - instabot.logger.info(f"Author has more than {config['author_requirements']['max_following']} following") + + if author_req['detect_biography_keywords']: + if not has_keywords(author.biography, author_req['biography_keywords']): return False - if config['author_requirements']['detect_biography_keywords']: - if not has_keywords(author.biography, config['author_requirements']['biography_keywords']): - instabot.logger.info(f"Author biography does not have keywords in {config['author_requirements']['biography_keywords']}") + + if author_req['detect_biography_language']: + if not is_language_allowed(author.biography, author_req[LANGUAGES]): return False - if config['author_requirements']['detect_biography_language']: - try: - detected_language = langdetect.detect(author.biography) - if detected_language not in config['author_requirements']['languages']: - instabot.logger.info(f"Author biography has language {detected_language} not in {config['author_requirements']['languages']}") - return False - except: - instabot.logger.info(f"Author biography has no language") - return True + return True +def is_language_allowed(text, allowed_languages): + try: + detected_language = langdetect.detect(text) + return detected_language in allowed_languages + except: + return True + +def is_post_type_allowed(post, allowed_post_types): + post_type = get_post_type(post) + return post_type is None or post_type.lower() in allowed_post_types + def get_post_type(post): if post.media_type == 1: return 'photo' @@ -70,21 +84,8 @@ def get_post_type(post): return 'igtv' elif post.media_type == 8: return 'album' - return None -def post_type_check(post, config): - post_type = get_post_type(post) - allowed_post_types = config['post_requirements']['allowed_post_types'] - allowed_post_types_lower = [post_type.lower() for post_type in allowed_post_types] - if post_type != None: - if allowed_post_types_lower is None: - return True - if post_type not in allowed_post_types_lower: - instabot.logger.info(f"Post {post.id} has type {post.media_type} not in {config['post_requirements']['types']}") - return False - return True - def has_keywords(biography, keywords): biography_lower = biography.lower() return any(keyword.lower() in biography_lower for keyword in keywords) @@ -93,21 +94,31 @@ def store_post(cl, account, post): photo_download_path = f"./saved_posts/{account['username']}/photo_downloads" video_download_path = f"./saved_posts/{account['username']}/video_downloads" album_download_path = f"./saved_posts/{account['username']}/album_downloads" + if post.media_type == 1: - os.makedirs(photo_download_path, exist_ok=True) - try: - cl.photo_download(post.pk, photo_download_path) - except: - instabot.logger.info(f"Failed to download photo for post {post.id}") + download_photo(cl, post, photo_download_path) elif post.media_type == 2: - os.makedirs(video_download_path, exist_ok=True) - try: - cl.video_download(post.pk, video_download_path) - except: - instabot.logger.info(f"Failed to download video for post {post.id}") + download_video(cl, post, video_download_path) elif post.media_type == 8: - os.makedirs(album_download_path, exist_ok=True) - try: - cl.album_download(post.pk, album_download_path) - except: - instabot.logger.info(f"Failed to download album for post {post.id}") + download_album(cl, post, album_download_path) + +def download_photo(cl, post, photo_download_path): + os.makedirs(photo_download_path, exist_ok=True) + try: + cl.photo_download(post.pk, photo_download_path) + except: + instabot.logger.info(f"Failed to download photo for post {post.id}") + +def download_video(cl, post, video_download_path): + os.makedirs(video_download_path, exist_ok=True) + try: + cl.video_download(post.pk, video_download_path) + except: + instabot.logger.info(f"Failed to download video for post {post.id}") + +def download_album(cl, post, album_download_path): + os.makedirs(album_download_path, exist_ok=True) + try: + cl.album_download(post.pk, album_download_path) + except: + instabot.logger.info(f"Failed to download album for post {post.id}") \ No newline at end of file From 834df0a1140a42d3dbd1337c5176cea63e5f6d99 Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 18:44:58 +0200 Subject: [PATCH 6/8] Update media_discovery.py --- src/instabot/media_discovery.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/instabot/media_discovery.py b/src/instabot/media_discovery.py index 31e7a3f..a9e7bac 100644 --- a/src/instabot/media_discovery.py +++ b/src/instabot/media_discovery.py @@ -13,6 +13,9 @@ async def media_auto_discovery(account): config = account['media_auto_discovery'] + ### TODO: Check if unauthenticated IG account proxy requests are working + ### If they are not working, use authenticated IG account for proxy + client = instabot.get_client(account['username'], account['password']) while True: From ed58b992debc2a80248a21983d3f6ac030e94c86 Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 18:48:47 +0200 Subject: [PATCH 7/8] Refactor media_auto_discovery function to use await when sleeping --- src/instabot/media_discovery.py | 2 +- src/main.py | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/instabot/media_discovery.py b/src/instabot/media_discovery.py index a9e7bac..41d4e09 100644 --- a/src/instabot/media_discovery.py +++ b/src/instabot/media_discovery.py @@ -24,7 +24,7 @@ async def media_auto_discovery(account): for post in posts: if await passes_requirements(client, post, config): store_post(client, account, post) - asyncio.sleep(sleep_time) + await asyncio.sleep(sleep_time) async def passes_requirements(cl, post, config): return (await check_post_requirements(cl, post, config) and diff --git a/src/main.py b/src/main.py index f101484..8f80d43 100644 --- a/src/main.py +++ b/src/main.py @@ -12,21 +12,21 @@ async def main(account): 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) - # ) + 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) + ) if account['media_auto_discovery']['enabled']: media_task = tg.create_task( media_auto_discovery(account) From d13234eca5c4df1dc1d42f181bc57b678ed21b2a Mon Sep 17 00:00:00 2001 From: peer Date: Thu, 27 Apr 2023 18:54:29 +0200 Subject: [PATCH 8/8] Refactor media_auto_discovery function to accept client as parameter --- src/instabot/media_discovery.py | 7 +------ src/main.py | 5 ++++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/instabot/media_discovery.py b/src/instabot/media_discovery.py index 41d4e09..b599358 100644 --- a/src/instabot/media_discovery.py +++ b/src/instabot/media_discovery.py @@ -10,14 +10,9 @@ LANGUAGES = "languages" ALLOWED_POST_TYPES = "allowed_post_types" -async def media_auto_discovery(account): +async def media_auto_discovery(client, account): config = account['media_auto_discovery'] - ### TODO: Check if unauthenticated IG account proxy requests are working - ### If they are not working, use authenticated IG account for proxy - - client = instabot.get_client(account['username'], account['password']) - while True: posts = client.hashtag_medias_top(config['from_tag']) sleep_time = instabot.calculate_sleep_time(config['amount_per_day']) diff --git a/src/main.py b/src/main.py index 8f80d43..a6426bd 100644 --- a/src/main.py +++ b/src/main.py @@ -28,8 +28,11 @@ async def main(account): comment_on_media(cl, account) ) if account['media_auto_discovery']['enabled']: + ### TODO: Check if unauthenticated IG account proxy requests are working + ### If they are not working, use authenticated IG account for proxy + cl = instabot.get_client(account['username'], account['password']) media_task = tg.create_task( - media_auto_discovery(account) + media_auto_discovery(cl, account) ) def run_account(account):