<commit_before>"""
currency.py by Zarthus
Licensed under MIT

This is a module that introduces commands to convert currencies.
"""

from core import moduletemplate

import requests
import time


class CurrencyConverter(moduletemplate.BotModule):

    def on_module_load(self):
        self.requireApiKey("open_exchange_rates")

        self.register_command("currency", "<amount> <currency> <other currency>",
                              "Convert <amount> <currency> to <other currency>.",
                              self.PRIV_NONE, ["cur", "convert"])
        self.register_command("currencyinfo", "<currency>", "Retrieve informaiton about <currency>.",
                              self.PRIV_NONE, ["curinfo"])

        self.cache = {"convert": {}, "info": {}}
        self.cache_currency_convert()
        self.cache_currency_info()

    def on_command(self, target, nick, command, commandtext, mod, admin):
        if command == "currency" or command == "cur" or command == "convert":
            if not commandtext or len(commandtext.split()) != 3:
                return self.reply_notice(nick, "Usage: currency <amount> <currency> <other currency>")

            split = commandtext.split()
            amount = split[0]
            currency = split[1].upper()
            ocurrency = split[2].upper()

            if not amount.isdigit():
                return self.reply_notice(nick, "<amount> needs to be a numeric value.")
            if not currency.isalpha():
                return self.reply_notice(nick, "<currency> may only be alphabetic letters.")
            if not ocurrency.isalpha():
                return self.reply_notice(nick, "<other currency> may only be alphabetic letters.")
            if ocurrency == currency:
                return self.reply_notice(nick, "<currency> and <other currency> may not be the same.")

            oamount = self.currency_convert(int(amount), currency, ocurrency)
            if oamount and type(oamount) == float or type(oamount) == int:
                return self.reply_target(target, nick, "$(bold) {} {} $(clear) is equal to $(bold) {} {} $+ $(clear) ."
                                                       .format(amount, currency, round(oamount, 3), ocurrency), True)
            elif oamount:
                return self.reply_target(target, nick, "An error occured: {}".format(oamount))
            else:
                return self.reply_target(target, nick, "Was unable to convert {}{} to {}."
                                                       .format(amount, currency, ocurrency))

        if command == "currencyinfo" or command == "curinfo":
            if not commandtext:
                return self.reply_notice(nick, "Usage: currencyinfo <currency>")

            currency = commandtext.upper()
            unabbr = self.currency_info(currency)
            if unabbr:
                # Yes, the wikipedia url *may* be invalid, but I trust that most of these do exist.
                return self.reply_target(target, nick, "{} information: Unabbreviated '{}', Wikipedia: {}"
                                                       .format(currency, unabbr, "https://en.wikipedia.org/wiki/{}"
                                                                                 .format(unabbr.replace(" ", "_"))))
            else:
                return self.reply_target(target, nick, "Currency '{}' does not exist or is not known."
                                                       .format(currency))

    def currency_convert(self, amount, currency, ocurrency):
        if self.cache["convert"]["age"] + 3600 * 6 > time.time():
            self.cache_currency_convert()

        if "rates" in self.cache["convert"]["json"]:
            json = self.cache["convert"]["json"]

            # One of <currency>'s worth (in json["base"] -- default USD)
            currency_one = 0
            ocurrency_one = 0

            if currency in json["rates"]:
                currency_one = json["rates"][currency]
            if ocurrency in json["rates"]:
                ocurrency_one = json["rates"][ocurrency]

            if currency_one == 0:
                return "Currency '{}' was not found.".format(currency)
            if ocurrency_one == 0:
                return "Currency '{}' was not found.".format(ocurrency)

            curInUsd = currency_one ** -1  # 1 currency in USD
            curInUsd = curInUsd * amount  # Tells us how much USD the amount is worth.
            return curInUsd * ocurrency_one
        return False

    def currency_info(self, currency):
        if self.cache["info"]["age"] + 3600 * 6 > time.time():
            self.cache_currency_info()

        if currency in self.cache["info"]["json"]:
            return self.cache["info"]["json"][currency]
        return False

    def cache_currency_convert(self):
        api_url = "http://openexchangerates.org/api/latest.json"
        api_key = self.api_key["open_exchange_rates"]

        payload = {
            "app_id": api_key
        }

        json = None<commit_msg>Hopefully fix the issue where caching did not refresh properly after 6h.<commit_after>"""
currency.py by Zarthus
Licensed under MIT

This is a module that introduces commands to convert currencies.
"""

from core import moduletemplate

import requests
import time


class CurrencyConverter(moduletemplate.BotModule):

    def on_module_load(self):
        self.requireApiKey("open_exchange_rates")

        self.register_command("currency", "<amount> <currency> <other currency>",
                              "Convert <amount> <currency> to <other currency>.",
                              self.PRIV_NONE, ["cur", "convert"])
        self.register_command("currencyinfo", "<currency>", "Retrieve informaiton about <currency>.",
                              self.PRIV_NONE, ["curinfo"])

        self.cache = {"convert": {}, "info": {}}
        self.cache_currency_convert()
        self.cache_currency_info()

    def on_command(self, target, nick, command, commandtext, mod, admin):
        if command == "currency" or command == "cur" or command == "convert":
            if not commandtext or len(commandtext.split()) != 3:
                return self.reply_notice(nick, "Usage: currency <amount> <currency> <other currency>")

            split = commandtext.split()
            amount = split[0]
            currency = split[1].upper()
            ocurrency = split[2].upper()

            if not amount.isdigit():
                return self.reply_notice(nick, "<amount> needs to be a numeric value.")
            if not currency.isalpha():
                return self.reply_notice(nick, "<currency> may only be alphabetic letters.")
            if not ocurrency.isalpha():
                return self.reply_notice(nick, "<other currency> may only be alphabetic letters.")
            if ocurrency == currency:
                return self.reply_notice(nick, "<currency> and <other currency> may not be the same.")

            oamount = self.currency_convert(int(amount), currency, ocurrency)
            if oamount and type(oamount) == float or type(oamount) == int:
                return self.reply_target(target, nick, "$(bold) {} {} $(clear) is equal to $(bold) {} {} $+ $(clear) ."
                                                       .format(amount, currency, round(oamount, 3), ocurrency), True)
            elif oamount:
                return self.reply_target(target, nick, "An error occured: {}".format(oamount))
            else:
                return self.reply_target(target, nick, "Was unable to convert {}{} to {}."
                                                       .format(amount, currency, ocurrency))

        if command == "currencyinfo" or command == "curinfo":
            if not commandtext:
                return self.reply_notice(nick, "Usage: currencyinfo <currency>")

            currency = commandtext.upper()
            unabbr = self.currency_info(currency)
            if unabbr:
                # Yes, the wikipedia url *may* be invalid, but I trust that most of these do exist.
                return self.reply_target(target, nick, "{} information: Unabbreviated '{}', Wikipedia: {}"
                                                       .format(currency, unabbr, "https://en.wikipedia.org/wiki/{}"
                                                                                 .format(unabbr.replace(" ", "_"))))
            else:
                return self.reply_target(target, nick, "Currency '{}' does not exist or is not known."
                                                       .format(currency))

    def currency_convert(self, amount, currency, ocurrency):
        if time.time() > self.cache["convert"]["age"] + 3600 * 6:
            self.cache_currency_convert()

        if "rates" in self.cache["convert"]["json"]:
            json = self.cache["convert"]["json"]

            # One of <currency>'s worth (in json["base"] -- default USD)
            currency_one = 0
            ocurrency_one = 0

            if currency in json["rates"]:
                currency_one = json["rates"][currency]
            if ocurrency in json["rates"]:
                ocurrency_one = json["rates"][ocurrency]

            if currency_one == 0:
                return "Currency '{}' was not found.".format(currency)
            if ocurrency_one == 0:
                return "Currency '{}' was not found.".format(ocurrency)

            curInUsd = currency_one ** -1  # 1 currency in USD
            curInUsd = curInUsd * amount  # Tells us how much USD the amount is worth.
            return curInUsd * ocurrency_one
        return False

    def currency_info(self, currency):
        if time.time() > self.cache["info"]["age"] + 3600 * 6:
            self.cache_currency_info()

        if currency in self.cache["info"]["json"]:
            return self.cache["info"]["json"][currency]
        return False

    def cache_currency_convert(self):
        api_url = "http://openexchangerates.org/api/latest.json"
        api_key = self.api_key["open_exchange_rates"]

        payload = {
            "app_id": api_key
        }

        json = None
{'fraction_non_alphanumeric': 0.11681921070760752, 'fraction_numerical': 0.004231866942230095, 'mean_word_length': 1.895981761185523, 'pattern_counts': {'":': 10, '<': 39, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3784192', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2567, 'n_words': 734}