Advertisement
theastropath

getSubscriberListPaginated

Sep 6th, 2017
2,737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. from urllib.parse import urlencode
  2. from requests import Session
  3. from requests.adapters import HTTPAdapter
  4. from requests.exceptions import HTTPError, InvalidURL, ConnectionError
  5. import json
  6.  
  7. ##########################################################
  8. #                Configure your stuff here               #
  9. ##########################################################
  10.  
  11. clientId=""  #Register a Twitch Developer application and put its client ID here
  12. accessToken="" #Generate an OAuth token with channel_subscriptions scope and insert your token here
  13.  
  14. channelName=""  #Put your channel name here
  15. saveLocation = "/var/www/api/subscriberListTest.txt" #Put the location you'd like to save your list here
  16.  
  17. ###################################################################
  18.  
  19. session=Session()
  20. channelId=""
  21.  
  22.  
  23. channelIdUrl="https://api.twitch.tv/kraken/users?login="+channelName
  24.  
  25. retryAdapter = HTTPAdapter(max_retries=2)
  26. session.mount('https://',retryAdapter)
  27. session.mount('http://',retryAdapter)
  28.  
  29. #Find the Channel ID
  30. response = session.get(channelIdUrl, headers={
  31. 'Client-ID': clientId,
  32. 'Accept': 'application/vnd.twitchtv.v5+json',
  33. 'Content-Type': 'application/json'
  34. })
  35. try:
  36.     result = json.loads(response.text)
  37. except:
  38.     result = None
  39.  
  40. if (result):
  41.     channelId = result["users"][0]["_id"]
  42.  
  43. print(channelId)
  44.  
  45. result = None
  46. response = None
  47. offset=0
  48. limit=100
  49. subList=[]
  50.  
  51. while (True):
  52.     apiRequestUrl="https://api.twitch.tv/kraken/channels/"+channelId+"/subscriptions?limit="+str(limit)+"&offset="+str(offset)
  53.  
  54.     #Do the API Lookup
  55.     response = session.get(apiRequestUrl, headers={
  56.     'Client-ID': clientId,
  57.     'Accept': 'application/vnd.twitchtv.v5+json',
  58.     'Authorization': 'OAuth '+accessToken,
  59.     'Content-Type': 'application/json'
  60.     })
  61.  
  62.     try:
  63.         result = json.loads(response.text)
  64.     except:
  65.         result = None
  66.  
  67.     if (result):
  68.         for sub in result["subscriptions"]:
  69.             name=sub['user']['display_name']
  70.             if name!=channelName:
  71.                 print(name)
  72.                 subList.append(sub['user']['display_name'])
  73.     else:
  74.         break
  75.  
  76.     if (len(result["subscriptions"])==limit):
  77.         offset+=limit
  78.     else:
  79.         print("Done")
  80.         break
  81.  
  82.  
  83. if(result):
  84.     f = open(saveLocation,'w')
  85.     for sub in subList:
  86.         f.write(sub+"\n")
  87.     f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement