Пример #1
0
def get_exchange_rate(input_currency, output_currency):
    """Gets the exchange rate as output_currency per input_currency.

    Caches the result for five minutes.
    """
    input_currency = input_currency.upper()
    output_currency = output_currency.upper()
    key = '%s_per_%s' % (output_currency, input_currency)
    rate = cache.get(key)
    if rate is None:
        rate = _query_exchange_rate(input_currency, output_currency)
        cache.set(key, rate, timeout=300)
        # TODO: cache the inverse while we have it?
    return rate
Пример #2
0
def get_exchange_rate(input_currency, output_currency):
    """Gets the exchange rate as output_currency per input_currency.

    Caches the result for five minutes.
    """
    input_currency = input_currency.upper()
    output_currency = output_currency.upper()
    key = '%s_per_%s' % (output_currency, input_currency)
    rate = cache.get(key)
    if rate is None:
        rate = _query_exchange_rate(input_currency, output_currency)
        cache.set(key, rate, timeout=300)
        # TODO: cache the inverse while we have it?
    return rate
Пример #3
0
def get_usd_per_btc():
    """Get current exchange rate as a float.

    Caches the exchange rate for five minutes.
    """
    usd_per_btc = cache.get('usd_per_btc')

    if usd_per_btc is None:
        url = 'https://api.bitcoinaverage.com/ticker/USD/'
        r = requests.get(url)
        data = r.json()
        usd_per_btc = float(data['24h_avg'])
        cache.set('usd_per_btc', usd_per_btc, timeout=300)

    return usd_per_btc
Пример #4
0
def get_usd_per_ltc():
    """Get current exchange rate as a float.

    Caches the exchange rate for five minutes.
    """
    usd_per_ltc = cache.get('usd_per_ltc')

    if usd_per_ltc is None:
        url = 'https://btc-e.com/api/2/ltc_usd/ticker'
        r = requests.get(url)
        data = r.json()
        usd_per_ltc = float(data['ticker']['avg'])
        urlfh.close()
        cache.set('usd_per_ltc', usd_per_ltc, timeout=300)

    return usd_per_ltc
Пример #5
0
def get_image_io(dpr, price, currency, color):
    """Get the StringIO object containing the image.

    Also cached, with a name containing the BTC price and color."""

    img_name = 'img_{0:f}_{1}_{2}[0]_{2}[1]_{2}[2]_{3}x'.format(price, currency, color, dpr)
    img_io = cache.get(img_name)

    if img_io is None:
        img = generate_image(dpr, price, currency, color)
        img_io = StringIO()
        img_io.write(img)
        img_io.seek(0)
        cache.set(img_name, img_io, timeout=300)

    img_io.seek(0)
    return img_io
Пример #6
0
def get_image_io(price, currency, color):
    """Get the StringIO object containing the image.

    Also cached, with a name containing the BTC price and color."""

    img_name = 'img_{0:f}_{1}_{2}[0]_{2}[1]_{2}[2]'.format(price, currency, color)
    img_io = cache.get(img_name)

    if img_io is None:
        img = generate_image(price, currency, color)
        img_io = StringIO()
        img_io.write(img)
        img_io.seek(0)
        cache.set(img_name, img_io, timeout=300)

    img_io.seek(0)
    return img_io
Пример #7
0
def get_exchange_rate(input_currency, output_currency):
    """Gets the exchange rate as output_currency per input_currency.

    Caches the result for five minutes.
    """
    input_currency = input_currency.lower()
    output_currency = output_currency.lower()
    key = '%s_per_%s' % (output_currency, input_currency)
    rate = cache.get(key)
    if rate is None:
        if output_currency == 'btc':
            rate = get_btc_rate(input_currency)
        elif output_currency == 'ltc' and input_currency == 'usd':
            rate = get_ltc_per_usd()
        else:
            raise ValueError('unsupported currency pair')
        cache.set(key, rate, timeout=300)
        # TODO: cache the inverse while we have it?
    return rate