Compare commits
6 commits
eb0fd40899
...
6e06bc963a
Author | SHA1 | Date | |
---|---|---|---|
6e06bc963a | |||
12c722bc08 | |||
e289fc4e61 | |||
f7b0efee99 | |||
1331bc0d3b | |||
26a7239020 |
1 changed files with 79 additions and 29 deletions
76
nauta.py
76
nauta.py
|
@ -55,6 +55,13 @@ def parse_time(t):
|
||||||
except:
|
except:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def get_password(username):
|
||||||
|
with dbm.open(CARDS_DB) as cards_db:
|
||||||
|
if not username in cards_db:
|
||||||
|
return None
|
||||||
|
info = json.loads(cards_db[username].decode())
|
||||||
|
return info['password']
|
||||||
|
|
||||||
def select_card():
|
def select_card():
|
||||||
cards = []
|
cards = []
|
||||||
with dbm.open(CARDS_DB) as cards_db:
|
with dbm.open(CARDS_DB) as cards_db:
|
||||||
|
@ -81,12 +88,19 @@ def up(args):
|
||||||
print("Looks like you're already connected. Use 'nauta down' to log out.")
|
print("Looks like you're already connected. Use 'nauta down' to log out.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if args.username:
|
||||||
|
username = args.username
|
||||||
|
password = get_password(username)
|
||||||
|
if password is None:
|
||||||
|
print("Invalid card: {}".format(args.username))
|
||||||
|
return
|
||||||
|
else:
|
||||||
username, password = select_card()
|
username, password = select_card()
|
||||||
if username is None:
|
if username is None:
|
||||||
print("No card available, add one with 'nauta cards add'")
|
print("No card available, add one with 'nauta cards add'")
|
||||||
return
|
return
|
||||||
|
|
||||||
username = username.decode()
|
username = username.decode()
|
||||||
|
|
||||||
tl = time_left(username)
|
tl = time_left(username)
|
||||||
print("Using card {}. Time left: {}".format(username, tl))
|
print("Using card {}. Time left: {}".format(username, tl))
|
||||||
log("Connecting with card {}. Time left on card: {}".format(username, tl))
|
log("Connecting with card {}. Time left on card: {}".format(username, tl))
|
||||||
|
@ -227,9 +241,10 @@ def fetch_expire_date(username, password):
|
||||||
form['password'] = password
|
form['password'] = password
|
||||||
r = session.post(action, form)
|
r = session.post(action, form)
|
||||||
soup = bs4.BeautifulSoup(r.text, 'html.parser')
|
soup = bs4.BeautifulSoup(r.text, 'html.parser')
|
||||||
exp_text = soup.find(string=re.compile("expiración"))\
|
exp_node = soup.find(string=re.compile("expiración"))
|
||||||
.parent.find_next_sibling('td')\
|
if not exp_node:
|
||||||
.text.strip()
|
return "**invalid credentials**"
|
||||||
|
exp_text = exp_node.parent.find_next_sibling('td').text.strip()
|
||||||
exp_text = exp_text.replace('\\', '')
|
exp_text = exp_text.replace('\\', '')
|
||||||
return exp_text
|
return exp_text
|
||||||
|
|
||||||
|
@ -238,12 +253,13 @@ 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 not cached:
|
||||||
if (now - last_update > 60) or fresh:
|
if (now - last_update > 60) or fresh:
|
||||||
time_left = fetch_usertime(username)
|
time_left = fetch_usertime(username)
|
||||||
last_update = time.time()
|
last_update = time.time()
|
||||||
|
@ -254,13 +270,14 @@ def time_left(username, fresh=False):
|
||||||
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 cached:
|
||||||
if (not 'expire_date' in card_info) or fresh:
|
if (not 'expire_date' in card_info) or fresh:
|
||||||
password = card_info['password']
|
password = card_info['password']
|
||||||
exp_date = fetch_expire_date(username, password)
|
exp_date = fetch_expire_date(username, password)
|
||||||
|
@ -296,14 +313,32 @@ 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 verify(username, password):
|
||||||
|
session = requests.Session()
|
||||||
|
r = session.get("https://secure.etecsa.net:8443/")
|
||||||
|
soup = bs4.BeautifulSoup(r.text, 'html.parser')
|
||||||
|
|
||||||
|
form = get_inputs(soup)
|
||||||
|
action = "https://secure.etecsa.net:8443/EtecsaQueryServlet"
|
||||||
|
form['username'] = username
|
||||||
|
form['password'] = password
|
||||||
|
r = session.post(action, form)
|
||||||
|
soup = bs4.BeautifulSoup(r.text, 'html.parser')
|
||||||
|
exp_node = soup.find(string=re.compile("expiración"))
|
||||||
|
if not exp_node:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def cards_add(args):
|
def cards_add(args):
|
||||||
if not args.username:
|
username = args.username or input("Username: ")
|
||||||
username = input("Username: ")
|
|
||||||
password = input("Password: ")
|
password = input("Password: ")
|
||||||
|
if not verify(username, password):
|
||||||
|
print("Credentials seem incorrect")
|
||||||
|
return
|
||||||
with dbm.open(CARDS_DB, "c") as cards_db:
|
with dbm.open(CARDS_DB, "c") as cards_db:
|
||||||
cards_db[username] = json.dumps({
|
cards_db[username] = json.dumps({
|
||||||
'password': password,
|
'password': password,
|
||||||
|
@ -320,7 +355,7 @@ def cards_clean(args):
|
||||||
delete_cards(cards_to_purge)
|
delete_cards(cards_to_purge)
|
||||||
|
|
||||||
def cards_rm(args):
|
def cards_rm(args):
|
||||||
delete_cards(args.cards)
|
delete_cards(args.usernames)
|
||||||
|
|
||||||
def cards_info(args):
|
def cards_info(args):
|
||||||
username = args.username
|
username = args.username
|
||||||
|
@ -364,9 +399,9 @@ def main(args):
|
||||||
epilog=dedent("""\
|
epilog=dedent("""\
|
||||||
Subcommands:
|
Subcommands:
|
||||||
|
|
||||||
up
|
up [username]
|
||||||
down
|
down
|
||||||
cards [-v] [-f]
|
cards [-v] [-f] [-c]
|
||||||
cards add [username]
|
cards add [username]
|
||||||
cards clean
|
cards clean
|
||||||
cards rm username [username ...]
|
cards rm username [username ...]
|
||||||
|
@ -377,6 +412,10 @@ def main(args):
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter
|
formatter_class=argparse.RawDescriptionHelpFormatter
|
||||||
)
|
)
|
||||||
subparsers = parser.add_subparsers()
|
subparsers = parser.add_subparsers()
|
||||||
|
parser.add_argument("-d", "--debug",
|
||||||
|
action="store_true",
|
||||||
|
help="show debug info"
|
||||||
|
)
|
||||||
|
|
||||||
cards_parser = subparsers.add_parser('cards')
|
cards_parser = subparsers.add_parser('cards')
|
||||||
cards_parser.set_defaults(func=cards)
|
cards_parser.set_defaults(func=cards)
|
||||||
|
@ -388,6 +427,10 @@ def main(args):
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="force a fresh request of card time"
|
help="force a fresh request of card time"
|
||||||
)
|
)
|
||||||
|
cards_parser.add_argument("-c", "--cached",
|
||||||
|
action="store_true",
|
||||||
|
help="shows cached data, avoids the network"
|
||||||
|
)
|
||||||
cards_subparsers = cards_parser.add_subparsers()
|
cards_subparsers = cards_parser.add_subparsers()
|
||||||
cards_add_parser = cards_subparsers.add_parser('add')
|
cards_add_parser = cards_subparsers.add_parser('add')
|
||||||
cards_add_parser.set_defaults(func=cards_add)
|
cards_add_parser.set_defaults(func=cards_add)
|
||||||
|
@ -406,11 +449,18 @@ def main(args):
|
||||||
|
|
||||||
up_parser = subparsers.add_parser('up')
|
up_parser = subparsers.add_parser('up')
|
||||||
up_parser.set_defaults(func=up)
|
up_parser.set_defaults(func=up)
|
||||||
|
up_parser.add_argument('username', nargs="?")
|
||||||
|
|
||||||
down_parser = subparsers.add_parser('down')
|
down_parser = subparsers.add_parser('down')
|
||||||
down_parser.set_defaults(func=down)
|
down_parser.set_defaults(func=down)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.debug:
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
from http.client import HTTPConnection
|
||||||
|
HTTPConnection.debuglevel = 2
|
||||||
|
|
||||||
if 'func' in args:
|
if 'func' in args:
|
||||||
args.func(args)
|
args.func(args)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue