def test_get_clips_all_lang_valid_token_ret_clips(): responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp), status=200, content_type='application/json') responses.add(responses.GET, f'{BASE_HELIX_URL}/clips?' f'broadcaster_id={example_users_list[0]["_id"]}', body=json.dumps(example_clips_resp), status=200, content_type='application/json') responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[1]["_id"]}-' f'{example_users_list[1]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp), status=200, content_type='application/json') responses.add(responses.GET, f'{BASE_HELIX_URL}/clips?' f'broadcaster_id={example_users_list[1]["_id"]}', body=json.dumps(example_clips_resp2), status=200, content_type='application/json') getter = ClipGetter(example_users_list) clips = getter.get_clips(oauth_token=example_app_access_token) assert len(clips) == 2 assert clips[0]['id'] == 'RandomClip1' assert clips[1]['id'] == 'RandomClip3'
def test__get_good_clips_valid_started_and_ended_at_ret_clips(): started_at = '2019-08-19T00:00:00Z' ended_at = '2019-08-21T00:00:00Z' responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp), status=200, content_type='application/json') responses.add(responses.GET, f'{BASE_HELIX_URL}/clips?' f'broadcaster_id={example_users_list[0]["_id"]}' f'&started_at={started_at}' f'&ended_at={ended_at}', body=json.dumps(example_clips_resp), status=200, content_type='application/json') getter = ClipGetter(example_users_list, started_at=started_at, ended_at=ended_at) clips = getter._get_good_clips(example_users_list[0]["_id"], example_users_list[0]["name"], oauth_token=example_app_access_token) assert len(clips) == 1 assert clips[0]['id'] == 'RandomClip1'
def test__get_avg_viewers_in_past_week_gt_400_status_code_throws_exception(): responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', status=400, content_type='text/html') getter = ClipGetter(example_users_list) with pytest.raises(requests.HTTPError): getter._get_avg_viewers_in_past_week(example_users_list[0]['_id'], example_users_list[0]['name'])
def test__get_avg_viewers_in_past_week_cant_find_user_ret_0(): expected_avg = 0 responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', status=404, content_type='text/html') getter = ClipGetter(example_users_list) avg = getter._get_avg_viewers_in_past_week(example_users_list[0]['_id'], example_users_list[0]['name']) assert expected_avg == avg
def test__get_good_clips_didnt_stream_ret_no_clips(): responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp_empty), status=200, content_type='application/json') getter = ClipGetter(example_users_list) clips = getter._get_good_clips(example_users_list[0]["_id"], example_users_list[0]["name"], oauth_token=example_app_access_token) assert len(clips) == 0
def test__get_avg_viewers_in_past_week_user_didnt_stream_ret_0(): expected_avg = 0 responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp_empty), status=200, content_type='application/json') getter = ClipGetter(example_users_list) avg = getter._get_avg_viewers_in_past_week(example_users_list[0]['_id'], example_users_list[0]['name']) assert expected_avg == avg
def test_get_clips_invalid_client_id_and_token_throws_exception(): responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp), status=200, content_type='application/json') responses.add( responses.GET, f'{BASE_HELIX_URL}/clips?' f'broadcaster_id={example_users_list[0]["_id"]}', body=json.dumps(example_clips_resp_invalid_client_id_and_token), status=401, content_type='application/json') getter = ClipGetter(example_users_list) with pytest.raises(requests.HTTPError): getter.get_clips()
def test__get_good_clips_valid_client_id_ret_clips(): responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp), status=200, content_type='application/json') responses.add(responses.GET, f'{BASE_HELIX_URL}/clips?' f'broadcaster_id={example_users_list[0]["_id"]}', body=json.dumps(example_clips_resp), status=200, content_type='application/json') getter = ClipGetter(example_users_list) clips = getter._get_good_clips(example_users_list[0]["_id"], example_users_list[0]["name"], client_id=example_client_id) assert len(clips) == 1 assert clips[0]['id'] == 'RandomClip1'
def test__get_good_clips_invalid_ended_at_throw_exception(): ended_at = 'a' responses.add(responses.GET, f'{BASE_TWITCHMETRICS_URL}/c/{example_users_list[0]["_id"]}-' f'{example_users_list[0]["name"]}/recent_viewership_values', body=json.dumps(example_twitchmetrics_viewership_resp), status=200, content_type='application/json') responses.add( responses.GET, f'{BASE_HELIX_URL}/clips?' f'broadcaster_id={example_users_list[0]["_id"]}' f'&ended_at={ended_at}', body=json.dumps(example_clips_resp_invalid_started_or_ended_at), status=400, content_type='application/json') getter = ClipGetter(example_users_list, ended_at=ended_at) with pytest.raises(requests.HTTPError): getter._get_good_clips(example_users_list[0]["_id"], example_users_list[0]["name"], oauth_token=example_app_access_token)
def main(): """Executes the Clip9 main script.""" start_time = time.time() args = _parse_args() logging.basicConfig(level=logging.INFO, filename=args.log_file, format='[%(asctime)s]%(levelname)s: %(message)s') sys.excepthook = handle_exception logging.info("-------STARTING CLIP9-------") cfg_file_name = f'{os.path.dirname(sys.argv[0])}/../credentials.cfg' credentials = _parse_credentials_cfg(cfg_file_name) client_id = credentials['TWITCH_CLIENT_ID'] client_secret = credentials['TWITCH_CLIENT_SECRET'] token = OauthToken(client_id, client_secret) if not token.validate(): logging.error("Token isn't valid") sys.exit(1) try: team_users = TeamUsers() team_users.get(args.team, client_id=client_id, oauth_token=token.token) users_list = team_users.users_list getter = ClipGetter(users_list, started_at=args.started_at, ended_at=args.ended_at, lang=args.lang) clips_list = getter.get_clips(client_id, token.token) splicer = ClipSplicer(clips_list) splicer.splice(args.output_file, args.clips_dir) logging.info("Successfully generated a video of good clips") except Exception as e: logging.error("There was an exception during execution") raise e finally: token.revoke() elapsed_time = time.time() - start_time logging.info("Execution time: %s seconds", elapsed_time)
def test__get_clip_rating_low_clip_views_high_avg_ret_lt_1(): expected_rating = 1 getter = ClipGetter(example_users_list) rating = getter._get_clip_rating(150, 1000) assert expected_rating > rating