import json
from twitter.scraper import Scraper
from twitter.account import Account
import time
email, username, password = "xxx", "xxx", "xxxx"
scraper = Scraper(email, username, password)
account = Account(email, username, password)
limit = 1
user_id = 1420528591511728134
cursor = None
def extract_all_rest_ids(data):
rest_ids = []
if isinstance(data, dict):
for key, value in data.items():
if key == 'rest_id':
rest_ids.append(value)
else:
rest_ids.extend(extract_all_rest_ids(value))
elif isinstance(data, list):
for item in data:
rest_ids.extend(extract_all_rest_ids(item))
return rest_ids
def extract_first_value(data):
if isinstance(data, dict):
for key, value in data.items():
if key == 'value':
return value
result = extract_first_value(value)
if result is not None:
return result
elif isinstance(data, list):
for item in data:
result = extract_first_value(item)
if result is not None:
return result
return None
# Load initial json_data from "fix.txt"
with open('fix.txt', 'r') as file:
json_data = json.load(file)
all_rest_ids = extract_all_rest_ids(json_data)
while True:
follower_subset = scraper.followers([user_id], limit=limit, cursor=cursor)
follower_subset_json = json.dumps(follower_subset)
# Extract the "value" from the current JSON data
cursor = extract_first_value(follower_subset)
print(cursor)
# Specify the file path
file_path = "fix.txt"
# Open the file in write mode and write the JSON data
with open(file_path, "w") as file:
file.write(follower_subset_json)
# Print a message to confirm the data was saved
print(f"Follower data saved to {file_path}")
with open('ids.txt', 'a') as ids_file: # Open in append mode
for line in all_rest_ids:
ids_file.write(str(line) + '\n') # Append the new ID to the file
with open('ids.txt', 'r') as ids_file:
for line in ids_file:
user_id_to_follow = line.strip() # Remove newline character if present
if not user_id_to_follow:
continue # Skip empty lines
print(f"Following user ID: {user_id_to_follow}")
# Follow the user
account.follow(int(user_id_to_follow)) # Convert the user ID to an integer
time.sleep(30)