def test_list_endpoint(): assert ( BaseAPI.join_base_and_endpoint("https://example.com/v1/", ["test", "def"]) == "https://example.com/v1/test/def" ) assert ( BaseAPI.join_base_and_endpoint("https://example.com/v1", ["test", "def"]) == "https://example.com/v1/test/def" )
def download_logo(twitch_helix_api, streamer, streamer_id): logo_url = twitch_helix_api.get_profile_image_url(streamer_id) logo_raw_path = f"static/images/logo_{streamer}.png" # returns bytes logo_image_bytes = BaseAPI(None).get_binary(logo_url) # write full-size image... with open(logo_raw_path, "wb") as logo_raw_file: logo_raw_file.write(logo_image_bytes)
def download_sub_badge(twitch_badges_api, streamer, streamer_id, subscriber_badge_version): try: subscriber_badge = twitch_badges_api.get_channel_subscriber_badge( streamer_id, subscriber_badge_version) except BadgeNotFoundError as e: log.warn(f"Unable to download subscriber badge: {e}") return subscriber_badge_path = f"static/images/badge_sub_{streamer}.png" # returns bytes subscriber_badge_bytes = BaseAPI(None).get_binary( subscriber_badge.image_url_1x) # write image... with open(subscriber_badge_path, "wb") as subscriber_badge_file: subscriber_badge_file.write(subscriber_badge_bytes)
def download_logo(twitch_helix_api: TwitchHelixAPI, streamer: UserBasics) -> None: logo_url = twitch_helix_api.get_profile_image_url(streamer.id) if logo_url is None: log.warn( f"Failed to query Twitch API for the profile image url of streamer {streamer.login}" ) return logo_raw_path = f"static/images/logo_{streamer.login}.png" # returns bytes logo_image_bytes = BaseAPI(None).get_binary(logo_url) # write full-size image... with open(logo_raw_path, "wb") as logo_raw_file: logo_raw_file.write(logo_image_bytes)
def download_logo(twitch_helix_api, streamer, streamer_id): logo_url = twitch_helix_api.get_profile_image_url(streamer_id) logo_raw_path = f"static/images/logo_{streamer}.png" logo_tn_path = f"static/images/logo_{streamer}_tn.png" # returns bytes logo_image_bytes = BaseAPI(None).get_binary(logo_url) # write full-size image... with open(logo_raw_path, "wb") as logo_raw_file: logo_raw_file.write(logo_image_bytes) # decode downloaded image read_stream = BytesIO(logo_image_bytes) pil_image = Image.open(read_stream) # downscale and save the thumbnail pil_image.thumbnail((64, 64), Image.ANTIALIAS) pil_image.save(logo_tn_path, "png")
def __init__(self, created_at, **options): self.id = None self.title = options.get("title", "NO TITLE") self.stream_start = BaseAPI.parse_datetime(created_at) self.stream_end = None self.ended = False
def test_list_endpoint_escaping(): assert ( BaseAPI.join_base_and_endpoint("https://example.com/v1/", ["test", "/some Data/"]) == "https://example.com/v1/test/%2Fsome%20Data%2F" )
def test_string_endpoint_no_leading_slash(): assert BaseAPI.join_base_and_endpoint("https://example.com/v1/", "test/def") == "https://example.com/v1/test/def" assert BaseAPI.join_base_and_endpoint("https://example.com/v1/", "/test/def") == "https://example.com/v1/test/def" assert BaseAPI.join_base_and_endpoint("https://example.com/v1", "test/def") == "https://example.com/v1/test/def" assert BaseAPI.join_base_and_endpoint("https://example.com/v1", "/test/def") == "https://example.com/v1/test/def"
def test_safebrowsing(): assert ( BaseAPI.join_base_and_endpoint("https://safebrowsing.googleapis.com/v4/", "/threatMatches:find") == "https://safebrowsing.googleapis.com/v4/threatMatches:find" )
def test_list_endpoint_with_int_path_segment(): assert ( BaseAPI.join_base_and_endpoint("https://example.com/v1", ["test", 1234]) == "https://example.com/v1/test/1234" )
def test_respects_passed_scheme(): assert BaseAPI.fill_in_url_scheme("//example.com", "wss") == "wss://example.com"
def test_fills_in_relative_url(): assert BaseAPI.fill_in_url_scheme("//example.com") == "https://example.com"
def test_does_not_change_non_relative_urls(): assert BaseAPI.fill_in_url_scheme( "http://example.com") == "http://example.com"