Skip to content

Instantly share code, notes, and snippets.

@rischanlab
Last active August 29, 2015 14:01
Show Gist options
  • Save rischanlab/a0a052e32183185841ef to your computer and use it in GitHub Desktop.
Save rischanlab/a0a052e32183185841ef to your computer and use it in GitHub Desktop.
Unfollow Twitter NonFollowers bulk [python]
def unfollow():
api = setup_api(consumer_key, consumer_secret, access_key, access_secret)
i = 0
while True:
try:
for page in Cursor(api.friends).pages():
print 'page:', i
f = open('out.txt','a')
f.write(str(datetime.now()) + ' Page ' + str(i) + '\n')
f.close()
i += 1
for friend in page:
while True:
try:
flag_unfollow = need_unfollow(api,friend)
if flag_unfollow:
while True:
try:
unfollow(api, friend)
print 'Success unfollow', friend.screen_name
f = open('out.txt','a')
f.write(str(datetime.now()) + ' Success unfollow ' + friend.screen_name + '\n')
f.close()
break
except Exception, e:
print 'Error unfollow.. Time out 60 s', e
f = open('out.txt','a')
f.write(str(datetime.now()) + ' Error unfollow.. Time out 60 s' + str(e) + '\n')
f.close()
time.sleep(60)
continue
except TweepError, e:
print 'Tweep Error need_unfollow.', e, 'Try again'
time.sleep(60)
continue
print 'Keep following', friend.screen_name
f = open('out.txt','a')
f.write(str(datetime.now()) + ' Keep Following ' + friend.screen_name + '\n')
f.close()
break
print 'Slepping (for page)...'
f = open('out.txt','a')
f.write(str(datetime.now()) + ' Slepping (for page) ' + '\n')
f.close()
time.sleep(60)
except TweepError, e:
print '', e
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment