def test_get_oauth_raises_oauth_exception_worse_request(): responses.add( responses.POST, "{}token".format(BASE_OAUTH_URL), body=json.dumps(example_get_oauth_bad_request), status=401, content_type="application/json", ) client = TwitchHelix("client id", client_secret="client secret") with pytest.raises(TwitchOAuthException): client.get_oauth()
def test_get_oauth_returns_oauth_token_with_scopes(): responses.add( responses.POST, "{}token".format(BASE_OAUTH_URL), body=json.dumps(example_get_oauth_response), status=200, content_type="application/json", ) client = TwitchHelix( "client id", client_secret="client secret", scopes=["analytics:read:extensions"] ) client.get_oauth()
def test_get_oauth_returns_oauth_token(): responses.add( responses.POST, "{}token".format(BASE_OAUTH_URL), body=json.dumps(example_get_oauth_response), status=200, content_type="application/json", ) client = TwitchHelix("client id", client_secret="client secret") client.get_oauth() assert client._oauth_token
def getGameName(ctx, c_id, c_secret, channel): """ Returns a string containing the current game being played by the streamer """ client = TwitchHelix(client_id=c_id, client_secret=c_secret) client.get_oauth() game_name = '' try: stream = client.get_streams(user_logins=channel)._queue[0] game_id = stream["game_id"] game_info = client.get_games(game_ids=game_id) game_name = game_info[0]["name"] except Exception as E: print('Game info not available') return game_name
class Twitch: """Twitch enables interaction with the twitch.tv API to get info on Heroes of the Storm streams.""" def __init__(self): self._client = TwitchHelix( client_id=os.environ["TWITCH_CLIENT_ID"], client_secret=os.environ["TWITCH_CLIENT_SECRET"], ) self._client.get_oauth() # TODO: doesn't use pagination, limited to 100 streams by twitch def _get_streams(self): games = self._client.get_games(names=["Heroes of the Storm"]) game_id = games[0]["id"] return self._client.get_streams(game_ids=[game_id], page_size=100) def _format_stream(self, stream): """Takes a stream dict as found in API responses and formats it for the sidebar.""" display_name = stream["user_name"].strip() status = stream["title"].strip() viewers = str(stream["viewer_count"]) url = "https://www.twitch.tv/{}".format(stream["user_login"]) return "* [{0} ~~{1}~~ ^{2}]({3})\n\n".format( display_name, status, viewers, url) # TODO: omg get this outta here def sidebar_text(self): streams = self._get_streams() # Choose top five streams top_streams_formatted = [self._format_stream(s) for s in streams[:5]] top_streams_text = "".join(top_streams_formatted) # Choose five randomly chosen streams random_streams = random.sample(streams[5:], 5) random_streams_formatted = [self._format_stream(s) for s in random_streams] random_streams_text = "".join(random_streams_formatted) return "Top\n\n{0}\nDiscover\n\n{1}".format( top_streams_text, random_streams_text)
def get_uptime(): """ Returns a string of the current uptime Returns: str: Contains the current amount of uptime for the channel """ # Set up twitch API call and get stream info client = TwitchHelix(client_id=settings.get_client_id(), client_secret=settings.get_client_secret()) client.get_oauth() stream = client.get_streams(user_logins=settings.get_channel())._queue[0] # Get stream start time (API sends UTC time) and calculate uptime start_time = stream["started_at"] uptime = datetime.utcnow() - start_time return str(uptime).split(".")[0]
def test_get_oauth_raises_oauth_exception_missing_secret(): client = TwitchHelix("client id") with pytest.raises(TwitchOAuthException): client.get_oauth()