Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannguyen/ab198eb61aaed4966bbf to your computer and use it in GitHub Desktop.
Save dannguyen/ab198eb61aaed4966bbf to your computer and use it in GitHub Desktop.
Turning a webpage into a Twitter list https://twitter.com/dancow/lists/srccon-2015
"""
Get all Twitter handles listed on a page, add them to a list
"""
import tweepy
import requests
import re
import json
from os.path import expanduser
TWITTER_CREDS_FILE = expanduser('~/.creds/me.json')
SOURCE_URL = 'http://srccon.org/sessions/'
LIST_NAME = 'srccon-2015'
BATCH_COUNT = 50
###
### Downloading the page and getting names
html = requests.get(SOURCE_URL).text
names = set([n.lower() for n in re.findall('twitter.com/@?(\w+)', html)])
# whatever, good enough for me
print("%s Twitter accounts found" %len(names))
###
### Fire up Tweepy and get an api handler
c = json.load(open(TWITTER_CREDS_FILE))
auth = tweepy.OAuthHandler(c['consumer_key'], c['consumer_secret'])
auth.set_access_token(c['access_token'], c['access_token_secret'])
api = tweepy.API(auth)
myname = api.me().screen_name
###
### Create and add to the list
print("Creating list %s/%s" % (myname, LIST_NAME))
api.create_list(LIST_NAME)
# apparently it needs to be broken up
for i in range(0, (len(names) // BATCH_COUNT) + 1):
k = (i * 50), ((i+1) * 50)
somenames = list(names)[k[0]:k[1]]
print("Adding members %s - %s" % (k[0], k[0] + len(somenames)))
api.add_list_members(somenames, slug = LIST_NAME, owner_screen_name = myname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment