Untitled
Anonymous
Plain Text
08/18/2023 7:15 AM
4.9 KB
256
No Index
Never
from datetime import datetime, time as dt_time
from instagram_private_api import Client
import random
import time
import ssl
import certifi
import instaloader
import sys
# Ignore SSL certificate verification
ssl._create_default_https_context = ssl._create_unverified_context
# Replace with your Instagram credentials
INSTALOADER_USERNAME = 'user'
INSTALOADER_PASSWORD = 'pass'
INSTAGRAM_PRIVATE_API_USERNAME = 'user'
INSTAGRAM_PRIVATE_API_PASSWORD = 'pass'
# Path to the file containing usernames to block
USERNAMES_TO_BLOCK_FILE = 'usernames_to_block.txt'
BLOCKED_USERS_FILE = 'blocked_users.txt'
def read_usernames_to_block(filename):
with open(filename, 'r') as file:
usernames = [line.strip() for line in file]
return usernames
def read_blocked_users(filename):
try:
with open(filename, 'r') as file:
blocked_users = [line.strip() for line in file]
return blocked_users
except FileNotFoundError:
return []
def is_within_hours(start_hour, end_hour):
current_hour = datetime.now().time().hour
return start_hour <= current_hour < end_hour
def main():
# Authenticate using instaloader
L = instaloader.Instaloader()
L.context.log("Logging in.")
L.context.log("Session file does not exist yet - Logging in.")
L.load_session_from_file(INSTALOADER_USERNAME) # Load session if available
if not L.context.is_logged_in:
L.interactive_login(INSTALOADER_USERNAME, INSTALOADER_PASSWORD)
# Create an instance of Instagram Private API
api = Client(auto_patch=True, authenticate=True, username=INSTAGRAM_PRIVATE_API_USERNAME,
password=INSTAGRAM_PRIVATE_API_PASSWORD, request_timeout=60, ca_bundle_path=certifi.where())
usernames_to_block = read_usernames_to_block(USERNAMES_TO_BLOCK_FILE)
blocked_accounts = read_blocked_users(BLOCKED_USERS_FILE)
total_blocked = len(blocked_accounts)
block_limit = 100 # Maximum block actions per day
min_blocks = 80 # Minimum number of blocks per day
max_blocks = 100 # Maximum number of blocks per day
target_blocks = random.randint(min_blocks, max_blocks)
block_count = 0
base_start_hour = 8 # Base start hour (8 AM)
base_end_hour = 23 # Base end hour (11 PM)
# Introduce a random deviation of up to 3 hours (10800 seconds)
start_hour_deviation = random.randint(-10800, 10800)
end_hour_deviation = random.randint(-10800, 10800)
start_hour = max(0, base_start_hour + start_hour_deviation // 3600) # Ensure start hour is >= 0
end_hour = min(24, base_end_hour + end_hour_deviation // 3600) # Ensure end hour is <= 24
print(f"Target blocks: {target_blocks}")
while block_count < target_blocks and len(usernames_to_block) > 0:
if is_within_hours(start_hour, end_hour):
try:
username = usernames_to_block[0] # Get the first username in the list
if username not in blocked_accounts: # Skip if user is already blocked
try:
user_info = api.username_info(username)['user']
user_id = user_info['pk']
api.friendships_block(user_id)
blocked_accounts.append(username)
block_count += 1
total_blocked += 1
print(f"Blocked: {username} (Total blocked today: {total_blocked}/{target_blocks})")
# Update the list of blocked accounts in the file
with open(BLOCKED_USERS_FILE, 'a') as file:
file.write(username + '\n')
except Exception as e:
print(f"Error blocking user {username}: {e}")
# Remove blocked user from the list
usernames_to_block.pop(0)
random_delay = random.randint(180, 540) # Random delay between 3 to 9 minutes
for remaining in range(random_delay, 0, -1):
sys.stdout.write(f"\rNext action in {remaining} seconds... ")
sys.stdout.flush()
time.sleep(1)
print() # Newline after countdown
except Exception as e:
print(f"Error processing user {username}: {e}")
random_delay = random.randint(180, 540) # Random delay between 3 to 9 minutes
for remaining in range(random_delay, 0, -1):
sys.stdout.write(f"\rNext action in {remaining} seconds... ")
sys.stdout.flush()
time.sleep(1)
print() # Newline after countdown
else:
print("Outside of scheduled hours. Taking a break.")
time.sleep(1800) # 30 minutes break
print("Script finished. Blocked accounts:")
for account in blocked_accounts:
print(account)
if __name__ == "__main__":
main()
Editor is loading...