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