Creating a Twitter Collection via API

Example collection. Can also be viewed here: https://twitter.com/laurenfratamico/timelines/1180945428222595074

Getting Started

Authenticating with Twitter

import json
from requests_oauthlib import OAuth1Session
api_key = 'your api key'
api_secret_key = 'your api secret key'
access_token = 'your access token'
access_token_secret = 'your access token secret'
# twitter oauth
twitter = OAuth1Session(api_key,
client_secret=api_secret_key,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret)

Creating a Collection

# create
url = 'https://api.twitter.com/1.1/collections/create.json'
params_create = {
'name': 'cutest puppies EVER',
'timeline_order': 'tweet_reverse_chron'
}
r = twitter.post(url, data=params_create)
# print the full returned json to ensure success
print(r.json())
# print the response to get the id of the timeline created. This is needed for adding/removing tweets
print(r.json()['response'])

Adding Tweets to a Collection

tweet_ids = [1148286877616803840, 1180845365542752256, 1180123991165607937, 1158830129080098823]# add
url = 'https://api.twitter.com/1.1/collections/entries/add.json'
for tweet_id in tweet_ids:
params_add = {
'tweet_id': tweet_id,
'id': 'custom-1180945428222595074'
}
r = twitter.post(url, data=params_add)
print(r.json())

Bulk Adding Tweets to a Collection

# bulk add
url = 'https://api.twitter.com/1.1/collections/entries/curate.json'
# split into batches of 100 for the uploads
n = 100
batches = [tweet_ids[i:i + n] for i in range(0, len(tweet_ids), n)]
print (len(batches))
for batch in batches:
params_add = {
"id": "custom-1180945428222595074",
"changes": []
}
for tweet_id in batch:
sub_params_add = {
"tweet_id": str(tweet_id),
"op": "add"
}
params_add['changes'].append(sub_params_add)

r = twitter.post(url, data=json.dumps(params_add))
print(r.json())

Other useful collections endpoints

Limitations

--

--

--

Data Scientist at Twitter | Previously Grad Student at MIT Media Lab www.laurenfratamico.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why Elon Musk Challenged Putin to a Fight

Message Queues

How to make Git “forget” about a file that was committed but is now in .gitignore?

How I Ended up Having 25,000 Math Problems for Free — Understanding Web Scraping

TeamCode x Merico, dive into an efficient cloud development era

The “Stack” Data Structure and its Python Implementation

All 49 Keywords in Java part 2

Object Oriented Programming(OOP) Series: Access modifiers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lauren Fratamico

Lauren Fratamico

Data Scientist at Twitter | Previously Grad Student at MIT Media Lab www.laurenfratamico.com

More from Medium

Automating Article Schema Implementation in Hubspot

Hubspot logo on a piece of card stock

Cleaning Data with Node.js, Sending SMS Twilio

Build your Own Google meet launcher (chrome extension) less than 15minutes

Transferring data from MySQL to MsSQL DataBase