Drew1
1
Hello, I have altered/updated a script I found on Github to fetch a user’s followers on twitch. For some reason the script is fetching way more than the actual followers of the user. Here is the code I am using. Replace username with actual twitch profile name.
import json
import sys
import csv
import urllib.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/username/follows?direction=ASC&limit=100"
req = urllib.request.Request(url)
req.add_header("Client-ID","72v2al6ufhz1x2a32ynl68i6j8vku3")
response = str(urllib.request.urlopen(req).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(urllib.request.urlopen(req).read().decode('utf-8'))
myjson = json.loads(response)
counter += 100
print(counter)
f.close
You are using a deprecated version of the API.
Please upgrade to either v5
Or helix
Drew1
3
Thanks you for your help so far. Ive updated my code to following:
import json
import sys
import csv
import urllib.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(['User Name','Display Name',])
url = "https://api.twitch.tv/helix/users/follows?to_id=6663444"
req = urllib.request.Request(url)
req.add_header("Client-ID","72v2al6ufhz1x2a32ynl68i6j8vku3")
response = str(urllib.request.urlopen(req).read().decode('utf-8'))
myjson = json.loads(response)
Everything before the while loop works but how would I go about listing all of the followers and not just the default 100?
Please wrap your code in three backticks so it is formatted for easier reading.
Please read the documentation, regarding pagination
Or even the note in the docs I linked to earlier
Response Fields
| Field |
Type |
Description |
pagination |
string |
A cursor value, to be used in a subsequent request to specify the starting point of the next set of results. |
Also note:
Optional Query String Paramaters
| Name |
Type |
Description |
after |
string |
Cursor for forward pagination: tells the server where to start fetching the next set of results, in a multi-page response. The cursor value specified here is from the pagination response field of a prior query. |
first |
integer |
Maximum number of objects to return. Maximum: 100. Default: 20. |
The default is 20 and you didn’t include a first in your request
Drew1
5
Thanks for your help. It is now working.
1 Like
system
Closed
6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.