summaryrefslogtreecommitdiffstats
path: root/ebknotify/cache.py
diff options
context:
space:
mode:
Diffstat (limited to 'ebknotify/cache.py')
-rw-r--r--ebknotify/cache.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/ebknotify/cache.py b/ebknotify/cache.py
new file mode 100644
index 0000000..fbc4b9b
--- /dev/null
+++ b/ebknotify/cache.py
@@ -0,0 +1,73 @@
+import logging
+from os.path import expanduser,exists
+from datetime import datetime
+from dateutil.tz import tzlocal
+from .common import html_unescape
+import dateutil
+import dateutil.parser
+import json
+
+log = logging.getLogger("cache")
+class Cache():
+
+ def __init__(self,path):
+ self.path = expanduser(path)
+
+ self.load(self.path)
+ # self.reset_init()
+
+ def load(self,path):
+ if not exists(path):
+ log.info(f"{path} as cache does not exist")
+ self.store = {}
+ else:
+ self.store = json.load(open(path))
+ log.debug("loaded {len(ret)} entries from {cachefile}")
+
+ def reset_seen(self):
+ """ reset the store 'seen' field to false for this run """
+ for k in self.store:
+ self.store[k]['notify']['seen'] = False
+
+ def save(self) -> None:
+ cachefile = self.path
+ if not exists(cachefile):
+ log.info(f"{cachefile} does not exist,creating")
+
+ with open(cachefile,"w+") as f:
+ json.dump(self.store,f)
+
+ def update(self,ad,searchquery):
+ now = datetime.now(tzlocal())
+ ident = ad['id']
+ creation = ad.get('start-date-time', {}).get('value', '').encode('utf-8')
+ price = ad.get('price',{ 'amount': {}})['amount'].get('value',0)
+ 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 = html_unescape(title)
+ try:
+ img = ad['pictures']['picture'][0]['link'][4]['href']
+ except Exception as e:
+ print(e)
+ img = None
+ # TODO: fetch detailed article
+ ad['notify'] = {
+ 'filter': searchquery,
+ 'creation': d.isoformat(),
+ 'filter-name': searchquery['name'],
+ 'first-seen': now.isoformat(),
+ 'title-unescaped': title_unescaped,
+ 'age_in_h': age_in_h,
+ 'price': price,
+ 'image': img,
+ 'url': ad['link'][1]['href'],
+ 'seen': True
+ }
+ if ident in self.store:
+ log.debug(f"{ident} already in store, updating")
+ ad['notify']['first-seen'] = self.store[ident]['notify']['first-seen'] # saving first_seen from cache
+ else:
+ log.debug(f"{ident} is new, adding")
+ self.store[ident] = ad
+ return ad