From e4942fdab9a43712d56b38dbdb8421d38d7743df Mon Sep 17 00:00:00 2001 From: makefu Date: Tue, 31 Mar 2020 22:23:38 +0200 Subject: ebknotify: add feed --- ebknotify/cli.py | 59 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'ebknotify/cli.py') diff --git a/ebknotify/cli.py b/ebknotify/cli.py index ef6f99a..ad7d0b7 100644 --- a/ebknotify/cli.py +++ b/ebknotify/cli.py @@ -1,48 +1,63 @@ -""" usage: ebk-notify [options] CONFIG +""" usage: ebk-notify [options] options: --lol=LOL Log Level [Default: INFO] + --config=FILE Path to config file [Default: ~/.config/ebk-notify/ebk.yml] + --filter=FILE Path to separate filter file, + if not set ebk-notify will check in config unter the 'items' key + --cache=FILE Path to cache file [Default: ~/.config/ebk-notify/cache.json] + --atom Write atom files + --outdir=DIR Write atom files to folder [Default: ~/.config/ebk-notify/feeds] """ + from docopt import docopt from .client import EbkClient -from .common import set_lol -import yaml +from .common import set_lol,load_config +from .cache import Cache +from .feed import Feed import json -import dateutil -import dateutil.parser -from datetime import datetime -from dateutil.tz import tzlocal import logging - +from os.path import join log = logging.getLogger('ebk-notify') def main(): args = docopt(__doc__) set_lol(args['--lol'],log) - - config = yaml.safe_load(open(args['CONFIG'])) + config = load_config(args['--config']) api = EbkClient(config['main']['appid'], config['main']['apppw'], config['main']['userid'], config['main']['userpw'] ) - for item in config['items']: - log.info(f"For Search \"{item['name']}\"") + + if args['--filter']: + log.info(f"--filter set, trying to load filters from {args['--filter']}") + items = load_config(args['--filter']) + else: + log.info("loading filter from configurtion['items']") + items = config['items'] + cache = Cache(args['--cache']) + + for item in items: + name = item['name'] + log.info(f"For Search \"{name}\"") del item['name'] + + if not 'adType' in item: + item['adType'] = 'OFFERED' if 'distance' in item and 'distanceUnit' not in item: item['distanceUnit'] = 'KM' ads = api.get_ads(**item) - now = datetime.now(tzlocal()) + item['name'] = name for ad in ads: id = ad.get('id', 0) log.debug(json.dumps(ad,indent=4)) - creation = ad.get('start-date-time', {}).get('value', '').encode('utf-8') - price = ad.get('price',{ 'amount': {}})['amount'].get('value',0) - url = ad['link'][1]['href'] - d = dateutil.parser.parse(creation) - age_in_h = round((now - d).total_seconds() / 3600,2) - - title = ad.get('title', {}).get('value', '').encode('utf-8') - title_unescaped = api.html_unescape(title) - log.info( f"{age_in_h}h ago: \"{title_unescaped}\" for {price}€ -> {url}") + ad = cache.update(ad,item) + e = ad['notify'] + print( f"{e['age_in_h']}h ago: \"{e['title-unescaped']}\" for {e['price']}€ -> {e['url']}") + cache.save() + if args['--atom']: + f = Feed() + f.load_cache(cache) + print(f.to_atom_file(join(args['--outdir'],f'{f.ident}.atom'))) -- cgit v1.2.3