···53535454Twitter's API only returns up to around 3,200 tweets for most user accounts, but you may find that it returns all available tweets for your own user account.
55555656+## Retrieve accounts in bulk
5757+5858+If you have a list of Twitter screen names (or user IDs) you can bulk fetch their fully inflated Twitter profiles using the `users-lookup` command:
5959+6060+ $ twitter-to-sqlite users-lookup users.db simonw cleopaws
6161+6262+You can pass user IDs instead usincg the `--ids` option:
6363+6464+ $ twitter-to-sqlite users-lookup users.db 12497 3166449535 --ids
6565+5666## Retrieving Twitter followers
57675868The `followers` command retrieves details of every follower of the specified account. You can use it to retrieve your own followers, or you can pass a screen_name to pull the followers for another account.
+25-5
twitter_to_sqlite/cli.py
···149149 default="auth.json",
150150 help="Path to auth.json token file",
151151)
152152-@click.option(
153153- "--stop_after",
154154- type=int,
155155- help="Only pull this number of recent tweets",
156156-)
152152+@click.option("--stop_after", type=int, help="Only pull this number of recent tweets")
157153@click.option("--user_id", help="Numeric user ID")
158154@click.option("--screen_name", help="Screen name")
159155def user_timeline(db_path, auth, stop_after, user_id, screen_name):
···177173 chunk = []
178174 if chunk:
179175 utils.save_tweets(db, chunk)
176176+177177+178178+@cli.command(name="users-lookup")
179179+@click.argument(
180180+ "db_path",
181181+ type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
182182+ required=True,
183183+)
184184+@click.argument("screen_name", nargs=-1)
185185+@click.option(
186186+ "-a",
187187+ "--auth",
188188+ type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
189189+ default="auth.json",
190190+ help="Path to auth.json token file",
191191+)
192192+@click.option("--ids", is_flag=True, help="Treat input as user IDs, not screen names")
193193+def users_lookup(db_path, screen_name, auth, ids):
194194+ "Fetch user accounts"
195195+ auth = json.load(open(auth))
196196+ session = utils.session_for_auth(auth)
197197+ db = sqlite_utils.Database(db_path)
198198+ for batch in utils.fetch_user_batches(session, screen_name, ids):
199199+ utils.save_users(db, batch)
+23
twitter_to_sqlite/utils.py
···229229 ),
230230 ignore=True,
231231 )
232232+233233+234234+def fetch_user_batches(session, ids_or_screen_names, use_ids=False, sleep=1):
235235+ # Yields lists of up to 70 users (tried 100 but got this error:
236236+ # # {'code': 18, 'message': 'Too many terms specified in query.'} )
237237+ batches = []
238238+ batch = []
239239+ for id in ids_or_screen_names:
240240+ batch.append(id)
241241+ if len(batch) == 70:
242242+ batches.append(batch)
243243+ batch = []
244244+ if batch:
245245+ batches.append(batch)
246246+ url = "https://api.twitter.com/1.1/users/lookup.json"
247247+ for batch in batches:
248248+ if use_ids:
249249+ args = {"user_id": ",".join(map(str, batch))}
250250+ else:
251251+ args = {"screen_name": ",".join(batch)}
252252+ users = session.get(url, params=args).json()
253253+ yield users
254254+ time.sleep(sleep)