From 9cf933723fe67d990af8d1f9cd2c1d44dbaabb85 Mon Sep 17 00:00:00 2001 From: pictuga Date: Thu, 19 Mar 2020 11:35:02 +0100 Subject: [PATCH] feeds: clean up time handling Includes a shameful fix on @property --- morss/feedify.ini | 2 +- morss/feeds.py | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/morss/feedify.ini b/morss/feedify.ini index 48240c4..d28a1e0 100644 --- a/morss/feedify.ini +++ b/morss/feedify.ini @@ -73,7 +73,7 @@ item_updated = atom03:updated mode = json mimetype = application/json -timeformat = YYYY-MM-DDTHH:MM:SS +timeformat = %Y-%m-%dT%H:%M:%SZ base = {} title = title diff --git a/morss/feeds.py b/morss/feeds.py index 03ce45c..4f5e964 100644 --- a/morss/feeds.py +++ b/morss/feeds.py @@ -232,10 +232,12 @@ class ParserBase(object): # format try: time = parse_time(x) - return time.strftime(self.rules['timeformat']) + return time.strftime(self.rules.get('timeformat', self.default_timeformat)) except ValueError: pass + default_timeformat = "%D" + # HELPERS def get_raw(self, rule_name): @@ -466,11 +468,14 @@ class ParserHTML(ParserXML): def parse_time(value): if isinstance(value, basestring): if re.match(r'^[0-9]+$', value): - return datetime.fromtimestamp(int(value), tz.tzutc()) + return datetime.fromtimestamp(int(value), tz.UTC) + else: - return dateutil.parser.parse(value, tzinfos=tz.tzutc) + return dateutil.parser.parse(value) + elif isinstance(value, int): - return datetime.fromtimestamp(value, tz.tzutc()) + return datetime.fromtimestamp(value, tz.UTC) + elif isinstance(value, datetime): return value else: @@ -661,12 +666,12 @@ class Item(Uniq): lambda f,x: f.set_str('item_content', x), lambda f: f.rmv('item_content') ) time = property( - lambda f: f.time_fmt(f.get_str('item_time')), - lambda f,x: f.set_str('title', f.time_prs(x)), + lambda f: f.time_prs(f.get_str('item_time')), + lambda f,x: f.set_str('item_time', f.time_fmt(x)), lambda f: f.rmv('item_time') ) updated = property( - lambda f: f.time_fmt(f.get_str('item_updated')), - lambda f,x: f.set_str('updated', f.time_prs(x)), + lambda f: f.time_prs(f.get_str('item_updated')), + lambda f,x: f.set_str('item_updated', f.time_fmt(x)), lambda f: f.rmv('item_updated') )