diff --git a/crawl.py b/crawl.py index e070e42..41baa8d 100755 --- a/crawl.py +++ b/crawl.py @@ -66,6 +66,7 @@ vals_group.add_argument( nargs='?' ) + def _process_stdin(argv: str, res: utils.CurrencyResult) -> None: argv = argv.split() try: @@ -123,6 +124,7 @@ d | date: Print the date which the data is from. except ValueError: print("The currency specified does not exist.") + def is_float(string: str) -> bool: try: float(string) @@ -130,9 +132,11 @@ def is_float(string: str) -> bool: except ValueError: return False + def _parse_date_from_args(date_str: str) -> DTDate: return DTDateTime.strptime(date_str).date() + 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 @@ -142,6 +146,7 @@ def calc_result(amt: float, rate: utils.Rate, direction: int, duty: float = 0) - raise ValueError('direction must be DIRECTION_FROM_EUR or DIRECTION_TO_EUR') return result + def fmt_and_calc(amt: float, cur: str, res: utils.CurrencyResult, direction: str) -> str: cur = cur.upper() if cur in res.rates: diff --git a/utils.py b/utils.py index 1f3c46d..8597f49 100644 --- a/utils.py +++ b/utils.py @@ -19,12 +19,14 @@ Rate = namedtuple('Rate', ['abbr', 'full_name', 'ask', 'bid']) CARD_MASTERCARD = ['0'] CARD_VISA = ['1'] -class CurrencyResult(): + +class CurrencyResult: def __init__(self): self.rates = list() self.card_type = str() self.date = None + def _parse_rate(text: str) -> float or None: if re.match('Keine Kursdaten vorhanden', text): _r = None @@ -37,17 +39,20 @@ def _parse_rate(text: str) -> float or None: _r = None return _r + def _parse_card_type(text: str) -> str: # Method for validating metadata from the PDF against the request data text = text.split(':')[1] text = text.strip('" ') return text + def _parse_date(text: str) -> DTDate: # Method for validating metadata from the PDF against the request data text = text.split(': ')[1].rstrip() return DTDateTime.strptime(text, '%d.%m.%Y').date() + def _array_remove_empty(obj: list) -> List[str]: # just a macro for removing empty or empty-string array objects try: @@ -57,6 +62,7 @@ def _array_remove_empty(obj: list) -> List[str]: return obj return obj + def _parse_line(line: str) -> Rate or None: arr = line.split(" ") # 3 spaces = minimum separation in PDF arr = _array_remove_empty(arr) @@ -97,6 +103,7 @@ def get_results_from_text(text: str, currency: str = None) -> CurrencyResult: result.rates = rates return result + def get_results_from_pdf(buf: BinaryIO or str, currency: str = None) -> CurrencyResult: print('Parsing data... ', end='') reader = PyPDF3.PdfFileReader(buf) @@ -105,6 +112,8 @@ def get_results_from_pdf(buf: BinaryIO or str, currency: str = None) -> Currency text += reader.getPage(num).extractText() print('Done.') return get_results_from_text(text, currency=currency) + + def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA) -> 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='') @@ -134,6 +143,8 @@ def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO: # py print(' Done.') # Returns an open file-like object with the PDF as contents return open(rp[0], 'rb') + + def get_date() -> DTDate: # For Sunday and Monday, use Friday's data; Saturday and Sunday are completely null if DTDate.today().weekday() in [6, 0]: @@ -143,6 +154,7 @@ def get_date() -> DTDate: date = DTDate.today() - DTTimeDelta(1) return date + def mk_filename(date: DTDate, card_type: List[str]) -> str: # List[str] is used because I don't want to make a class for just this if card_type == CARD_MASTERCARD: