Exemplo n.º 1
0
def test_size():
    # Using the traditional system, where a factor of 1024 is used
    assert size(10) == "10B"
    assert size(100) == "100B"
    assert size(1000) == "1000B"
    assert size(2000) == "1K"
    assert size(10000) == "9K"
    assert size(20000) == "19K"
    assert size(100000) == "97K"
    assert size(200000) == "195K"
    assert size(1000000) == "976K"
    assert size(2000000) == "1M"
Exemplo n.º 2
0
def test_size_si():
    # Using the SI system, with a factor of 1000
    assert size(10, system=si) == "10B"
    assert size(100, system=si) == "100B"
    assert size(1000, system=si) == "1K"
    assert size(2000, system=si) == "2K"
    assert size(10000, system=si) == "10K"
    assert size(20000, system=si) == "20K"
    assert size(100000, system=si) == "100K"
    assert size(200000, system=si) == "200K"
    assert size(1000000, system=si) == "1M"
    assert size(2000000, system=si) == "2M"
Exemplo n.º 3
0
def bingimage(text, bot):
    """<query> - returns the first bing image search result for <query>"""
    api_key = bot.config.get("api_keys", {}).get("bing_azure")

    # handle NSFW
    show_nsfw = text.endswith(" nsfw")

    # remove "nsfw" from the input string after checking for it
    if show_nsfw:
        text = text[:-5].strip().lower()

    rating = NSFW_FILTER if show_nsfw else DEFAULT_FILTER

    if not api_key:
        return "Error: No Bing Azure API details."

    # why are these all differing formats and why does format have a $? ask microsoft
    params = {
        "Sources": bingify("image"),
        "Query": bingify(text),
        "Adult": bingify(rating),
        "$format": "json"
    }

    request = requests.get(API_URL, params=params, auth=(api_key, api_key))

    # I'm not even going to pretend to know why results are in ['d']['results'][0]
    j = request.json()['d']['results'][0]

    if not j["Image"]:
        return "No results."

    # grab a random result from the top 10
    result = random.choice(j["Image"][:10])

    # output stuff
    tags = []

    # image size
    tags.append("{}x{}px".format(result["Width"], result["Height"]))
    # file type
    tags.append(result["ContentType"])
    # file size
    tags.append(filesize.size(int(result["FileSize"]), system=filesize.alternative))
    # NSFW warning
    if "explicit" in result["Thumbnail"]["MediaUrl"]:
        tags.append("NSFW")

    # join all the tags together in a comma separated string ("tag1, tag2, tag3")
    tag_text = ", ".join(tags)

    return '{} ({})'.format(unescape(result["MediaUrl"]), tag_text)
Exemplo n.º 4
0
def test_size_verbose():
    # Using the verbose system, where a factor of 1024 is used
    assert size(1, system=verbose) == "1 byte"
    assert size(1000, system=verbose) == "1000 bytes"
    assert size(2000, system=verbose) == "1 kilobyte"
    assert size(10000, system=verbose) == "9 kilobytes"
    assert size(2000000, system=verbose) == "1 megabyte"
    assert size(30000000, system=verbose) == "28 megabytes"
Exemplo n.º 5
0
def gse_gis(text):
    """<query> -- Returns first Google Images result for <query>."""
    if not dev_key:
        return "This command requires a Google Developers Console API key."
    if not cx:
        return "This command requires a custom Google Search Engine ID."

    parsed = requests.get(API_CS, params={"cx": cx, "q": text, "searchType": "image", "key": dev_key}).json()

    try:
        result = parsed['items'][0]
        metadata = parsed['items'][0]['image']
    except KeyError:
        return "No results found."

    dimens = '{}x{}px'.format(metadata['width'], metadata['height'])
    size = filesize.size(int(metadata['byteSize']))

    return u'{} [{}, {}, {}]'.format(result['link'], dimens, result['mime'], size)
Exemplo n.º 6
0
def test_size_alias():
    assert size(1, system=fs.V) == "1 byte"