def store(): from api.kv_store import KvStore, session now = datetime.now() key = "feeds_{}_{}".format(now.strftime('%Y-%m-%d'), now.month / 6) existing = session.query(KvStore.id).filter(KvStore.key == key).count() if existing == 0: log.info("Staring cache of {}...".format(key)) feeds = {} for url in default_feeds: r = requests.get(url) feeds[url] = r.text if r.status_code != 200: log.error("HTTP error code {} for {}".format( r.status_code, url)) new_store = KvStore(key, feeds) session.add(new_store) session.commit() log.info("Feed Data saved to db with key {}".format(key)) else: feeds = session.query(KvStore).filter(KvStore.key == key).one().value key_x = key + '_x' existing = session.query(KvStore.id).filter(KvStore.key == key_x).count() if existing == 0: log.info("Extracting feed data...") feeds_x = {} for url in default_feeds: data = feeds[url] feed = Feed(feed=data) feed.extract() feeds_x[url] = json.dumps(feed.serializable()) new_store_x = KvStore(key_x, feeds_x) session.add(new_store_x) session.commit() log.info("Processed data saved to db with key {}".format(key)) return json.dumps({'data': key, 'processed': key_x}), 200
def feeds(): from api.kv_store import KvStore, session url = request.form.get('url') cached = session.query(KvStore).filter( KvStore.key.like("%_x")).order_by(KvStore.id.desc()).first() return cached.value[url], 200 abort(400)
def feeds(): from api.kv_store import KvStore, session url = request.form.get('url') cached = session.query(KvStore).filter(KvStore.key.like("%_x")).order_by( KvStore.id.desc()).first() return cached.value[url], 200 abort(400)