print date in interactive prompt

This commit is contained in:
Christopher Teutsch 2019-05-03 23:16:00 +02:00
parent 357c403d8b
commit 6afbc59ef4
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D

59
crawl.py Normal file → Executable file
View File

@ -16,7 +16,8 @@ from datetime import timedelta as DTTimeDelta
from dateutil.relativedelta import FR, relativedelta
import appdirs
import crawl2
import utils
import mechanize as m
CARD_MASTERCARD = ['0']
@ -69,31 +70,31 @@ vals_group.add_argument(
help='Amount',
nargs='?'
)
# args = parser.parse_args('USD 1000'.split())
args = parser.parse_args()
#logger = logging.getLogger('mechanize')
#logger.addHandler(logging.StreamHandler(sys.stdout))
#logger.setLevel(logging.DEBUG)
def _process_stdin(argv: str) -> None:
if argv == 'q':
sys.exit()
argv = argv.split()
def _process_stdin(argv: str, res: utils.CurrencyResult) -> None:
argv=argv.split()
try:
if is_float(argv[0]):
if argv[0] in ['q', 'exit', 'quit']:
sys.exit()
elif argv[0] in ['date', 'd']:
print(res.date)
elif is_float(argv[0]):
# amount first -> convert to currency in argv[1]
print(fmt_and_calc(
cur=argv[1].upper(),
amt=float(argv[0]),
res=results,
res=res,
direction=DIRECTION_FROM_EUR))
elif is_float(argv[1]):
# currency first -> convert to EUR
print(fmt_and_calc(
cur=argv[0].upper(),
amt=float(argv[1]),
res=results,
res=res,
direction=DIRECTION_TO_EUR))
except IndexError:
if len(argv) == 0:
if argv is None:
pass
else:
print("Too few arguments: '" + " ".join(argv) + "'")
@ -106,7 +107,7 @@ def is_float(string: str) -> bool:
return False
def _retrieve_file(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO: # pylint: disable=dangerous-default-value
print('Downloading newest rates...')
b = m.Browser()
# Firefox 64 User-Agent
# ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0'
@ -114,9 +115,9 @@ def _retrieve_file(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO:
# Ignore robots.txt
# b.set_handle_robots(False)
# Debugging flags
b.set_debug_http(True)
b.set_debug_redirects(True)
b.set_debug_responses(True)
# b.set_debug_http(True)
# b.set_debug_redirects(True)
# b.set_debug_responses(True)
# PDF URL
b.open('https://misc.firstdata.eu/CurrencyCalculator/fremdwaehrungskurse/pdf')
@ -130,6 +131,7 @@ def _retrieve_file(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO:
rq = fm_i.click(name='submitButton', coord=(random.randint(1, 119), random.randint(1, 20)))
rq.add_header('Accept', '*/*')
rp = b.retrieve(rq)
print('Done.')
return open(rp[0], 'rb')
def _get_date() -> DTDate:
@ -140,7 +142,7 @@ def _get_date() -> DTDate:
return date
def _parse_date_from_args(date_str: str) -> DTDate:
return DTDateTime.strptime(date_str).date()
def calc_result(amt: float, rate: crawl2.Rate, direction: int, duty: float = 0) -> float:
def calc_result(amt: float, rate: utils.Rate, direction: int, duty: float = 0) -> float:
if direction == DIRECTION_FROM_EUR:
result = amt * rate.ask / 1+duty
elif direction == DIRECTION_TO_EUR:
@ -148,9 +150,9 @@ def calc_result(amt: float, rate: crawl2.Rate, direction: int, duty: float = 0)
else:
raise ValueError('direction must be DIRECTION_FROM_EUR or DIRECTION_TO_EUR')
return result
def fmt_and_calc(amt: float, cur: str, res: crawl2.CurrencyResult, direction: str) -> str:
def fmt_and_calc(amt: float, cur: str, res: utils.CurrencyResult, direction: str) -> str:
cur = cur.upper()
if cur in results.rates:
if cur in res.rates:
numeric_result = calc_result(amt, res.rates[cur], direction)
if direction == DIRECTION_FROM_EUR:
fmt_vals = ('EUR', round(amt, 2), cur, round(numeric_result, 2))
@ -160,6 +162,12 @@ def fmt_and_calc(amt: float, cur: str, res: crawl2.CurrencyResult, direction: st
else:
return 'Currency %s could not be found' % cur
# args = parser.parse_args('USD 1000'.split())
args = parser.parse_args()
#logger = logging.getLogger('mechanize')
#logger.addHandler(logging.StreamHandler(sys.stdout))
#logger.setLevel(logging.DEBUG)
# determine card type
if args.card_type == 'VISA':
use_card_type = CARD_VISA
@ -184,22 +192,23 @@ if not filepath.exists():
filename = filepath / (retrieve_date.strftime('%Y%m%d') + '.pdf')
if os.path.exists(filename):
with open(filename, 'rb') as f:
results = crawl2.get_results_from_pdf(f)
results = utils.get_results_from_pdf(f)
else:
buf = _retrieve_file(retrieve_date, card_type=use_card_type)
with open(filename, 'wb') as f:
f.write(buf.read())
buf.seek(0)
results = crawl2.get_results_from_pdf(buf)
results = utils.get_results_from_pdf(buf)
#
# processing
#
if args.interactive:
try:
print('Ready.')
while True:
_process_stdin(input())
except KeyboardInterrupt:
_process_stdin(input('> '), results)
except (KeyboardInterrupt, EOFError):
sys.exit()
else:
print(fmt_and_calc(args.amt, args.currency, results, direction))