Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twython import Twython
- import codecs
- import sys
- import time
- from datetime import timedelta, datetime
- from time import mktime
- #application and user key
- APP_KEY = ' '
- APP_SECRET = ' '
- OAUTH_TOKEN = ' '
- OAUTH_TOKEN_SECRET = ' '
- #your display name
- account = ' '
- #limit for last tweet, for example remove all those who have not tweeted since 28 October 2014
- l = '28 Oct 2014'
- li = time.strptime(l, "%d %b %Y")
- lim = datetime.fromtimestamp(mktime(li))
- today = datetime.now()
- limi = today - lim
- #limit time in seconds (timedelta.total_seconds() works fine in python 2.7, but not in 2.6)
- limit = limi.seconds + limi.days * 24 * 3600
- #log file with current datetime
- logfile = 'log_' + today.strftime("%H-%M_%d-%m-%y") + '.txt'
- #save string and print it
- def scrivi(stringa):
- with codecs.open(logfile, "a", encoding="utf-8") as f:
- f.write(stringa + '\n')
- f.close()
- print stringa
- return
- #interface
- twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
- curs = -1
- pag = 1
- following_all = []
- #we need to do multiple friends list request, because there is a limit of 200 per page
- #cursor = -1 means first page, cursor = 0 means last page, page N contains N+1 cursor
- while curs != 0:
- #get the list of following
- f = twitter.get_friends_list(screen_name = account, count = 200, cursor = curs)
- following_all.append(f)
- scrivi('You have ' + str(len(f['users'])) + ' following at page ' + str(pag) + '\n')
- curs = f['next_cursor']
- pag = pag + 1
- n = 1
- for following in following_all:
- for utente in following['users']:
- #request limit is 180/15min
- time.sleep(6)
- #detail of current user
- scrivi(utente['name'] + ' - ' + utente['id_str'] + ' N:' + str(n))
- try:
- #get last tweet of the user
- tweet = twitter.get_user_timeline(user_id= utente['id'], count=1)
- #date of the last tweet
- date = tweet[0]['created_at']
- scrivi(date + '\n')
- las = time.strptime(date, "%a %b %d %H:%M:%S +0000 %Y")
- last = datetime.fromtimestamp(mktime(las))
- #seconds from last tweet
- delta = today - last
- sec = delta.seconds + delta.days * 24 * 3600
- if sec > limit:
- scrivi('INACTIVE\n')
- try:
- #removing following
- rem = twitter.destroy_friendship(user_id = utente['id'])
- #twitter return a json object for success and a string for fail
- if isinstance(rem, str):
- scrivi('Removing failed for ignote reason\n')
- else:
- scrivi('Removed\n')
- except:
- scrivi('Removing failed for problem with api or network\n')
- except:
- for ii in sys.exc_info():
- print ii
- scrivi('Get last tweet failed for problem with api or network\n')
- n = n + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement