From cf1b6dade9d719c8be6acc217634312805435bea Mon Sep 17 00:00:00 2001 From: Christopher Teutsch Date: Mon, 24 Aug 2020 17:20:36 +0200 Subject: [PATCH] Add quiet flag for usage in cronjobs --- README.md | 3 +++ crawl.py | 16 +++++++++++----- utils.py | 21 ++++++++++++--------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index facaa6f..4e7a1d0 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,9 @@ Write the currency results to standard output, formatted as CSV: |ISO4217 abbreviation|Full German name|Asking rate|Bidding rate|Date the rate was valid on| |:---|:---|:---|:---|:---| +#### `-q`, `--quiet` +Do not output informational messages such as "Parsing..." or "Downloading..." + #### `-i`, `--interactive` Calculate interactively on stdin diff --git a/crawl.py b/crawl.py index d895781..a89f906 100755 --- a/crawl.py +++ b/crawl.py @@ -45,6 +45,12 @@ parser.add_argument( action='store_true', help='Write the results to stdout as CSV' ) +parser.add_argument( + '-q', '--quiet', + dest='quiet', + action='store_true', + help='Do not output the \'Downloading...\' and \'Parsing...\' messages.' +) parser.add_argument( '--cache-dir', dest='cache_dir', @@ -141,7 +147,8 @@ def is_float(string: str) -> bool: def _parse_date_from_args(date_str: str) -> DTDate: - return DTDateTime.strptime(date_str).date() + from dateutil.parser import isoparse + return isoparse(date_str).date() def calc_result(amt: float, rate: utils.Rate, direction: int, duty: float = 0) -> float: @@ -202,17 +209,16 @@ if not filepath.exists(): filename = filepath / utils.mk_filename(retrieve_date, use_card_type) if os.path.exists(filename): with open(filename, 'rb') as f: - results = utils.get_results_from_pdf(f) + results = utils.get_results_from_pdf(f, quiet=args.quiet) else: - buf = utils.get_fileio(retrieve_date, card_type=use_card_type) + buf = utils.get_fileio(retrieve_date, card_type=use_card_type, quiet=args.quiet) with open(filename, 'wb') as f: f.write(buf.read()) buf.seek(0) - results = utils.get_results_from_pdf(buf) + results = utils.get_results_from_pdf(buf, quiet=args.quiet) # # processing # - if args.interactive: try: while True: diff --git a/utils.py b/utils.py index c38f61a..5d73a4a 100644 --- a/utils.py +++ b/utils.py @@ -21,7 +21,6 @@ Rate = namedtuple('Rate', ['abbr', 'full_name', 'ask', 'bid', 'date']) CARD_MASTERCARD = ['0'] CARD_VISA = ['1'] - class CurrencyResult: def __init__(self): self.rates = Dict[str, Rate] @@ -80,7 +79,7 @@ def _parse_line(line: str, ctx: CurrencyResult) -> Rate or None: return rate -def get_results_from_text(text: str, currency: str = None) -> CurrencyResult: +def get_results_from_text(text: str, currency: str = None, quiet: bool = False) -> CurrencyResult: rates = OrderedDict() result = CurrencyResult() lines = text.splitlines() @@ -107,19 +106,22 @@ def get_results_from_text(text: str, currency: str = None) -> CurrencyResult: return result -def get_results_from_pdf(buf: BinaryIO or str, currency: str = None) -> CurrencyResult: - print('Parsing data... ', end='', file=stderr) +def get_results_from_pdf(buf: BinaryIO or str, currency: str = None, quiet: bool = False) -> CurrencyResult: + if not quiet: + print('Parsing data... ', end='', file=stderr) reader = PyPDF3.PdfFileReader(buf) text = str() for num in range(0, reader.getNumPages()-1): text += reader.getPage(num).extractText() - print('Done.', file=stderr) - return get_results_from_text(text, currency=currency) + if not quiet: + print('Done.', file=stderr) + return get_results_from_text(text, currency=currency, quiet=quiet) -def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO: # pylint: disable=dangerous-default-value +def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA, quiet: bool = False) -> BinaryIO: # pylint: disable=dangerous-default-value # pylint: disable=no-member # mechanize.Browser has some lazy-loading methods that pylint doesn't see - print('Downloading rates for ' + date.strftime('%Y-%m-%d') + '... ', end='', file=stderr) + if not quiet: + print('Downloading rates for ' + date.strftime('%Y-%m-%d') + '... ', end='', file=stderr) b = m.Browser() # Firefox 64 User-Agent # ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0' @@ -143,7 +145,8 @@ def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO: # py rq = fm.click(name='submitButton', coord=(random.randint(1, 114), random.randint(1, 20))) rq.add_header('Accept', '*/*') rp = b.retrieve(rq) - print(' Done.', file=stderr) + if not quiet: + print(' Done.', file=stderr) # Returns an open file-like object with the PDF as contents return open(rp[0], 'rb')