From 0c16692f1e1d605ca44dc3e0dd584d043fc9fb85 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 17 May 2015 21:33:25 +0200 Subject: [PATCH] Add followertxt to twitools Modify followertxt such as to use credentials from config Move followertxt's paginate() function to tools --- followertxt.py | 19 +++++++++++++++++++ tools.py | 11 ++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100755 followertxt.py diff --git a/followertxt.py b/followertxt.py new file mode 100755 index 0000000..3e5fb96 --- /dev/null +++ b/followertxt.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +import config, tools +import tweepy + +outfile = "followers.txt" + +auth = tweepy.OAuthHandler(config.cke, config.cse) +auth.set_access_token(config.ato, config.ase) +api = tweepy.API(auth) + +follower_ids = api.followers_ids() + +with open(outfile, 'a') as f: + for page in tools.paginate(follower_ids, 100): + followers = api.lookup_users(user_ids=page) + for follower in followers: + f.write(follower.screen_name + "\n") + diff --git a/tools.py b/tools.py index a3863b3..da22819 100644 --- a/tools.py +++ b/tools.py @@ -1,6 +1,6 @@ import config -import csv, datetime, os, sqlite3, sys, tweepy +import csv, datetime, itertools, os, sqlite3, sys, tweepy class dbObject: @@ -79,6 +79,15 @@ def getDate(date): raise ValueError("Dates must be in YYYY-MM-DD format.") +def paginate(iterable, page_size): + while True: + i1, i2 = itertools.tee(iterable) + iterable, page = (itertools.islice(i1, page_size, None), list(itertools.islice(i2, page_size))) + if len(page) == 0: + break + yield page + + def parseArgs(argv): args = [] path = None