Skip to content

Commit

Permalink
Add ability to parse time strings for unfollowing users in Instabot
Browse files Browse the repository at this point in the history
  • Loading branch information
birdhouses committed Apr 26, 2023
1 parent 63fccfc commit 2c72329
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion example.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"unfollow_users": {
"enabled": true,
"unfollow_after_days": 1
"unfollow_after": "days-hours-minutes-seconds"
},
"comment_on_media": {
"enabled": true,
Expand Down
2 changes: 1 addition & 1 deletion instabot/__init__.py
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, logger
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
11 changes: 7 additions & 4 deletions instabot/follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .like_media import like_recent_posts
from .utils import load_config, calculate_sleep_time
import asyncio
import instabot

logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
Expand Down Expand Up @@ -47,11 +48,12 @@ def load_followed_users(cl: Client) -> List[Tuple[int, datetime.datetime]]:

return followed_users

def filter_users_to_unfollow(followed_users: List[Tuple[int, datetime.datetime]], follow_time: int) -> List[int]:
def filter_users_to_unfollow(followed_users: List[Tuple[int, datetime.datetime]], follow_time: str) -> List[int]:
"""Filter users that should be unfollowed based on follow_time."""
now = datetime.datetime.now()
follow_time = follow_time * 86400
return [user for user, timestamp, *unfollow_timestamp in followed_users if (now - timestamp).total_seconds() >= follow_time and not unfollow_timestamp]
follow_time_seconds = instabot.parse_time_string(follow_time)
return [user for user, timestamp, *unfollow_timestamp in followed_users if (now - timestamp).total_seconds() >= follow_time_seconds and not unfollow_timestamp]


def remove_unfollowed_user(cl: Client, user: int) -> None:
"""Remove unfollowed user from the followed users file."""
Expand Down Expand Up @@ -79,9 +81,10 @@ def mark_unfollowed_user(cl: Client, user_id: int) -> None:
for user_info in followed_users:
file.write(",".join(str(x) for x in user_info) + "\n")

async def unfollow_users(cl: Client, unfollow_after: int) -> None:
async def unfollow_users(cl: Client, account: Dict[str, Any]) -> None:
"""Unfollow users after a specified time."""
logger.info("Started unfollowing process")
unfollow_after = account["unfollow_users"]["unfollow_after"]
followed_users = load_followed_users(cl)
users_to_unfollow = filter_users_to_unfollow(followed_users, follow_time=unfollow_after)
unfollow_users_count = len(users_to_unfollow)
Expand Down
11 changes: 9 additions & 2 deletions instabot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import os.path
import logging
import random
import requests
from dateutil import parser

logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
Expand Down Expand Up @@ -146,4 +146,11 @@ def calculate_sleep_time(actions_per_day: int) -> float:
average_sleep_time = 86400 / actions_per_day
min_sleep_time = average_sleep_time * 0.5
max_sleep_time = average_sleep_time * 1.5
return random.uniform(min_sleep_time, max_sleep_time)
return random.uniform(min_sleep_time, max_sleep_time)

def parse_time_string(time_string: str) -> int:
"""Parse the time string in the format 'day-hour-min-s' into total seconds."""
parts = time_string.split('-')
days, hours, minutes, seconds = [int(part) for part in parts]
total_seconds = days * 86400 + hours * 3600 + minutes * 60 + seconds
return total_seconds
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def main(account):
if account['unfollow_users']['enabled']:
cl = instabot.get_client(username, password)
unfollow_task = tg.create_task(
unfollow_users(cl, account['unfollow_users']['unfollow_after_days'])
unfollow_users(cl, account)
)
if account['comment_on_media']['enabled']:
cl = instabot.get_client(username, password)
Expand Down

0 comments on commit 2c72329

Please sign in to comment.