Skip to content

Instantly share code, notes, and snippets.

@faried
Created February 4, 2016 17:53
Show Gist options
  • Save faried/60818a1aa3ac0f394339 to your computer and use it in GitHub Desktop.
Save faried/60818a1aa3ac0f394339 to your computer and use it in GitHub Desktop.
migrating to a new twitter account
if you want to change your twitter screen_name, use https://twitter.com/settings/account
if you want to move to a new account but don't want to give up the old one:
old user = @old
new user = @new
it helps to have two independent browser windows open (two different browsers,
or one browser with a normal window and an incognito window).
steps:
* login to each account separately in the browsers
* create a twitter app at https://apps.twitter.com/ using either account
* note down its consumer_key, consumer_secret
from tweepy import OAuthHandler, API, Cursor
oldauth = OAuthHandler(consumer_key, consumer_secret)
print(oldauth.get_authorization_url())
visit that link in its browser, get the verifier, then
oldauth.get_access_token(verifier)
oldapi = API(oldauth)
newauth = OAuthHandler(consumer_key, consumer_secret)
print(newauth.get_authorization_url())
visit that link in its browser, get the verifier, then
newauth.get_access_token(verifier)
newapi = API(newauth)
subscribing to all users:
oldfriends = []
for friend in Cursor(oldapi.friends_ids).items():
oldfriends.append(friend)
tweepy throws an exception if you try to add a friend who's already your
friend, so
already = set()
for friend in Cursor(newapi.friends_ids).items():
already.add(friend)
for friend in oldfriends:
if friend not in already:
try:
_ = snowapi.create_friendship(user_id=friend)
except Exception as exc:
print(str(exc))
else:
print(friend)
already.add(friend)
copying lists:
oldids = {}
for old in oldapi.lists_all():
oldids[old.name] = old.id
_ = newapi.create_list(name=old.name, description=old.description, mode=old.mode)
for new in newapi.lists_all():
print new.name
atnames = ['@' + member.screen_name for member in oldapi.list_members(list_id=oldids[new.name], count=5000)]
print len(atnames)
# there is a limit to how many i can add at a time, but i don't know
# what it is. using 20 for now
for i in range(0, len(atnames), 20):
_ = newapi.add_list_members(list_id=new.id, screen_name=atnames[i:i+20])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment