Skip to content

Instantly share code, notes, and snippets.

@swbuehler
Last active September 28, 2016 13:36
Show Gist options
  • Save swbuehler/b48ecbd7d34f834a6479351514d41a2d to your computer and use it in GitHub Desktop.
Save swbuehler/b48ecbd7d34f834a6479351514d41a2d to your computer and use it in GitHub Desktop.
Twitch.tv: Gather followers for an account
$users = @("stevenwatsonb","missellacronin")
foreach ($user in $users) {
"Fetching $user..."
$url = "https://api.twitch.tv/kraken/channels/$user/follows?direction=ASC&limit=100"
$headers = @{"Client-ID" = "XXXXXXXXXX"}
$feed = Invoke-RestMethod -Uri $url -Headers $headers
$total = $feed._total
"Retrieving $total follower(s) for $user..."
$rows = @()
while (($feed.follows).count -ne 0) {
foreach ($follower in $feed.follows) {
$rows += $follower
}
$feed = Invoke-RestMethod -Uri $feed._links.next -Headers $headers
}
$rows | ConvertTo-Json | Out-File "C:\Temp\$user.json"
"Follower(s) for $user fetched and saved."
}
@swbuehler
Copy link
Author

I changed the retrieval method from Invoke-WebRequest to Invoke-RestMethod as the latter handles converting JSON to PowerShell Objects for you. :-)

@swbuehler
Copy link
Author

swbuehler commented Sep 28, 2016

UPDATED: The script now loops through a set of IDs (in this sample, mine and Ella Cronin's) and outputs a JSON file for each ID. I've also updated the end-of-pull criteria to evaluate the number of follows in the batch rather than use a counter (more idiot-proof). You can use Excel 2013/2016's Power Query to import and transform the JSON data.

Also, Twitch now requires that a Client-ID be sent with every request. When you have obtained a Client-ID place it in line 5 where shown by "XXXXXXXXXX".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment