Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time, json, csv, urllib.request
- # Twitch Follower Fetcher ver 0.1
- # ----------------------------------------------------------------------------
- # "THE BEER-WARE LICENSE" (Revision 42):
- # <[email protected]> wrote this file. As long as you retain this notice you
- # can do whatever you want with this stuff. If we meet some day, and you think
- # this stuff is worth it, you can buy me a beer in return.
- # Append Huang ( https://twitch.tv/append )
- # ----------------------------------------------------------------------------
- #### BEGIN INPUT SECTION ####
- # The channel name
- username = "append"
- # The Client-ID you got at at https://dev.twitch.tv/console/apps
- # Note that the default value "p0gch4mp101fy451do9uod1s1x9i4a" will not work
- # You'll need to apply one at https://dev.twitch.tv/console/apps if you didn't have one.
- client_id = "p0gch4mp101fy451do9uod1s1x9i4a"
- # The output file (in csv format)
- outputfile = "output.csv"
- #### END INPUT SECTION ####
- def urlreq(url):
- """
- Basic urllib.request helper function
- Twitch API needs GET method with header contains client-id or OAUTH.
- Actually OAUTH is better for lower limitation, but that's really annoying....
- """
- metadata = {"Client-ID": client_id};
- req = urllib.request.Request(url, headers=metadata);
- res = urllib.request.urlopen(req);
- resData = str(res.read(), encoding='utf-8')
- return json.loads(resData)
- def find_id(username):
- """
- Twitch use userid to store all data, but nobody can remember those id.
- We need to convert the username to its userid first.
- """
- url = "https://api.twitch.tv/helix/users?login=" + username;
- resdict = urlreq(url)
- userid = resdict['data'][0]['id']
- return userid
- def get_follower_list(userid):
- """
- With the userid, we can obtain 100 followers with the api.
- Here I use a while loop to get the whole follower lists.
- Twitch accept 30 calls in 1 minutes for client-id user,
- so I submit a call every 2 seconds.
- """
- cursor = None
- total = 100
- res_lst = []
- while len(res_lst) < total:
- url = "https://api.twitch.tv/helix/users/follows?first=100&to_id=" + userid
- if cursor:
- url += "&after=" + cursor
- resdict = urlreq(url)
- total = resdict['total']
- cursor = resdict['pagination']['cursor']
- cur_lst = [[x['from_id'], x['from_name'], x['followed_at']] for x in resdict['data']]
- res_lst.extend(cur_lst)
- print('%d/%d'%(len(res_lst),total))
- time.sleep(2)
- return res_lst
- def output_csv(outputfile, table):
- """
- For any table (list of list),
- output it in the csv format.
- """
- with open(outputfile, 'w', newline='', encoding='utf-8-sig') as csvfile:
- writer = csv.writer(csvfile)
- writer.writerows(table)
- return
- # main sciript
- if __name__ == "__main__":
- userid = find_id(username)
- follower_lst = get_follower_list(userid)
- output_csv(outputfile, follower_lst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement