Dejar de seguir a quienes no te siguen en Twitter con este script de Python.
Hace unas semanas hice un script en Python que permitía saber quienes no te seguían pero con el cambio de la API a la versión 1.1 quedo obsoleto.
Entre las peticiones que he recibido aparte de ponerlo a funcionar con la API nueva era para dejar de seguir a esos que no te
seguían.
El siguiente script no te dice los nombres de quienes no te siguen como lo hacia el que mencione anteriormente pero cuenta cuantos de los que tu sigues no te siguen y los deja de seguir. Antes de ejecutarlo les recomiendo que organicen las cuentas que ustedes siguen y que tal vez no los sigan en listas, por ejemplo crear una lista con todas sus fuentes de noticia o una de robots y comedia, etc.
Este es el script:
import requests
from requests_oauthlib import OAuth1
import re
from time import sleep
import operator
import sys
import os
import collections
consumer_key=''
consumer_secret=''
access_token_key=''
access_token_secret=''
def get_follower_ids():
cursor = "-1"
listadeIDs = []
while cursor != '0':
try:
api_url='https://api.twitter.com/1.1/followers/ids.json'
payload = {'count':'5000', 'cursor':cursor}
auth = OAuth1(consumer_key, consumer_secret, access_token_key, access_token_secret)
r = requests.get(api_url, stream=False, auth=auth, params=payload)
if r.headers['x-rate-limit-remaining'] and r.headers['x-rate-limit-remaining'] == "0":
time_out = int(r.headers["x-rate-limit-reset"]) - int(time.time())
print("We reached rate limit for ", api_url)
print "Try again in", time_out, "seconds"
quit()
IDs = json.loads(r.content)
cursor = IDs['next_cursor_str']
listadeIDs = listadeIDs + IDs['ids']
sleep(1)
except KeyError:
print "Unable to navigate through cursors, last attempt: ", cursor
break
return list(set(listadeIDs))
def get_friends_ids():
cursor = "-1"
listadeIDs = []
while cursor != '0':
try:
api_url='https://api.twitter.com/1.1/friends/ids.json'
payload = {'count':'5000', 'cursor':cursor}
auth = OAuth1(consumer_key, consumer_secret, access_token_key, access_token_secret)
r = requests.get(api_url, stream=False, auth=auth, params=payload)
if r.headers['x-rate-limit-remaining'] and r.headers['x-rate-limit-remaining'] == "0":
print("We reached rate limit for ", api_url)
print("Try again at", r.headers["x-rate-limit-reset"])
quit()
IDs = json.loads(r.content)
cursor = IDs['next_cursor_str']
listadeIDs = listadeIDs + IDs['ids']
sleep(1)
except KeyError:
print "Unable to navigate through cursors, last attempt: ", cursor
break
return list(set(listadeIDs))
def get_unfollowers_info(unfollowers):
print len(unfollowers)
api_url='https://api.twitter.com/1.1/users/lookup.json'
auth = OAuth1(consumer_key, consumer_secret, access_token_key, access_token_secret)
user_objs = {}
sleep(1)
i = 0
user_id = []
for i in unfollowers:
user_id.append(i)
if (len(user_id) == 100):
payload = {'user_id':user_id}
r = requests.get(api_url, stream=False, auth=auth, params=payload)
if r.headers['x-rate-limit-remaining'] and r.headers['x-rate-limit-remaining'] == "0":
print("We reached rate limit for ", api_url)
print("Try again at", r.headers["x-rate-limit-reset"])
quit()
tmp_user_objs = json.loads(r.content)
for y in range(len(tmp_user_objs)):
print tmp_user_objs[y]['screen_name'] , tmp_user_objs[y]['followers_count'] , tmp_user_objs[y]['friends_count'] , tmp_user_objs[y]['following'] , tmp_user_objs[y]['verified'], tmp_user_objs[y]['default_profile']
user_objs = user_objs , tmp_user_objs
user_id = []
payload = {'user_id':user_id}
r = requests.get(api_url, stream=False, auth=auth, params=payload)
if r.headers['x-rate-limit-remaining'] and r.headers['x-rate-limit-remaining'] == "0":
print("We reached rate limit for ", api_url)
print("Try again at", r.headers["x-rate-limit-reset"])
quit()
tmp_user_objs = json.loads(r.content)
user_objs = user_objs , tmp_user_objs
def unfollow_by_id(user_id):
api_url='https://api.twitter.com/1.1/friendships/destroy.json'
payload = {'user_id':user_id}
auth = OAuth1(consumer_key, consumer_secret, access_token_key, access_token_secret)
r = requests.post(api_url, stream=False, auth=auth, params=payload)
followers = get_follower_ids()
print "Followers: ", len(followers)
following = get_friends_ids()
print "Following: ", len(following)
unfollower_ids = set(following) - set(followers)
print "unfollower ids: ", unfollower_ids , len(unfollower_ids)
get_unfollowers_info(unfollower_ids)
print "About to unfollow: " , len(unfollower_ids) , "users."
sleep(10)
for user_id in unfollower_ids:
print "Unfollowing ", user_id
unfollow_by_id(user_id)
En este script a diferencia de los anteriores que he hecho no use el módulo python-twitter y como ven intento trabajar con el API y su rate-limit
En mi caso al ejecutarlo deje de seguir a algunas cuentas de comedia, noticias y cuentas interesantes que enriquecían mi timeline.
Si el tiempo me lo permite, en la próxima versión creo que voy a agregar esos usuarios a una lista privada antes de dejarlos de seguir.
He trabajado en otros scripts privados en calcular la importancia de una cuenta en base a la cantidad de seguidores, cantidad de personas que esta sigue, cantidad de tweets, edad de la cuenta y otros factores que tal vez incorpore en un futuro.
Como siempre en todos mis publicaciones que tiene que ver con Python, les agradecería mucho si me dan consejos de como mejorar este script o la lógica del mismo, Incluso pull requests al repo github.com/orvtech/Python-tools-for-twitter/blob/master/unfollow-unfollowers.py.