Have you ever wondered if your account is missing follow-ups or if you don't follow back? This time, I implemented a program that can display all accounts in one follow (I wonder?) State with Twitter API and Python! (There may be many apps that can be followed and managed, but please watch with warm eyes because it is a practice of Python lol)
It is like this
Getting the id and name of the user you are following...
Processd: 100.00 %
Done
Getting follower id and name...
Processd: 100.00 %
Done
Checking one follow...
Batch display of users who do not follow back
5 sample1
Name: name, dtype: object
Batch display of users who have followed but have not followed back
0 sample2
Name: name, dtype: object
Done
--The output is the same as the DataFrame of pandas. If you want to save csv, please modify the code by yourself.
--Twitter API: How to apply for use is introduced in various articles, so please refer to that. (** It is very important that you need to get a total of 4 types of API Key and Accesee Token. **)
--tweepy: You can download it with pip install tweepy
--Python3 series
import tweepy
import pandas as pd
import time
def authorize():
Consumer_key = 'My API Key'
Consumer_secret = 'My API Secret Key'
Access_token = 'My Access Token'
Access_secret = 'Your Access Secret Token'
authorization = [Consumer_key, Consumer_secret, Access_token, Access_secret]
return authorization
def get_myaccount_data(authorization):
auth = tweepy.OAuthHandler(authorization[0], authorization[1])
auth.set_access_token(authorization[2], authorization[3])
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
def get_friends_id(api):
print("Getting the id and name of the user you are following...")
df_friends = pd.DataFrame(columns=["id","name"])
process_id = len(api.friends_ids())
for i, user_id in enumerate(api.friends_ids()[:6]):
if (i+1) % 5 == 0:
print("Processd: ", round(((i+1)/process_id)*100,2),"%")
time.sleep(1) #It may not be necessary. Uncomment if there are many people
user = api.get_user(user_id)
user_info = [user.id_str, user.name]
df_friends.loc[i,"id"] = user_info[0]
df_friends.loc[i,"name"] = user_info[1]
print("Done")
print("\n")
df_friends.to_csv("./friends_ids.csv", index=False, encoding='utf-8-sig')
return df_friends
def get_followers_id(api):
print("Getting follower id and name...")
df_followers = pd.DataFrame(columns=["id","name"])
process_id = len(api.followers_ids())
for i, user_id in enumerate(api.followers_ids()[:6]):
if (i+1) % 5 == 0:
print("Processd: ", round(((i+1)/process_id)*100,2),"%")
time.sleep(1) #It may not be necessary. Uncomment if there are many people
user = api.get_user(user_id)
user_info = [user.id_str, user.name]
df_followers.loc[i,"id"] = user_info[0]
df_followers.loc[i,"name"] = user_info[1]
print("Done")
print("\n")
df_followers.to_csv("./followers_ids.csv", index=False, encoding='utf-8-sig')
return df_followers
def check_onesided_follow(df_friends, df_followers):
print("Checking one follow...")
#Check one follow by converting to set and giving the set difference
friends = set(df_friends["id"])
followers = set(df_followers["id"])
# 1.Batch display of users who do not follow back
diff1 = friends.difference(followers)
diff1_list = list(diff1)
print("Batch display of users who do not follow back\n")
print(df_friends['name'][df_friends['id'].isin(diff1_list)])
# 2.Batch display of users who have followed but have not followed back
diff2 = followers.difference(friends)
diff2_list = list(diff2)
print("Batch display of users who have followed but have not followed back\n")
print(df_followers['name'][df_followers['id'].isin(diff2_list)])
print("Done")
print("\n")
def main():
#Consumer key and access token setting for using Twitter API
authorization = authorize()
#Get your account information with API
api = get_myaccount_data(authorization)
#friend(The account I followed)Get id and name of
df_friends = get_friends_id(api)
#Get follower id and name all at once
df_followers = get_followers_id(api)
#Follow one(I followed but did not follow back or followed me but I did not follow)of
#Batch display of user names with print statement
check_onesided_follow(df_friends, df_followers)
if __name__ == "__main__":
main()
It's easy if you use the API ~
The contents are almost the same, but it is also listed on GitHub, so please check it if you like. https://github.com/kkkodai/check-onesided-follwer-in-twitter
Recommended Posts