Last active
September 28, 2016 13:36
-
-
Save swbuehler/b48ecbd7d34f834a6479351514d41a2d to your computer and use it in GitHub Desktop.
Twitch.tv: Gather followers for an account
This file contains hidden or 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
$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." | |
} |
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
I changed the retrieval method from Invoke-WebRequest to Invoke-RestMethod as the latter handles converting JSON to PowerShell Objects for you. :-)