Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. from twython import Twython
  2. import codecs
  3. import sys
  4. import time
  5. from datetime import timedelta, datetime
  6. from time import mktime
  7.  
  8. #application and user key
  9. APP_KEY = ' '
  10. APP_SECRET = ' '
  11. OAUTH_TOKEN = ' '
  12. OAUTH_TOKEN_SECRET = ' '
  13.  
  14. #your display name
  15. account = ' '
  16.  
  17. #limit for last tweet, for example remove all those who have not tweeted since 28 October 2014
  18. l = '28 Oct 2014'
  19.  
  20. li = time.strptime(l, "%d %b %Y")
  21. lim = datetime.fromtimestamp(mktime(li))
  22. today = datetime.now()
  23. limi = today - lim
  24.  
  25. #limit time in seconds (timedelta.total_seconds() works fine in python 2.7, but not in 2.6)
  26. limit = limi.seconds + limi.days * 24 * 3600
  27.  
  28. #log file with current datetime
  29. logfile = 'log_' + today.strftime("%H-%M_%d-%m-%y") + '.txt'
  30.  
  31. #save string and print it
  32. def scrivi(stringa):
  33.     with codecs.open(logfile, "a", encoding="utf-8") as f:
  34.         f.write(stringa + '\n')
  35.         f.close()
  36.     print stringa
  37.     return
  38.  
  39.  
  40.  
  41. #interface
  42. twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
  43.  
  44. curs = -1
  45. pag = 1
  46. following_all = []
  47.  
  48. #we need to do multiple friends list request, because there is a limit of 200 per page
  49. #cursor = -1 means first page, cursor = 0 means last page, page N contains N+1 cursor
  50. while curs != 0:
  51.     #get the list of following
  52.     f = twitter.get_friends_list(screen_name = account, count = 200, cursor = curs)
  53.     following_all.append(f)
  54.     scrivi('You have ' + str(len(f['users'])) + ' following at page ' + str(pag) + '\n')
  55.     curs = f['next_cursor']
  56.     pag = pag + 1
  57.    
  58.  
  59. n = 1
  60.  
  61. for following in following_all:              
  62.     for utente in following['users']:
  63.         #request limit is 180/15min
  64.         time.sleep(6)
  65.        
  66.         #detail of current user
  67.         scrivi(utente['name'] + ' - ' + utente['id_str'] + ' N:' + str(n))
  68.         try:
  69.             #get last tweet of the user
  70.             tweet = twitter.get_user_timeline(user_id= utente['id'], count=1)
  71.            
  72.             #date of the last tweet
  73.             date = tweet[0]['created_at']
  74.      
  75.             scrivi(date + '\n')
  76.             las = time.strptime(date, "%a %b %d %H:%M:%S +0000 %Y")
  77.             last = datetime.fromtimestamp(mktime(las))
  78.            
  79.             #seconds from last tweet
  80.             delta = today - last
  81.             sec = delta.seconds + delta.days * 24 * 3600
  82.            
  83.             if sec > limit:
  84.                 scrivi('INACTIVE\n')
  85.                 try:
  86.                     #removing following
  87.                     rem = twitter.destroy_friendship(user_id = utente['id'])
  88.                    
  89.                     #twitter return a json object for success and a string for fail
  90.                     if isinstance(rem, str):
  91.                         scrivi('Removing failed for ignote reason\n')
  92.                     else:
  93.                         scrivi('Removed\n')
  94.                    
  95.                 except:
  96.                     scrivi('Removing failed for problem with api or network\n')
  97.            
  98.         except:
  99.             for ii in sys.exc_info():
  100.                 print ii
  101.             scrivi('Get last tweet failed for problem with api or network\n')
  102.        
  103.         n = n + 1