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 test_get_videos_returns_list_of_video_objects_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 video.id == example_get_videos_response["data"][0]["id"] assert video.title == example_get_videos_response["data"][0]["title"] assert video.created_at == datetime(2018, 3, 2, 20, 53, 41)
def test_get_videos_next_returns_video_object(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} videos = client.get_videos(**kwargs) video = videos.next() assert len(responses.calls) == 1 assert isinstance(videos, APICursor) assert videos._cursor == example_get_videos_cursor_response['pagination']['cursor'] assert isinstance(video, Video) assert video.id == example_get_videos_cursor_response['data'][0]['id'] assert video.title == example_get_videos_response['data'][0]['title'] assert video.created_at == datetime(2018, 3, 2, 20, 53, 41)
if found: log.info(f"Found stream {stream.user_name} : {stream.title}") bldr = [] if stream.user_name is None or stream.user_name == "": log.warning(f"Stream username for id {stream.user_id} empty") bldr.append("API returned empty username for stream") else: bldr.append("<https://www.twitch.tv/") bldr.append(stream.user_name) bldr.append(">") bldr.append(" | ") bldr.append(stream.title) bldr.append(" | Viewers: ") bldr.append(str(stream.viewer_count)) vods = helix.get_videos(user_id=stream.user_id, page_size=1) if len(vods): log.info(f"Found VOD : {vods[0].url}") bldr.append(" | VOD: <") bldr.append(vods[0].url) bldr.append(">") try: requests.post(WEBHOOK, data={"content": ''.join(bldr)}) except Exception: log.warning(f"Unable to post discord announcement") log.warning(traceback.format_exc()) streams[stream.user_id] = datetime.utcnow() log.debug(f"Searched {count} streams")
def videos(): client = TwitchHelix() videos_iterator = client.get_videos(game_id=493057, page_size=5) for video in islice(videos_iterator, 0, 10): print(video)
class TwitchInterface: 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 get_twitch_metadatas(self, twitch_video_id): twitch_videos = self.twitch_client.get_videos(video_ids=[twitch_video_id]) if twitch_videos: twitch_video_infos = twitch_videos[0] return twitch_video_infos else: raise Exception(f"<!!> Can't find Twitch metadatas for video id {twitch_video_id}") def get_video_games_list(self, twitch_video_id): r = requests.post( "https://gql.twitch.tv/gql", headers={ 'Client-Id': self.browser_client_id, f'Authentification': 'OAuth ' + self.browser_oauth_token, }, data=json.dumps([{ "operationName": "VideoPlayer_ChapterSelectButtonVideo", "variables": { "includePrivate": False, "videoID": str(twitch_video_id) }, "extensions": { "persistedQuery": { "version": 1, "sha256Hash": "8d2793384aac3773beab5e59bd5d6f585aedb923d292800119e03d40cd0f9b41" } } }]) ) if r.status_code == 200: answer_data = r.json() moments_data = answer_data[0]["data"]["video"]["moments"]["edges"] moments = [] for m in moments_data: moments.append(m["node"]) return moments else: return None def get_video_thumbnails_manifest_url(self, twitch_video_id): r = requests.post( "https://gql.twitch.tv/gql", headers={ 'Client-Id': self.browser_client_id, f'Authentification': 'OAuth ' + self.browser_oauth_token, }, data=json.dumps([{ "operationName": "VideoPlayer_VODSeekbarPreviewVideo", "variables": { "includePrivate": False, "videoID": str(twitch_video_id) }, "extensions": { "persistedQuery": { "version": 1, "sha256Hash": "07e99e4d56c5a7c67117a154777b0baf85a5ffefa393b213f4bc712ccaf85dd6" } } }]) ) if r.status_code == 200: answer_data = r.json() seek_previews_url = answer_data[0]["data"]["video"]["seekPreviewsURL"] return seek_previews_url else: return None def get_thumbnails_url_from_manifest(manifest_url): r = requests.get(manifest_url) thumbnails_manifest = r.json() thumbnails_base_url = "/".join(manifest_url.split("/")[:-1]) images = {} for m in thumbnails_manifest: for i in m["images"]: full_url = thumbnails_base_url + "/" + i images[i] = full_url return thumbnails_manifest, images