Exemplo n.º 1
0
def imgur_top(req_image, imgur_id):
    headers = {
        "Authorization": "Client-ID " + imgur_id,
    }
    imgurUrl = "https://api.imgur.com/3/gallery/search/viral/all?q_all={s}".format(
        s=req_image)  # noqa: 501
    data = headers_request(imgurUrl, headers)

    if len(data["data"]) == 0:
        return errorEmbedBuilder("Imgur couldn't find anything", "Imgur")

    top_image = data["data"][0]

    em = Embed(
        title="Top image for " + req_image,
        description=urlBuilder(top_image["title"], top_image["link"]),
        colour=0x00FF00,
    )
    if "images" in top_image.keys():
        potential_image = top_image["images"][0]["link"]
        if top_image["images"][0]["type"] == "video/mp4":
            return potential_image

        em.set_image(url=potential_image)
    elif "link" in top_image.keys():
        em.set_image(url=top_image["link"])
    elif len(top_image) == 0:
        return errorEmbedBuilder("Imgur didn't find any images", "Imgur")

    else:
        return errorEmbedBuilder("Imgur had issue with that request", "Imgur")

    general_info("Imgur created and returned embed object")
    return em
Exemplo n.º 2
0
def websterDict_helper(request_word, api_key, max_definitions=5):
    if len(request_word.split(" ")) > 1:
        request_word = request_word.replace(" ", "%20")

    data = query_request(
        "www.dictionaryapi.com",
        "/api/v3/references/collegiate/json/{t}?key={k}".format(
            t=request_word, k=api_key
        ),
    )
    if not any(isinstance(x, dict) for x in data):
        return errorEmbedBuilder(
            "Couldn't define: *"
            + request_word
            + "* "
            + "did you mean: "
            + ", ".join(data),
            "Webster Dictionary",
        )

    general_debug("Webster Dictionary is: " + str(data))

    em = Embed(title="Webster Dictionary: " + data[0]["meta"]["id"], colour=0x2784EF)

    for element in range(min(max_definitions, len(data))):
        datum = data[element]
        em.add_field(
            name=datum["meta"]["id"] + "(" + datum["fl"] + ")",
            value=";\n".join(datum["shortdef"]),
            inline=False,
        )

    general_info("Webster Dictionary created and returned embed object")
    return em
Exemplo n.º 3
0
def wiki_helper(request_word):
    if len(request_word.split(" ")) > 1:
        request_word = request_word.replace(" ", "_")

    data = payload_request(
        "https://en.wikipedia.org/api/rest_v1/page/summary/{t}?redirect=true&origin=*"
        .format(t=request_word), )
    if (data["title"] == "Not found."):
        return errorEmbedBuilder(data["detail"], "Wikipedia")

    general_debug("Wikipedia Entry is:" + str(data))

    message = length_limiter(data["extract"])

    em = Embed(
        title="Wikipedia Entry:" + data["title"],
        url=data["content_urls"]["desktop"]["page"],
        colour=0xFFE9AB,
        description=message,
    )

    if "thumbnail" in data:
        em.set_thumbnail(url=data["thumbnail"]["source"])

    general_info("Wikifind created and returned embed object")
    return em
Exemplo n.º 4
0
def thes_helper(request_word, api_key, max_definitions=7):
    if len(request_word.split(" ")) > 1:
        request_word = request_word.replace(" ", "%20")

    data = query_request(
        "www.dictionaryapi.com",
        "/api/v3/references/thesaurus/json/{t}?key={k}".format(t=request_word,
                                                               k=api_key),
    )
    if not any(isinstance(x, dict) for x in data):
        return errorEmbedBuilder(
            "Couldn't thesaurus: *" + request_word + "* " + "did you mean: " +
            ", ".join(data),
            "Webster Thesaurus",
        )

    general_debug("Thesaurus lookup is: " + str(data))

    syns = data[0]["meta"]["syns"][0]

    number_of_entries = min(len(syns), max_definitions)

    em = Embed(
        title="Thesaurus Entry: " + data[0]["meta"]["id"],
        colour=0xEDBE47,
        description=", ".join(syns[:number_of_entries]),
    )

    general_info("Thesaurus Lookup created and returned embed object")
    return em
Exemplo n.º 5
0
def weather_helper_repeat_user(request_user, location_token, forecast_token):
    try:
        if str(request_user) in user_library:
            lon = user_library[request_user]["lon"]
            lat = user_library[request_user]["lat"]
            return weather_helper(
                request_user, "congo", location_token, forecast_token, lat, lon
            )
    except KeyError:
        return errorEmbedBuilder("Sorry, I have no memory of your user", "Weather")
Exemplo n.º 6
0
def reddit_top3(req_sub):
    if " " in req_sub:
        return errorEmbedBuilder("Subreddit can't have spaces", "Reddit")

    data = query_request(
        "www.reddit.com",
        "/r/{s}/top/.json?limit=3".format(s=req_sub),
    )

    if data is False:
        return errorEmbedBuilder("Something went wrong when decoding",
                                 "Reddit")

    general_debug("Reddit is: " + str(data))

    if "error" in data.keys():
        if "reason" in data.keys():
            return errorEmbedBuilder("Subreddit is " + data["reason"],
                                     "Reddit")
        return errorEmbedBuilder("Subreddit doesn't exist", "Reddit")

    tops = data["data"]["children"]
    if len(tops) < 1:
        return errorEmbedBuilder(
            "[/r/{0}]({1}) has less than 1 recent posts".format(
                req_sub, "https://www.reddit.com/r/" + req_sub),
            "Reddit",
        )

    message = ""
    for i in range(0, len(tops)):
        message += (str(i + 1) + ". " + shortStringBuild(
            tops[i]["data"]["title"],
            tops[i]["data"]["url"],
            tops[i]["data"]["permalink"],
        ))

    em = Embed(title="Top posts of /r/" + req_sub,
               description=message,
               colour=0x0000FF)

    general_info("Reddit created and returned embed object")
    return em
Exemplo n.º 7
0
def urbanDict_multiple(request_word, numberOfDefs=3, char_lim=1000):

    if len(request_word.split(" ")) > 1:
        request_word = request_word.replace(" ", "%20")

    data = query_request("api.urbandictionary.com",
                         "/v0/define?term={stk}".format(stk=request_word))

    definitions = data["list"]

    if len(definitions) == 0:
        return errorEmbedBuilder("Couldn't define: *" + request_word + "*",
                                 "Urban Dictionary")

    em = Embed(
        title="Urban Dictionary Top " + str(numberOfDefs) + ": " +
        definitions[0]["word"],
        colour=0xEF8427,
    )

    definitionRange = min(numberOfDefs, len(definitions))
    for defin in range(definitionRange):

        temp_def = length_limiter(definitions[defin]["definition"],
                                  char_lim // numberOfDefs)
        temp_example = length_limiter(definitions[defin]["example"],
                                      char_lim // numberOfDefs)
        em.add_field(
            name="Definition " + str(defin + 1),
            value=urlBuilder(
                re.sub(bracketRemove, underLinesSub, temp_def, 0,
                       re.MULTILINE),
                definitions[defin]["permalink"],
            ),
        )
        em.add_field(
            name="Example " + str(defin + 1),
            value="*" + re.sub(bracketRemove, underLinesSub, temp_example, 0,
                               re.MULTILINE) + "*",
        )

        if defin != definitionRange - 1:
            emptyString = "\u200B "
            em.add_field(name=emptyString, inline=False, value=emptyString)

    return em
Exemplo n.º 8
0
def urbanDict_helper(request_word, char_lim=900):
    if len(request_word.split(" ")) > 1:
        request_word = request_word.replace(" ", "%20")

    data = query_request("api.urbandictionary.com",
                         "/v0/define?term={stk}".format(stk=request_word))

    definitions = data["list"]

    if len(definitions) == 0:
        return errorEmbedBuilder("Couldn't define: *" + request_word + "*",
                                 "Urban Dictionary")

    general_debug("Urban Dictionary is: " + str(definitions[0]))

    temp_def = definitions[0]["definition"]
    temp_example = definitions[0]["example"]

    temp_def = length_limiter(temp_def, char_lim)
    temp_example = length_limiter(temp_example, char_lim)

    em = Embed(
        title="Urban Dictionary: " + definitions[0]["word"],
        url=definitions[0]["permalink"],
        colour=0xEF8427,
    )
    em.add_field(
        name="Definition",
        value=re.sub(bracketRemove, underLinesSub, temp_def, 0, re.MULTILINE),
    )
    em.add_field(
        name="Example",
        value="*" +
        re.sub(bracketRemove, underLinesSub, temp_example, 0, re.MULTILINE) +
        "*",
    )

    general_info("Urban Dictionary created and returned embed object")
    return em