def update_exchange_rates(): exchange_table = datastore.get_table('exchange_rates') max_date = max( [r.get('date').to_datetime_utc() for r in exchange_table.query()]) start_date = max_date + datetime.timedelta(1) req_fmt_str = "http://openexchangerates.org/api/historical/{}.json?app_id=36646cf83ce04bc1af40246f9015db65" req_strs = [ req_fmt_str.format( (start_date + datetime.timedelta(i)).strftime('%Y-%m-%d')) for i in range((datetime.datetime.today() - start_date).days) ] responses = [requests.get(rs) for rs in req_strs] json_content = [json.loads(r.content) for r in responses] good_content = [{ 'date': dropbox.datastore.Date.from_datetime_utc( datetime.datetime.fromtimestamp(j['timestamp']) - datetime.timedelta(hours=23)), 'rate': j['rates']['GBP'] } for j in json_content] print(good_content) for xr in good_content: exchange_table.insert(**xr) datastore.commit()
def create_records(filename, field_dict, start, end, use_minus=True,date_parse=None): acct_id = choose_account() trans_prototype = {'Date':'', 'Category':'', 'Amount':0, 'Account':'', 'Note':''} with open(filename,'r') as f: recs = [l for l in reader(f)] for rec in recs[start:end]: # print(rec) r_dict = dict(trans_prototype) if date_parse: r_dict['Date'] = datetime.datetime.strptime(rec[field_dict['Date']], date_parse) else: r_dict['Date'] = parser.parse(rec[field_dict['Date']]) r_dict['Date'] = calendar.timegm(r_dict['Date'].timetuple()) r_dict['Date'] = dropbox.datastore.Date(r_dict['Date']) r_dict['Note'] = rec[field_dict['Note']] r_dict['Category'] = rec[field_dict['Category']] r_dict['Account'] = acct_id if rec[field_dict['credit']].strip(): r_dict['Amount'] = float(rec[field_dict['credit']].strip().replace(',','')) else: r_dict['Amount'] = float(rec[field_dict['debit']].strip().replace(',','')) if use_minus: r_dict['Amount'] *= -1 transaction_table.insert(**r_dict) datastore.commit()
def add_exchange_rate_date(dates): if isinstance(dates, basestring): dates = [dates] exchange_table = datastore.get_table('exchange_rates') req_fmt_str = "http://openexchangerates.org/api/historical/{}.json?app_id=36646cf83ce04bc1af40246f9015db65" req_strs = [req_fmt_str.format(date) for date in dates] responses = [requests.get(rs) for rs in req_strs] json_content = [json.loads(r.content) for r in responses] good_content = [{ 'date': dropbox.datastore.Date.from_datetime_utc(datetime.datetime.utcfromtimestamp(j['timestamp'])-datetime.timedelta(hours=23)), 'rate':j['rates']['GBP']} for j in json_content] print(good_content) for xr in good_content: exchange_table.insert(**xr) datastore.commit()
def update_exchange_rates(): exchange_table = datastore.get_table('exchange_rates') max_date = max([r.get('date').to_datetime_utc() for r in exchange_table.query()]) start_date = max_date + datetime.timedelta(1) req_fmt_str = "http://openexchangerates.org/api/historical/{}.json?app_id=36646cf83ce04bc1af40246f9015db65" req_strs = [req_fmt_str.format((start_date + datetime.timedelta(i)).strftime('%Y-%m-%d')) for i in range((datetime.datetime.today() - start_date).days)] responses = [requests.get(rs) for rs in req_strs] json_content = [json.loads(r.content) for r in responses] good_content = [{ 'date': dropbox.datastore.Date.from_datetime_utc(datetime.datetime.fromtimestamp(j['timestamp'])-datetime.timedelta(hours=23)), 'rate':j['rates']['GBP']} for j in json_content] print(good_content) for xr in good_content: exchange_table.insert(**xr) datastore.commit()
def add_exchange_rate_date(dates): if isinstance(dates, basestring): dates = [dates] exchange_table = datastore.get_table('exchange_rates') req_fmt_str = "http://openexchangerates.org/api/historical/{}.json?app_id=36646cf83ce04bc1af40246f9015db65" req_strs = [req_fmt_str.format(date) for date in dates] responses = [requests.get(rs) for rs in req_strs] json_content = [json.loads(r.content) for r in responses] good_content = [{ 'date': dropbox.datastore.Date.from_datetime_utc( datetime.datetime.utcfromtimestamp(j['timestamp']) - datetime.timedelta(hours=23)), 'rate': j['rates']['GBP'] } for j in json_content] print(good_content) for xr in good_content: exchange_table.insert(**xr) datastore.commit()
def create_records(filename, field_dict, start, end, use_minus=True, date_parse=None): acct_id = choose_account() trans_prototype = { 'Date': '', 'Category': '', 'Amount': 0, 'Account': '', 'Note': '' } with open(filename, 'r') as f: recs = [l for l in reader(f)] for rec in recs[start:end]: # print(rec) r_dict = dict(trans_prototype) if date_parse: r_dict['Date'] = datetime.datetime.strptime( rec[field_dict['Date']], date_parse) else: r_dict['Date'] = parser.parse(rec[field_dict['Date']]) r_dict['Date'] = calendar.timegm(r_dict['Date'].timetuple()) r_dict['Date'] = dropbox.datastore.Date(r_dict['Date']) r_dict['Note'] = rec[field_dict['Note']] r_dict['Category'] = rec[field_dict['Category']] r_dict['Account'] = acct_id if rec[field_dict['credit']].strip(): r_dict['Amount'] = float(rec[field_dict['credit']].strip().replace( ',', '')) else: r_dict['Amount'] = float(rec[field_dict['debit']].strip().replace( ',', '')) if use_minus: r_dict['Amount'] *= -1 transaction_table.insert(**r_dict) datastore.commit()