Advertisement
Appendko

Twitch Follower Fetcher ver 0.1

Jul 13th, 2019
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. import time, json, csv, urllib.request
  2.  
  3. # Twitch Follower Fetcher ver 0.1
  4. # ----------------------------------------------------------------------------
  5. # "THE BEER-WARE LICENSE" (Revision 42):
  6. # <[email protected]> wrote this file. As long as you retain this notice you
  7. # can do whatever you want with this stuff. If we meet some day, and you think
  8. # this stuff is worth it, you can buy me a beer in return.
  9. # Append Huang ( https://twitch.tv/append )
  10. # ----------------------------------------------------------------------------
  11.  
  12. #### BEGIN INPUT SECTION ####
  13.  
  14. # The channel name
  15. username = "append"
  16.  
  17. # The Client-ID you got at at https://dev.twitch.tv/console/apps
  18. # Note that the default value "p0gch4mp101fy451do9uod1s1x9i4a" will not work
  19. # You'll need to apply one at https://dev.twitch.tv/console/apps if you didn't have one.
  20. client_id = "p0gch4mp101fy451do9uod1s1x9i4a"
  21.  
  22. # The output file (in csv format)
  23. outputfile = "output.csv"
  24.  
  25. #### END INPUT SECTION ####
  26.  
  27. def urlreq(url):
  28.     """
  29.    Basic urllib.request helper function
  30.    Twitch API needs GET method with header contains client-id or OAUTH.
  31.    Actually OAUTH is better for lower limitation, but that's really annoying....
  32.    """
  33.     metadata = {"Client-ID": client_id};
  34.     req = urllib.request.Request(url, headers=metadata);
  35.     res = urllib.request.urlopen(req);
  36.     resData = str(res.read(), encoding='utf-8')
  37.     return json.loads(resData)
  38.  
  39. def find_id(username):
  40.     """
  41.    Twitch use userid to store all data, but nobody can remember those id.
  42.    We need to convert the username to its userid first.
  43.    """
  44.     url = "https://api.twitch.tv/helix/users?login=" + username;
  45.     resdict = urlreq(url)
  46.     userid = resdict['data'][0]['id']
  47.     return userid
  48.  
  49. def get_follower_list(userid):
  50.     """
  51.    With the userid, we can obtain 100 followers with the api.
  52.    Here I use a while loop to get the whole follower lists.
  53.    Twitch accept 30 calls in 1 minutes for client-id user,
  54.    so I submit a call every 2 seconds.
  55.    """
  56.     cursor = None
  57.     total = 100
  58.     res_lst = []
  59.     while len(res_lst) < total:
  60.         url = "https://api.twitch.tv/helix/users/follows?first=100&to_id=" + userid
  61.         if cursor:
  62.             url += "&after=" + cursor
  63.         resdict = urlreq(url)
  64.         total = resdict['total']
  65.         cursor = resdict['pagination']['cursor']
  66.         cur_lst = [[x['from_id'], x['from_name'], x['followed_at']] for x in resdict['data']]
  67.         res_lst.extend(cur_lst)
  68.         print('%d/%d'%(len(res_lst),total))
  69.         time.sleep(2)
  70.     return res_lst
  71.  
  72. def output_csv(outputfile, table):
  73.     """
  74.    For any table (list of list),
  75.    output it in the csv format.
  76.    """
  77.     with open(outputfile, 'w', newline='', encoding='utf-8-sig') as csvfile:
  78.         writer = csv.writer(csvfile)
  79.         writer.writerows(table)
  80.     return
  81.  
  82. # main sciript
  83. if __name__ == "__main__":
  84.     userid = find_id(username)
  85.     follower_lst = get_follower_list(userid)
  86.     output_csv(outputfile, follower_lst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement