top of page

Spotify & Twitter APIs - The Next Two Data retrieval APIs


I have been working on a side project where I started with scarping real-time data. For my side project, I used Youtube's API to collect data relevant to my interesting channel and collect data such as likes, dislikes, view counts, comments, etc.


However, there are various other ways used such as using other APIs like Spotify API, Twitter API, and web scraping.


In this post, let us go through the two APIs used more often for data collection.


Spotify API:


Spotify Developer Account

Similar to the steps I used for Youtube, the following is self-explanatory and doable:

  • Log in to Spotify's developer account

  • Create a Client ID (One thing particular I felt somewhat absurd is that Spotify asks us to fill out a form in order to know what kind of application you are going to build).

  • Collect your Client ID and Client Secret

Using Spotify library in Python:

  • Install, Import & Setting Up Spotipy:

#install spotipy
pip install spotipy
#Import
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
clientId = 'Your Client ID'
clientsecret = 'Your Secret ID'
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)
  • Data Collection using Spotipy Libary in Python

artist_name = []
track_name = []
popularity = []
track_id = []for i in range(0,10000,50):
    track_results = sp.search(q='year:2018', type='track', limit=50,offset=i)
    for i, t in enumerate(track_results['tracks']['items']):
        artist_name.append(t['artists'][0]['name'])
        track_name.append(t['name'])
        track_id.append(t['id'])
        popularity.append(t['popularity'])
  • This can be then transported to a Dataframe for further analysis or model building.

Twitter API:


  • Log in to Twitter Developer Account:

  • Fill out the form to get access to the respective API id's and it may take a while

  • Get all the Consumer Keys and Access Tokens

Using Tweetpy library in Python:

  • Install and Import tweepy:

#install libraries
pip install tweetpy
  • Setup the tokens and keys for data access

#import files and give access to tokens and keys
import tweepy,json
access_token=ACCESS_TOKEN
access_token_secret=TOKEN_SECRET
consumer_key=CONSUMER_KEY
consumer_secret=CONSUMER_SECRET
auth= tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
  • After getting access, retrieve tweets using tweepy stream listener:

tweet_list=[]
class MyStreamListener(tweepy.StreamListener):
    def __init__(self,api=None):
        super(MyStreamListener,self).__init__()
        self.num_tweets=0
        self.file=open("tweet.txt","w")
    def on_status(self,status):
        tweet=status._json
        self.file.write(json.dumps(tweet)+ '\n')
        tweet_list.append(status)
        self.num_tweets+=1
        if self.num_tweets<1000:
            return True
        else:
            return False
        self.file.close()

Later this data can be used as a list or Dataframe for further analysis.


There are a lot of free tutorials out there to work and build on and some of them are below:

The below project ideas can help you with your kind of ideas are being used and can ideate on any ideas of our own.


Project Ideas using APIs:

If you like this post, feel free to check out my other posts and subscribe for weekly updates on data science related topics.

Comments


bottom of page