-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmscripting.py
More file actions
98 lines (85 loc) · 3.27 KB
/
smscripting.py
File metadata and controls
98 lines (85 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import time
from dotenv import load_dotenv
from instagrapi import Client as InstaClient
import tweepy
# Load environment variables from .env file
load_dotenv()
# Platform selection (set to "instagram" or "twitter")
PLATFORM = "instagram" # Change to "twitter" for Twitter
# Instagram setup
def instagram_login():
ig_username = os.getenv("IG_USERNAME")
ig_password = os.getenv("IG_PASSWORD")
client = InstaClient()
try:
client.login(ig_username, ig_password)
print("Logged into Instagram successfully!")
return client
except Exception as e:
print(f"Instagram login failed: {e}")
return None
def get_instagram_following_followers(client):
user_id = client.user_id_from_username(client.username)
following = client.user_following(user_id, amount=0) # 0 means fetch all
followers = client.user_followers(user_id, amount=0)
return set(following.keys()), set(followers.keys())
def instagram_unfollow(client, user_id):
client.user_unfollow(user_id)
print(f"Unfollowed user with ID: {user_id}")
# Twitter setup
def twitter_login():
auth = tweepy.OAuthHandler(os.getenv("TW_CONSUMER_KEY"), os.getenv("TW_CONSUMER_SECRET"))
auth.set_access_token(os.getenv("TW_ACCESS_TOKEN"), os.getenv("TW_ACCESS_TOKEN_SECRET"))
api = tweepy.API(auth, wait_on_rate_limit=True)
try:
api.verify_credentials()
print("Logged into Twitter successfully!")
return api
except Exception as e:
print(f"Twitter login failed: {e}")
return None
def get_twitter_following_followers(api):
screen_name = os.getenv("TW_SCREEN_NAME")
following = api.get_friend_ids(screen_name=screen_name)
followers = api.get_follower_ids(screen_name=screen_name)
return set(following), set(followers)
def twitter_unfollow(api, user_id):
api.destroy_friendship(user_id=user_id)
print(f"Unfollowed user with ID: {user_id}")
# Main logic
def unfollow_non_followers(platform):
if platform == "instagram":
client = instagram_login()
if not client:
return
following, followers = get_instagram_following_followers(client)
unfollow_func = instagram_unfollow
api_client = client
elif platform == "twitter":
api = twitter_login()
if not api:
return
following, followers = get_twitter_following_followers(api)
unfollow_func = twitter_unfollow
api_client = api
else:
print("Invalid platform. Use 'instagram' or 'twitter'.")
return
# Find users who don't follow back
non_followers = following - followers
print(f"Found {len(non_followers)} users who don't follow you back.")
# Unfollow with a delay to avoid rate limits
for i, user_id in enumerate(non_followers):
try:
unfollow_func(api_client, user_id)
time.sleep(5) # Delay to avoid hitting rate limits (adjust as needed)
except Exception as e:
print(f"Error unfollowing user {user_id}: {e}")
time.sleep(60) # Longer delay on error
if i % 10 == 0: # Status update every 10 unfollows
print(f"Unfollowed {i+1} users so far...")
print("Unfollowing complete!")
# Run the script
if __name__ == "__main__":
unfollow_non_followers(PLATFORM)