From 4ba4d73ce6e9f41a355d87baab9ef3ee69f5fb88 Mon Sep 17 00:00:00 2001 From: pictuga Date: Tue, 17 Mar 2020 14:02:01 +0100 Subject: [PATCH] feeds: add json support --- morss/feedify.ini | 22 ++++++++++ morss/feeds.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/morss/feedify.ini b/morss/feedify.ini index b280be4..4be2d1a 100644 --- a/morss/feedify.ini +++ b/morss/feedify.ini @@ -94,6 +94,28 @@ item_updated = atom:updated atom03:updated +[json] +mode = json +path = + N/A + +mimetype = application/json +timeformat = YYYY-MM-DDTHH:MM:SS +base = {} + +title = title +desc = desc +items = items.[] + +item_title = title +item_link = url +item_desc = desc +item_content = content +item_id = id +item_is_permalink = is_permalink +item_time = time +item_updated = updated + [twitter] mode = xpath path = diff --git a/morss/feeds.py b/morss/feeds.py index bad9ede..43e3f89 100644 --- a/morss/feeds.py +++ b/morss/feeds.py @@ -394,6 +394,97 @@ def parse_time(value): return False +class ParserJSON(ParserBase): + ruleset = 'json' + + def parse(self, raw): + return json.loads(raw) + + def remove(self): + # delete oneself FIXME + pass + + def tostring(self): + return json.dumps(self.root, indent=True, ensure_ascii=False) + # ensure_ascii = False to have proper utf-8 string and not \u00 + + def _rule_parse(self, rule): + return rule.split(".") + + def rule_search_all(self, rule): + try: + rrule = self._rule_parse(rule) + cur = self.root + + for node in rrule: + if node == '[]': + break + else: + cur = cur[node] + + return cur if isinstance(cur, list) else [cur,] + + except (AttributeError, KeyError): + return [] + + def rule_create(self, rule): + # create from scracth + rrule = self._rule_parse(rule) + cur = self.root + + for (i, node) in enumerate(rrule): + if rrule[i+1] == '[]': + if node in cur and isinstance(cur[node], list): + cur[node].append({}) + + else: + cur[node] = [{}] + + return + + else: + if node in cur: + # yay, go on + cur = cur[node] + else: + # opps need to create + cur[node] = {} + + def rule_remove(self, rule): + if '[]' in rule: + raise ValueError('not supported') # FIXME + + rrule = self._rule_parse(rule) + cur = self.root + + for node in rrule[:-1]: + cur = cur[node] + + del cur[rrule[-1]] + + def rule_set(self, rule, value): + if '[]' in rule: + raise ValueError('not supported') # FIXME + + rrule = self._rule_parse(rule) + cur = self.root + + for node in rrule[:-1]: + cur = cur[node] + + cur[rrule[-1]] = value + + def rule_str(self, rule): + out = self.rule_search(rule) + return str(out).replace('\n', '
') if out else out + + def bool_prs(self, x): + return (x or '').lower() != 'false' + + def bool_fmt(self, x): + return str(bool(x)) + + class Uniq(object): _map = {} _id = None @@ -518,3 +609,20 @@ class FeedXML(Feed, ParserXML): class ItemXML(Item, ParserXML): pass + + +class FeedJSON(Feed, ParserJSON): + itemsClass = 'ItemJSON' + + +class ItemJSON(Item, ParserJSON): + def remove(self): + rrule = self._rule_parse(self.rules['items']) + cur = self.parent.root + + for node in rrule: + if node == '[]': + cur.remove(self.root) + return + + cur = cur[node]