diff --git a/src/main.py b/src/main.py index e5d03ce..5971919 100755 --- a/src/main.py +++ b/src/main.py @@ -2,13 +2,17 @@ # -*- coding: utf-8 -*- from twitch import TwitchClient +import json def main(): with open('.token', 'r') as reader: twitch_token = reader.read().strip() client = TwitchClient(twitch_token) - print(client.raw_query('streams?first=20')) + streams = client.raw_query_v6('streams?game_id=%s&first=100&language=ru&type=live' % client.get_game_id('IRL')[0]) + for i in streams['data']: + print(i['title']) + if __name__ == '__main__': main() diff --git a/src/twitch.py b/src/twitch.py index b860f98..425fbab 100644 --- a/src/twitch.py +++ b/src/twitch.py @@ -1,12 +1,29 @@ +import json import requests class TwitchClient: - def __init__(self, token): self.token = token - self.header = {'Client-ID': self.token} - self.urlbase = 'https://api.twitch.tv/helix/' + + self.header_v5 = {'Client-ID': self.token, 'Accept': 'application/vnd.twitchtv.v5+json'} + self.header_v6 = {'Client-ID': self.token} + + self.urlbase_v5 = 'https://api.twitch.tv/kraken/' + self.urlbase_v6 = 'https://api.twitch.tv/helix/' + + def get_base(self, ver): + if ver == 'v5': return (self.header_v5, self.urlbase_v5) + elif ver == 'v6': return (self.header_v6, self.urlbase_v6) + else: return (None, None) - def raw_query(self, q): - r = requests.get(self.urlbase+q, headers=self.header) - return r.text + def raw_query_v6(self, q): + header, base = self.get_base('v6') + return requests.get(base+q, headers=header).json() + + # Returns (ID, GAMENAME) or None + def get_game_id(self, name): + header, base = self.get_base('v5') + r = requests.get('%s/search/games?query=%s' % (base, name), headers=header).json() + if r.get('games', None): return (r['games'][0]['_id'], r['games'][0]['name']) + return None +