Last active
April 20, 2022 05:07
-
-
Save swbuehler/8a72fe2e741eed41791268ef112aa6c2 to your computer and use it in GitHub Desktop.
Python 3: Get list of followers for a Twitch Channel (replace 'MissEllaCronin' in line 10 with Twitch username)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
import csv | |
from urllib import request | |
counter = 0 | |
f = open('followers.csv','w',encoding='utf-8',newline="\n") | |
mywriter = csv.writer(f,dialect='excel',quoting=csv.QUOTE_MINIMAL) | |
mywriter.writerow(['Followed At (UTC)','Notifications?','User ID','User Name','User Created At (UTC)','User Updated At (UTC)','Display Name','Logo URL','Bio','User Type']) | |
url = ("https://api.twitch.tv/kraken/channels/missellacronin/follows?direction=ASC&limit=100") | |
response = str(request.urlopen(url).read().decode('utf-8')) | |
myjson = json.loads(response) | |
while (len(myjson['follows']) > 0): | |
for follower in myjson['follows']: | |
created_at = follower['created_at'] | |
notified = follower['notifications'] | |
user_id = follower['user']['_id'] | |
user_name = follower['user']['name'] | |
user_created = follower['user']['created_at'] | |
user_updated = follower['user']['updated_at'] | |
user_display = follower['user']['display_name'] | |
if (follower['user']['logo'] != None): | |
logo_url = follower['user']['logo'] | |
else: | |
logo_url = "" | |
if (follower['user']['bio'] != None): | |
user_bio = follower['user']['bio'] | |
else: | |
user_bio = "" | |
user_type = follower['user']['type'].encode("utf-8") | |
mywriter.writerow([str(created_at),str(notified),user_id,str(user_name),str(user_created),str(user_updated),str(user_display),str(logo_url),str(user_bio),str(user_type)]) | |
url = myjson['_links']['next'] | |
response = str(request.urlopen(url).read().decode('utf-8')) | |
myjson = json.loads(response) | |
counter += 100 | |
print(counter) | |
f.close |
Updated to add a header row and the "Notifications" indicator (indicates TRUE if the follower gets a notification when the streamer goes live).
I get a 400 "Bad Request" code. Is this API request obsolete?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be gentle; I'm just starting out with Python. ;-)
The order of fields in the CSV is as follows:
The 'excel' dialect is used for the CSV Writer to make it easy to double-click open in Excel or your other spreadsheet program of choice.
I also have a version in Power Query (which only pulls the first 100 until I figure out how to iterate it on the 'next' value) and Google Apps Script (which pulls about 2500 before hitting the 4-minute time limit).
The default script pulls all 51,000+ followers for the
missellacronin
account as an example. changemissellacronin
in line 10 to the username you want to see.