gabtv_feed/state.py
2021-03-07 14:10:55 -05:00

41 lines
1.4 KiB
Python

from time import time
import os
from hexlib.db import VolatileState, VolatileBooleanState
from hexlib.log import logger
RECRAWL_HOURS = int(os.environ.get("GTV_RECRAWL_HOURS", 8))
class GabTvState:
def __init__(self, prefix):
self._episodes = VolatileState(prefix)
self._visited = VolatileBooleanState(prefix)
def has_visited(self, item_id):
return self._visited["byid"][item_id]
def mark_visited(self, item_id):
self._visited["byid"][item_id] = True
def has_visited_episode(self, episode):
# TODO: This doesn't actually work because the 'stats' object never actually updates (?!?)
# if episode["stats"]["commentCount"] == 0:
# # No comments, don't need to visit
# return True
# com_count = self._episodes["episodes"][episode["_id"]]
# return not com_count or episode["stats"]["commentCount"] == com_count
last_visited = self._episodes["ep_ts"][episode["_id"]]
if last_visited:
logger.debug("Last seen: %d hours ago" % (int(time() - last_visited) / 3600))
return last_visited and int(time() - last_visited) <= 3600 * RECRAWL_HOURS
def mark_visited_episode(self, episode):
self._episodes["ep_ts"][episode["_id"]] = int(time())
def has_visited_channel(self, channel_id):
return self._visited["channel"][channel_id]
def mark_visited_channel(self, item_id):
self._visited["channel"][item_id] = True