Add a --cached option to list the cards without network access

master
Andy Teijelo Pérez 2017-10-11 09:28:36 -04:00
parent 1331bc0d3b
commit f7b0efee99
1 changed files with 18 additions and 16 deletions

View File

@ -253,34 +253,36 @@ def fetch_usertime(username):
r = session.get("https://secure.etecsa.net:8443/EtecsaQueryServlet?op=getLeftTime&op1={}".format(username)) r = session.get("https://secure.etecsa.net:8443/EtecsaQueryServlet?op=getLeftTime&op1={}".format(username))
return r.text return r.text
def time_left(username, fresh=False): def time_left(username, fresh=False, cached=False):
now = time.time() now = time.time()
with dbm.open(CARDS_DB, "c") as cards_db: with dbm.open(CARDS_DB, "c") as cards_db:
card_info = json.loads(cards_db[username].decode()) card_info = json.loads(cards_db[username].decode())
last_update = card_info.get('last_update', 0) last_update = card_info.get('last_update', 0)
password = card_info['password'] password = card_info['password']
if (now - last_update > 60) or fresh: if not cached:
time_left = fetch_usertime(username) if (now - last_update > 60) or fresh:
last_update = time.time() time_left = fetch_usertime(username)
if re.match(r'[0-9:]+', time_left): last_update = time.time()
card_info['time_left'] = time_left if re.match(r'[0-9:]+', time_left):
card_info['last_update'] = last_update card_info['time_left'] = time_left
cards_db[username] = json.dumps(card_info) card_info['last_update'] = last_update
cards_db[username] = json.dumps(card_info)
time_left = card_info.get('time_left', '-') time_left = card_info.get('time_left', '-')
return time_left return time_left
def expire_date(username, fresh=False): def expire_date(username, fresh=False, cached=False):
# expire date computation won't depend on last_update # expire date computation won't depend on last_update
# because the expire date will change very infrequently # because the expire date will change very infrequently
# in the case of rechargeable accounts and it will # in the case of rechargeable accounts and it will
# never change in the case of non-rechargeable cards # never change in the case of non-rechargeable cards
with dbm.open(CARDS_DB, "c") as cards_db: with dbm.open(CARDS_DB, "c") as cards_db:
card_info = json.loads(cards_db[username].decode()) card_info = json.loads(cards_db[username].decode())
if (not 'expire_date' in card_info) or fresh: if not cached:
password = card_info['password'] if (not 'expire_date' in card_info) or fresh:
exp_date = fetch_expire_date(username, password) password = card_info['password']
card_info['expire_date'] = exp_date exp_date = fetch_expire_date(username, password)
cards_db[username] = json.dumps(card_info) card_info['expire_date'] = exp_date
cards_db[username] = json.dumps(card_info)
exp_date = card_info['expire_date'] exp_date = card_info['expire_date']
return exp_date return exp_date
@ -311,8 +313,8 @@ def cards(args):
print("{}\t{}\t{}\t(expires {})".format( print("{}\t{}\t{}\t(expires {})".format(
card, card,
password, password,
time_left(card, args.fresh), time_left(card, args.fresh, args.cached),
expire_date(card, args.fresh) expire_date(card, args.fresh, args.cached)
)) ))
def cards_add(args): def cards_add(args):