Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 101 lines (80 sloc) 3.289 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#!/usr/bin/env python
import tweepy
import json
import sys
import os
import types
import re

def authorize(config):
    auth = tweepy.OAuthHandler(config['consumer']['key'].encode('ascii','ignore'), config['consumer']['secret'].encode('ascii','ignore'))
    auth_url = auth.get_authorization_url()
    print '==> Please authorize: ' + auth_url
    verifier = raw_input('==> PIN: ').strip()
    auth.get_access_token(verifier)

    config['access'] = {}
    config['access']['key'] = auth.access_token.key
    config['access']['secret'] = auth.access_token.secret

    with open('config.json', 'wb') as handle:
        handle.write(json.dumps(config))

    return config

def get_users(path):
    users = []
    with open( path, 'r' ) as handle:
        for line in handle:
            line = re.sub('[^a-zA-Z0-9_]', '', line)
            if len(line) > 0:
                users.append(line)
    return users

def main():

    if 3 != len(sys.argv):
        sys.stderr.write("""usage: %s <LIST> <FILE>

LIST : The name of the list you want to import members into.
FILE : A file with newline separated screen names.
""" % os.path.basename(sys.argv[0]))
        exit(1)

    with open('config.json', 'rb') as handle:
        config = json.loads(handle.read())

    list_name = sys.argv[1]
    csv = sys.argv[2]
    users = get_users(csv)

    print "==> Loading %d users into \"%s\"" % (len(users), list_name)

    if 'access' not in config or 'key' not in config['access'] or 'secret' not in config['access']:
        config = authorize(config)

    while True:
        try:
            auth = tweepy.OAuthHandler(config['consumer']['key'].encode('ascii','ignore'), config['consumer']['secret'].encode('ascii','ignore'))
            auth.set_access_token(config['access']['key'].encode('ascii','ignore'), config['access']['secret'].encode('ascii','ignore'))
            api = tweepy.API(auth)
            me = api.me()
            break
        except:
            print "==> Existing tokens failed, re-authorizing"
            config = authorize(config)

    the_list = None
    for obj in me.lists():
        if obj.name == list_name:
            the_list = obj
            break

    if not the_list:
        print "==> List doesn't exist, creating it"
        the_list = api.create_list(name=list_name)

    # Okay, so now we have to monkey-patch the api for the lists/members/create_all
    # We only need this until the module gets updated
    if 'add_list_members' not in dir(api):
        def add_list_members (self, *args, **kargs):
                return tweepy.binder.bind_api(
                    path = '/lists/members/create_all.json',
                    method = 'POST',
                    payload_type = 'list',
                    allowed_param = ['list_id','slug','owner_id','owner_screen_name','screen_name','user_id'],
                    require_auth = True
                )(self, *args, **kargs)

        api.add_list_members = types.MethodType( add_list_members, api )

    offset = 0
    while offset < len(users):
        print "==> Sending #%d through #%d" % (offset, offset+100)
        api.add_list_members(list_id=the_list.id, screen_name=','.join(users[offset:offset+100]))
        offset += 100

    print "==> Done!"

if __name__ == "__main__":
    main()
Something went wrong with that request. Please try again.