print(('Skipping {0}, because there are no price ' + 'entries in gnucash denominated in PLN.').format( cmdt.get_fullname())) continue quote = mstfun.get_fund_price( unidecode(cmdt.get_fullname().decode('utf8'))) if not quote: print( ('Skipping {0}, because its price is not ' + 'available on Bossa.pl.').format(cmdt.get_fullname())) continue if pl.get_time() >= quote[1]: print(('Skipping {0}, because its latest price ' + 'is not older ({1}) than reference ({2}).').format( cmdt.get_fullname(), pl.get_time(), quote[1])) continue print('Updating {0} with price {1} at {2}.'.format( cmdt.get_fullname(), quote[0], quote[1])) p_new = pl.clone(book) p_new = gnucash.GncPrice(instance=p_new) p_new.set_time(quote[1]) v = p_new.get_value() v.num = (quote[0] * v.denom).to_integral() p_new.set_value(v) pdb.add_price(p_new) session.save() finally: session.end() session.destroy()
def update_prices(session, base_currency, offset, all_commodities, currencies, prices): logging.debug("Updating prices") ret = True update_commodities = {} if base_currency is None: root = session.book.get_root_account() base_currency = root.get_children_sorted()[0].GetCommodity( ).get_mnemonic() base_currency = currencies[base_currency] logging.debug("Base currency = %s", base_currency.get_mnemonic()) if offset is None: offset = 0 now_adjusted = now - timedelta(days=offset) while now_adjusted.isoweekday() in [6, 7]: now_adjusted -= timedelta(days=1) logging.debug("Date offset = %d (%s)", offset, now_adjusted) for (key, commodity) in all_commodities.items(): if commodity == base_currency: continue if key not in prices: logging.debug("Need to update %s (no prices)", _cty_desc(commodity)) update_commodities[key] = commodity elif now_adjusted - prices[key] > timedelta(days=0): logging.debug("Need to update %s not updated for %s (since %s)", _cty_desc(commodity), now_adjusted - prices[key], prices[key]) update_commodities[key] = commodity else: logging.debug("Price data for %s updated on %s", _cty_desc(commodity), prices[key]) pdb = session.book.get_price_db() for (key, commodity) in update_commodities.items(): if commodity.is_currency(): lookup = [ b"currency", commodity.get_mnemonic(), base_currency.get_mnemonic() ] else: lookup = [ gnc_quote_source_get_internal_name( commodity.get_quote_source()).encode("utf-8"), key[1] ] logging.info("Updating %s", _cty_desc(commodity)) try: result = quote_lookup(lookup) except Exception as e: logging.critical("Unable to get data for %s", _cty_desc(commodity)) for line in traceback.format_exc().strip().split("\n"): logging.critical("%s", line) ret = False continue if result is None: logging.warn("No data for %s", _cty_desc(commodity)) else: tz = commodity.get_quote_tz() if tz: result["ts"] = pytz.timezone( commodity.get_quote_tz()).localize(result["ts"]) else: result["ts"] = local_tz.localize(result["ts"]) if key in prices and result["ts"].date() <= prices[key]: logging.warn("Ignoring old data for %s", _cty_desc(commodity)) result = None if result is not None: price = gnucash.GncPrice(instance=gnucash.gnucash_core_c. gnc_price_create(session.book.instance)) price.set_commodity(commodity) price.set_currency(currencies[result["currency"]]) price.set_source_string("Finance::Quote") price.set_typestr(result["type"]) ts = datetime(result["ts"].year, result["ts"].month, result["ts"].day, 12, tzinfo=pytz.utc).astimezone(local_tz) price.set_time64(ts) value = Fraction.from_float( result["price"]).limit_denominator(1000000000) price.set_value( gnucash.GncNumeric(value.numerator, value.denominator)) pdb.add_price(price) logging.info("Updated %s", _cty_desc(commodity)) logging.debug("Updated prices") return ret