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 handle_delimited_query(query): """Process sub-commands. Args: query (str): User query """ # Currencies or decimal places if query.endswith(DELIMITER): # User deleted trailing space run_trigger('config') # subprocess.call(['osascript', '-e', ALFRED_AS]) # return mode, query = [s.strip() for s in query.split(DELIMITER)] if mode == 'currencies': currencies = sorted([(name, symbol) for (symbol, name) in CURRENCIES.items()] + [(name, symbol) for (symbol, name) in CRYPTO_CURRENCIES.items()]) if query: currencies = wf.filter(query, currencies, key=lambda t: ' '.join(t), match_on=MATCH_ALL ^ MATCH_ALLCHARS, min_score=30) else: # Show last update time age = wf.cached_data_age(CURRENCY_CACHE_NAME) if age > 0: # Exchange rates in cache td = timedelta(seconds=age) wf.add_item('Exchange rates updated {}'.format( human_timedelta(td)), icon=ICON_INFO) if not currencies: wf.add_item('No matching currencies', 'Try a different query', icon=ICON_WARNING) for name, symbol in currencies: wf.add_item(u'{} // {}'.format(name, symbol), u'Use `{}` in conversions'.format(symbol), copytext=symbol, valid=False, icon=ICON_CURRENCY) wf.send_feedback()
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