client.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import requests
  2. class TwitchApiClient:
  3. def __init__(self, client_id, client_secret):
  4. self._auth_url = 'https://id.twitch.tv/oauth2/token'
  5. self.base_url = 'https://api.twitch.tv/helix'
  6. self._client_id = client_id
  7. self._client_secret = client_secret
  8. self._bearer = None
  9. self._authorize()
  10. def _authorize(self):
  11. params = {
  12. 'client_id': self._client_id,
  13. 'client_secret': self._client_secret,
  14. 'grant_type': 'client_credentials'
  15. }
  16. response = requests.post(self._auth_url, params)
  17. json = response.json()
  18. self._bearer = json['access_token']
  19. def user(self, user):
  20. headers = {
  21. 'Authorization': 'Bearer {0}'.format(self._bearer),
  22. 'Client-ID': self._client_id
  23. }
  24. response = requests.get("{0}/users?login={1}".format(self.base_url, user), headers=headers)
  25. return response.json()
  26. def streams(self, query):
  27. headers = {
  28. 'Authorization': 'Bearer {0}'.format(self._bearer),
  29. 'Client-ID': self._client_id
  30. }
  31. url = "{0}/search/channels?query={1}&live_only=true".format(self.base_url, query)
  32. response = requests.get(url, headers=headers)
  33. return response.json()
  34. def subscriptions(self):
  35. headers = {
  36. 'Authorization': 'Bearer {0}'.format(self._bearer),
  37. 'Client-ID': self._client_id
  38. }
  39. url = "{0}/eventsub/subscriptions".format(self.base_url)
  40. response = requests.get(url, headers=headers)
  41. print(response.text)
  42. exit()
  43. return response.json()