def test_get_user_follows_passes_all_params_to_request(): responses.add( responses.GET, "{}users/follows".format(BASE_HELIX_URL), body=json.dumps(example_get_streams_metadata_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") user_follows = client.get_user_follows( after="eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ==", page_size=100, from_id=23161357, to_id=12345678, ) follow = user_follows.next() assert len(responses.calls) == 1 assert isinstance(user_follows, APICursor) assert isinstance(follow, Follow) url = responses.calls[0].request.url assert url.startswith("https://api.twitch.tv/helix/users/follows?") assert "after=eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ%3D%3D" in url assert "first=100" in url assert "from_id=23161357" in url assert "to_id=12345678" in url
def test_get_top_games_passes_all_params_to_request(): responses.add( responses.GET, "{}games/top".format(BASE_HELIX_URL), body=json.dumps(example_get_top_games_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") games = client.get_top_games( after="eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ==", before="eyJiIjp7Ik9mZnNldCI6MH0sImEiOnsiT2Zmc2V0Ijo0MH19==", page_size=100, ) game = games.next() assert len(responses.calls) == 1 assert isinstance(games, APICursor) assert isinstance(game, Game) url = responses.calls[0].request.url assert url.startswith("https://api.twitch.tv/helix/games/top?") assert "after=eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ%3D%3D" in url assert "before=eyJiIjp7Ik9mZnNldCI6MH0sImEiOnsiT2Zmc2V0Ijo0MH19%3D%3D" in url assert "first=100" in url
def test_get_clips_next_returns_clip_object(param, value): responses.add( responses.GET, "{}clips".format(BASE_HELIX_URL), body=json.dumps(example_get_clips_cursor_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") kwargs = {param: value} clips = client.get_clips(**kwargs) clip = clips.next() assert len(responses.calls) == 1 assert isinstance(clips, APICursor) assert clips._cursor == example_get_clips_cursor_response["pagination"]["cursor"] assert isinstance(clip, Clip) assert clip.id == example_get_clips_cursor_response["data"][0]["id"] assert ( clip.broadcaster_id == example_get_clips_cursor_response["data"][0]["broadcaster_id"] ) assert clip.created_at == datetime(2017, 11, 30, 22, 34, 17)
def test_get_videos_passes_correct_params_when_user_or_game_is_set(param, value): responses.add( responses.GET, "{}videos".format(BASE_HELIX_URL), body=json.dumps(example_get_videos_cursor_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") kwargs = { param: value, "after": "eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ==", "before": "eyJiIjp7Ik9mZnNldCI6MH0sImEiOnsiT2Zmc2V0Ijo0MH19==", "page_size": 100, "language": "en", } videos = client.get_videos(**kwargs) video = videos.next() assert len(responses.calls) == 1 assert isinstance(videos, APICursor) assert isinstance(video, Video) url = responses.calls[0].request.url url.startswith("https://api.twitch.tv/helix/videos?") assert "{}=23161357".format(param) in url assert "after=eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ%3D%3D" in url assert "before=eyJiIjp7Ik9mZnNldCI6MH0sImEiOnsiT2Zmc2V0Ijo0MH19%3D%3D" in url assert "first=100" in url assert "language=en" in url assert "period=all" in url assert "sort=time" in url assert "type=all" in url
def twitch_chat_metadata_scraper(channel_name): print('getting metadata for channel: ' + channel_name + '\n') log_path = '../chat_log_data/' log_file_name = log_path + channel_name + '_stream_metadata.txt' client = TwitchHelix(client_id=os.environ.get('TWITCH_CLIENT_KEY')) f = open(log_file_name, "a+") f.close() #writes to time and data to file import time starttime = time.time() import datetime while True: #gets current channel metadata streams_iterator = client.get_streams(user_logins=channel_name) stream_data = streams_iterator.next() #gets current datetime now = str(datetime.datetime.now().isoformat()) #prints current time output # print ("tick- ",now) #writes time and channel metadata to file data = str(now + str(stream_data) + "\n") f = open(log_file_name, "a+") f.write(data) print(data) f.close() time.sleep(60.0 - ((time.time() - starttime) % 60.0))
def test_get_streams_metadata_next_returns_stream_metadata_object(): responses.add( responses.GET, "{}streams/metadata".format(BASE_HELIX_URL), body=json.dumps(example_get_streams_metadata_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") streams_metadata = client.get_streams_metadata() metadata = streams_metadata.next() assert len(responses.calls) == 1 assert isinstance(streams_metadata, APICursor) assert ( streams_metadata._cursor == example_get_streams_metadata_response["pagination"]["cursor"] ) assert isinstance(metadata, StreamMetadata) assert ( metadata.user_id == example_get_streams_metadata_response["data"][0]["user_id"] ) assert ( metadata.game_id == example_get_streams_metadata_response["data"][0]["game_id"] )
def test_get_videos_passes_correct_params_when_user_or_game_is_set(param, value): responses.add(responses.GET, '{}videos'.format(BASE_HELIX_URL), body=json.dumps(example_get_videos_cursor_response), status=200, content_type='application/json') client = TwitchHelix('client id') kwargs = { param: value, 'after': 'eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ==', 'before': 'eyJiIjp7Ik9mZnNldCI6MH0sImEiOnsiT2Zmc2V0Ijo0MH19==', 'page_size': 100, 'language': 'en', } videos = client.get_videos(**kwargs) video = videos.next() assert len(responses.calls) == 1 assert isinstance(videos, APICursor) assert isinstance(video, Video) url = responses.calls[0].request.url url.startswith('https://api.twitch.tv/helix/videos?') assert '{}=23161357'.format(param) in url assert 'after=eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ%3D%3D' in url assert 'before=eyJiIjp7Ik9mZnNldCI6MH0sImEiOnsiT2Zmc2V0Ijo0MH19%3D%3D' in url assert 'first=100' in url assert 'language=en' in url assert 'period=all' in url assert 'sort=time' in url assert 'type=all' in url
def test_get_user_follows_next_returns_follow_object(): responses.add( responses.GET, "{}users/follows".format(BASE_HELIX_URL), body=json.dumps(example_get_user_follows_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") user_follows = client.get_user_follows(to_id=23161357) follow = user_follows.next() assert len(responses.calls) == 1 assert isinstance(user_follows, APICursor) assert ( user_follows.cursor == example_get_user_follows_response["pagination"]["cursor"] ) assert user_follows.total == example_get_user_follows_response["total"] assert isinstance(follow, Follow) assert follow.from_id == example_get_user_follows_response["data"][0]["from_id"] assert follow.to_id == example_get_user_follows_response["data"][0]["to_id"] assert follow.followed_at == datetime(2017, 8, 22, 22, 55, 24)
def __init__(self, *args): super().__init__(*args) self.helix = TwitchHelix( client_id='vnsbdjciw4fcif1k57w1c07a65wk03', oauth_token='oauth:17qyf4koyvfdqjs4me7zr451lccmtn') self.client = TwitchClient( client_id='vnsbdjciw4fcif1k57w1c07a65wk03', oauth_token='oauth:17qyf4koyvfdqjs4me7zr451lccmtn')
def __init__(self): super(TwitchStreams, self).__init__(name="TwitchStreams") self.streamObject = {} self.streamList = [] self.set_refresh_token_url = "https://twitchtokengenerator.com/api/refresh" \ "/mvfohzdugl5spyqmzsilbfoqm4d45ie965i6h55vxeovbr3eqe " self.client = TwitchHelix(client_id="gp762nuuoqcoxypju8c569th9wz7q5", oauth_token="fu4l5bwwgmk9b4f7rfaphzetntp30o")
def __init__(self, api_client_id, api_oauth_token, browser_client_id, browser_oauth_token): self.api_client_id = api_client_id self.api_oauth_token = api_oauth_token self.browser_client_id = browser_client_id self.browser_oauth_token = browser_oauth_token self.twitch_client = TwitchHelix( client_id=self.api_client_id, oauth_token=self.api_oauth_token )
def test_get_user_follows_raises_attribute_exception_if_no_param_is_set(): responses.add(responses.GET, '{}users/follows'.format(BASE_HELIX_URL), body=json.dumps(example_get_clips_response), status=200, content_type='application/json') client = TwitchHelix('client id') with pytest.raises(TwitchAttributeException): client.get_user_follows() assert len(responses.calls) == 0
def test_get_top_games_returns_api_cursor(): responses.add(responses.GET, '{}games/top'.format(BASE_HELIX_URL), body=json.dumps(example_get_top_games_response), status=200, content_type='application/json') client = TwitchHelix('client id') games = client.get_top_games() assert len(responses.calls) == 1 assert isinstance(games, APICursor)
def test_get_user_follows_returns_api_cursor(): responses.add(responses.GET, '{}users/follows'.format(BASE_HELIX_URL), body=json.dumps(example_get_user_follows_response), status=200, content_type='application/json') client = TwitchHelix('client id') user_follows = client.get_user_follows(to_id=23161357) assert len(responses.calls) == 1 assert isinstance(user_follows, APICursor)
def test_get_streams_metadata_returns_api_cursor(): responses.add(responses.GET, '{}streams/metadata'.format(BASE_HELIX_URL), body=json.dumps(example_get_streams_metadata_response), status=200, content_type='application/json') client = TwitchHelix('client id') streams_metadata = client.get_streams_metadata() assert len(responses.calls) == 1 assert isinstance(streams_metadata, APICursor)
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_videos_raises_attribute_exception_for_invalid_video_ids(): responses.add(responses.GET, '{}videos'.format(BASE_HELIX_URL), body=json.dumps(example_get_videos_response), status=200, content_type='application/json') client = TwitchHelix('client id') kwargs = {'video_ids': ['12345'] * 101} with pytest.raises(TwitchAttributeException): client.get_videos(**kwargs) assert len(responses.calls) == 0
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 test_get_streams_metadata_raises_attribute_exception_for_invalid_params(param, value): responses.add(responses.GET, '{}streams/metadata'.format(BASE_HELIX_URL), body=json.dumps(example_get_streams_metadata_response), status=200, content_type='application/json') client = TwitchHelix('client id') kwargs = {param: value} with pytest.raises(TwitchAttributeException): client.get_streams_metadata(**kwargs) assert len(responses.calls) == 0
def test_get_clips_raises_attribute_exception_if_no_param_is_set(): responses.add(responses.GET, '{}clips'.format(BASE_HELIX_URL), body=json.dumps(example_get_clips_response), status=200, content_type='application/json') client = TwitchHelix('client id') with pytest.raises(TwitchAttributeException) as e: client.get_clips() assert ('At least one of the following parameters must be provided ' '[broadcaster_id, clip_ids, game_id]') in str(e) assert len(responses.calls) == 0
def test_get_streams_returns_api_cursor(): responses.add( responses.GET, "{}streams".format(BASE_HELIX_URL), body=json.dumps(example_get_streams_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") streams = client.get_streams() assert len(responses.calls) == 1 assert isinstance(streams, APICursor)
def test_get_top_games_raises_attribute_exception_for_invalid_params(): responses.add(responses.GET, '{}games/top'.format(BASE_HELIX_URL), body=json.dumps(example_get_top_games_response), status=200, content_type='application/json') client = TwitchHelix('client id') kwargs = {'page_size': 101} with pytest.raises(TwitchAttributeException): client.get_top_games(**kwargs) assert len(responses.calls) == 0
def test_get_videos_passes_correct_params_when_video_ids_are_set(): responses.add(responses.GET, '{}videos'.format(BASE_HELIX_URL), body=json.dumps(example_get_videos_response), status=200, content_type='application/json') client = TwitchHelix('client id') videos = client.get_videos(video_ids=['234482848']) assert len(responses.calls) == 1 assert isinstance(videos, list) video = videos[0] assert isinstance(video, Video) assert responses.calls[0].request.url == 'https://api.twitch.tv/helix/videos?id=234482848'
def name_to_id(nombre): helix = TwitchHelix(client_id='vnsbdjciw4fcif1k57w1c07a65wk03', oauth_token='oauth:17qyf4koyvfdqjs4me7zr451lccmtn') HEADS = { "Accept": "application/vnd.twitchtv.v5+json", "Client-ID": "vnsbdjciw4fcif1k57w1c07a65wk03" #"Authorization" : "OAuth 17qyf4koyvfdqjs4me7zr451lccmtn" } #print("Favor de ingresar su usuario de Twitch") name = nombre URL = "https://api.twitch.tv/kraken/users?login={}".format(name) r = requests.get(url=URL, headers=HEADS) temp = r.json() id = temp["users"][0]["_id"] return id
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
def test_get_clips_raises_attribute_exception_for_invalid_clip_ids(): responses.add( responses.GET, "{}clips".format(BASE_HELIX_URL), body=json.dumps(example_get_clips_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") kwargs = {"clip_ids": ["12345"] * 101} with pytest.raises(TwitchAttributeException): client.get_clips(**kwargs) assert len(responses.calls) == 0
def test_get_videos_raises_attribute_exception_for_invalid_params(param, value): responses.add( responses.GET, "{}videos".format(BASE_HELIX_URL), body=json.dumps(example_get_videos_cursor_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") kwargs = {"game_id": "123456", param: value} with pytest.raises(TwitchAttributeException): client.get_videos(**kwargs) assert len(responses.calls) == 0
def test_get_games_returns_list_of_game_objects(): responses.add(responses.GET, '{}games'.format(BASE_HELIX_URL), body=json.dumps(example_get_games_response), status=200, content_type='application/json') client = TwitchHelix('client id') games = client.get_games() assert len(responses.calls) == 1 assert isinstance(games, list) game = games[0] assert isinstance(game, Game) assert game.id == example_get_games_response['data'][0]['id'] assert game.name == example_get_games_response['data'][0]['name']
def test_get_user_follows_raises_attribute_exception_for_invalid_params(): responses.add( responses.GET, "{}users/follows".format(BASE_HELIX_URL), body=json.dumps(example_get_top_games_response), status=200, content_type="application/json", ) client = TwitchHelix("client id") kwargs = {"from_id": 23161357, "page_size": 101} with pytest.raises(TwitchAttributeException): client.get_user_follows(**kwargs) assert len(responses.calls) == 0