def fetch_exchange_rates(): """Retrieve all currency exchange rates. Batch currencies into requests of `SYMBOLS_PER_REQUEST` currencies each. Returns: list: List of `{abbr : n.nn}` dicts of exchange rates (relative to EUR). """ rates = {} futures = [] active = load_active_currencies() syms = [s for s in CURRENCIES.keys() if s in active] if not OPENX_APP_KEY: log.warning('fetching limited set of fiat currency exchange rates: ' 'APP_KEY for openexchangerates.org not set. ' 'Please sign up for a free account here: ' 'https://openexchangerates.org/signup/free') jobs = [(load_xra_rates, (syms, ))] else: jobs = [(load_openx_rates, (syms, ))] syms = [] for s in CRYPTO_CURRENCIES.keys(): if s in CURRENCIES: log.warning( 'ignoring crytopcurrency "%s", as it conflicts with ' 'a fiat currency', s) continue if s in active: syms.append(s) for symbols in grouper(SYMBOLS_PER_REQUEST, syms): jobs.append((load_cryptocurrency_rates, (symbols, ))) # fetch data in a thread pool pool = Pool(2) for job in jobs: futures.append(pool.apply_async(*job)) pool.close() pool.join() for f in futures: for currency, rate in f.get().iteritems(): if is_valid_currency(currency) and is_valid_exchange_rate(rate): rates[currency] = rate log.debug("Currency %s has new rate %s", currency, rate) else: log.warn("Got invalid rate update for currency '%s' to '%s'", currency, rate) return rates
def fetch_currency_rates(): """Retrieve all currency exchange rates. Batch currencies into requests of `SYMBOLS_PER_REQUEST` currencies each. Returns: list: List of `{abbr : n.nn}` dicts of exchange rates (relative to EUR). """ rates = {} for symbols in grouper(SYMBOLS_PER_REQUEST, CURRENCIES.keys()): symbols = [s for s in symbols if s] d = load_yahoo_rates(symbols) rates.update(d) return rates
def fetch_exchange_rates(): """Retrieve all currency exchange rates. Batch currencies into requests of `SYMBOLS_PER_REQUEST` currencies each. Returns: list: List of `{abbr : n.nn}` dicts of exchange rates (relative to EUR). """ rates = {} futures = [] active = load_active_currencies() syms = [s for s in CURRENCIES.keys() if s in active] # rates.update(load_openx_rates(syms)) jobs = [(load_openx_rates, (syms, ))] syms = [] for s in CRYPTO_CURRENCIES.keys(): if s in CURRENCIES: log.warning( 'ignoring crytopcurrency "%s", as it conflicts with ' 'a fiat currency', s) continue if s in active: syms.append(s) # syms = [s for s in CRYPTO_CURRENCIES.keys() if s in active] for symbols in grouper(SYMBOLS_PER_REQUEST, syms): jobs.append((load_cryptocurrency_rates, (symbols, ))) # fetch data in a thread pool pool = Pool(2) for job in jobs: futures.append(pool.apply_async(*job)) pool.close() pool.join() for f in futures: rates.update(f.get()) return rates
def fetch_exchange_rates(): """Retrieve all currency exchange rates. Batch currencies into requests of `SYMBOLS_PER_REQUEST` currencies each. Returns: list: List of `{abbr : n.nn}` dicts of exchange rates (relative to EUR). """ rates = {} futures = [] active = load_active_currencies() syms = [s for s in CURRENCIES.keys() if s in active] # rates.update(load_openx_rates(syms)) jobs = [(load_openx_rates, (syms,))] syms = [] for s in CRYPTO_CURRENCIES.keys(): if s in CURRENCIES: log.warning('ignoring crytopcurrency "%s", as it conflicts with ' 'a fiat currency', s) continue if s in active: syms.append(s) # syms = [s for s in CRYPTO_CURRENCIES.keys() if s in active] for symbols in grouper(SYMBOLS_PER_REQUEST, syms): jobs.append((load_cryptocurrency_rates, (symbols,))) # fetch data in a thread pool pool = Pool(2) for job in jobs: futures.append(pool.apply_async(*job)) pool.close() pool.join() for f in futures: rates.update(f.get()) return rates
def fetch_exchange_rates(): """Retrieve all currency exchange rates. Batch currencies into requests of `SYMBOLS_PER_REQUEST` currencies each. Returns: list: List of `{abbr : n.nn}` dicts of exchange rates (relative to EUR). """ rates = {} futures = [] active = load_active_currencies() # batch symbols into groups and interleave requests to the # different services yjobs = [] syms = [s for s in CURRENCIES.keys() if s in active] for symbols in grouper(SYMBOLS_PER_REQUEST, syms): yjobs.append((load_yahoo_rates, (symbols,))) cjobs = [] syms = [sym for sym in CRYPTO_CURRENCIES.keys() if sym in active] for symbols in grouper(SYMBOLS_PER_REQUEST, syms): cjobs.append((load_cryptocurrency_rates, (symbols,))) # fetch data in a thread pool pool = Pool(4) for job in interleave(yjobs, cjobs): futures.append(pool.apply_async(*job)) pool.close() pool.join() for f in futures: rates.update(f.get()) return rates