Skip to content

Instantly share code, notes, and snippets.

@blech
Created March 17, 2010 22:03
Show Gist options
  • Save blech/335774 to your computer and use it in GitHub Desktop.
Save blech/335774 to your computer and use it in GitHub Desktop.
Create an 'everyone' list on Twitter
#!/usr/bin/python
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
import httplib2, urllib, time
from urllib import urlencode
try:
import json
except ImportError:
import simplejson as json
from config import USERNAME, PASSWORD
FRIENDS = "http://api.twitter.com/1/friends/ids.json"
h = httplib2.Http()
h.add_credentials(USERNAME, PASSWORD, 'api.twitter.com')
# make list - TODO check for existing list and update that
data = dict(name="everyone", description="everyone I follow")
headers, body = h.request(
"http://api.twitter.com/1/%s/lists.json" % USERNAME, "POST", urlencode(data)
)
list_info = json.loads(body)
print list_info
list_id = list_info['id']
print "Created 'everyone' list with id '%s'" % list_id
# fetch friends
headers, body = h.request(
FRIENDS, method='GET'
)
friend_ids = json.loads(body)
for id in friend_ids:
data = dict(list_id=list_id, id=id)
headers, body = h.request(
"http://api.twitter.com/1/%s/%s/members.json" % (USERNAME, list_id), "POST", urlencode(data)
)
user_info = json.loads(body)
print "Added user id %s" % id
# TODO remove users no longer followed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment