Make a python API to fetch the number of followers or subscribers from various platforms.

Anwar Ali
2 min readMar 29, 2023

--

To create a Django REST API to fetch the number of followers or subscribers from various platforms, we need first to set up the necessary API endpoints and integrate the official APIs of each platform to fetch the required data.

Photo by Douglas Lopes on Unsplash

Here’s an example implementation:

  1. Install the necessary packages:
pip install django djangorestframework requests

2. Create a Django project and app:

django-admin startproject follower_count_project
cd follower_count_project
python manage.py startapp follower_count_app

3. Add 'rest_framework' and 'follower_count_app' to the INSTALLED_APPS list in the settings.py file:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'follower_count_app',
]

4. Create a views.py file in the follower_count_app app:

from rest_framework.views import APIView
from rest_framework.response import Response
import requests

class FollowerCountView(APIView):
def get(self, request):
instagram_username = request.query_params.get('insta')
twitch_username = request.query_params.get('twitch')
twitter_username = request.query_params.get('twitter')
youtube_channel = request.query_params.get('youtube')

results = {}

if instagram_username:
instagram_response = requests.get(f"https://www.instagram.com/{instagram_username}/?__a=1")
if instagram_response.status_code == 200:
instagram_data = instagram_response.json()['graphql']['user']
results['instagram'] = instagram_data['edge_followed_by']['count']

if twitch_username:
twitch_response = requests.get(f"https://api.twitch.tv/helix/users?login={twitch_username}", headers={"Client-ID": "<your-twitch-client-id>"})
if twitch_response.status_code == 200:
twitch_data = twitch_response.json()['data']
if len(twitch_data) > 0:
results['twitch'] = twitch_data[0]['follower_count']

if twitter_username:
twitter_response = requests.get(f"https://api.twitter.com/2/users/by/username/{twitter_username}", headers={"Authorization": "Bearer <your-twitter-bearer-token>"})
if twitter_response.status_code == 200:
twitter_data = twitter_response.json()['data']
results['twitter'] = twitter_data['public_metrics']['followers_count']

if youtube_channel:
youtube_response = requests.get(f"https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername={youtube_channel}&key=<your-youtube-api-key>")
if youtube_response.status_code == 200:
youtube_data = youtube_response.json()['items'][0]['statistics']
results['youtube'] = youtube_data['subscriberCount']

return Response(results)

5. Add the FollowerCountView to the urls.py file:

from django.urls import path
from follower_count_app.views import FollowerCountView

urlpatterns = [
path('getFollowerCount/', FollowerCountView.as_view()),
]

6. Run the server and test the API:

python manage.py runserver

Make a GET request to the http://localhost:8000/getFollowerCount/?insta=<instagram_username>&twitch=<twitch_username>&twitter=<twitter_username>&youtube=<youtube_channel> endpoint with the required parameters.

This API will return a JSON response containing the follower counts for each platform.

GitHub

--

--

Anwar Ali
Anwar Ali

Written by Anwar Ali

Python Developer | Data Science, Machine Learning Engineer....... Chackout me here: https://www.youtube.com/@GeekySession

No responses yet