Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Saves all your twitter lists as CSV files. #python #twitter
# Save Twitter Lists
# Requirements :
# Python 3 (tested with Python 3.7)
# python-twitter (https://github.com/bear/python-twitter)
# $ pip install python-twitter
# You’ll need to create a Twitter app:
# https://python-twitter.readthedocs.io/en/latest/getting_started.html#create-your-app
# By Joachim Robert
# http://github.com/joachimesque
# Shared under the WTFPL - Do What the Fuck You Want to Public License
import os
import twitter
TWITTER_CONSUMER_KEY="your twitter consumer_key"
TWITTER_CONSUMER_SECRET="your twitter consumer_secret"
TWITTER_ACCESS_TOKEN="your twitter access_token_key"
TWITTER_ACCESS_TOKEN_SECRET="your twitter access_token_secret"
# create dir
dirName = 'lists'
if not os.path.exists(dirName):
os.makedirs(dirName)
# create the API object
api = twitter.Api(consumer_key=TWITTER_CONSUMER_KEY,
consumer_secret=TWITTER_CONSUMER_SECRET,
access_token_key=TWITTER_ACCESS_TOKEN,
access_token_secret=TWITTER_ACCESS_TOKEN_SECRET)
# get the lists
listsObject = api.GetListsList()
# make a CSV file for all lists
print('writing CSV file with lists info')
listsFile = open('%s/all_lists.csv' % dirName, 'w+')
listsFile.write('id,slug,name,member_count,full_name,description,user.screen_name\n')
for l in listsObject:
listInfo = (str(l.id),
l.slug,
l.name,
str(l.member_count),
'"%s"' % l.full_name,
'"%s"' % l.description,
'@' + l.user.screen_name)
listsFile.write(','.join(listInfo) + "\n")
listsFile.close()
# for each list…
for l in listsObject:
# …get its members
members = api.GetListMembers(list_id=l.id)
print('writing list @%s/%s: %s members' % (l.user.screen_name, l.slug, len(members)))
# …define the filename and open it for writing
fileName = "%s/list_%s-%s.csv" % (dirName, l.user.screen_name, l.slug)
listFile = open(fileName, 'w+')
listFile.write('id,screen_name,name,url\n')
for member in members:
# …define the content to be written
memberInfo= (member.id_str,
'@' + member.screen_name,
'"%s"' % member.name,
member.url if member.url else '')
# …write it
listFile.write(','.join(memberInfo) + "\n")
# …and close the file.
listFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment