From 94372af868d37ccfca3b8a9a42f40b92b597d270 Mon Sep 17 00:00:00 2001 From: pictuga Date: Fri, 9 Nov 2018 22:04:46 +0100 Subject: [PATCH] feeds: transitional code for json/csv/html export --- morss/feeds.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/morss/feeds.py b/morss/feeds.py index a2b6679..585c5b4 100644 --- a/morss/feeds.py +++ b/morss/feeds.py @@ -157,6 +157,34 @@ class ParserBase(object): # to output in sth fancy (json, csv, html), change class type pass + def tojson(self, indent=None): + # TODO temporary + return json.dumps(OrderedDict(self.iterdic()), indent=indent) + + def tocsv(self): + # TODO temporary + out = StringIO() + c = csv.writer(out, dialect=csv.excel) + + for item in self.items: + row = [getattr(item, x) for x in item.dic] + + if sys.version_info[0] < 3: + row = [x.encode('utf-8') if isinstance(x, unicode) else x for x in row] + + c.writerow(row) + + out.seek(0) + return out.read() + + def tohtml(self): + # TODO temporary + path = os.path.join(os.path.dirname(__file__), 'reader.html.template') + loader = DictLoader({'reader': open(path).read()}) + engine = Engine(loader=loader, extensions=[CoreExtension()]) + template = engine.get_template('reader') + return template.render({'feed': self}).encode('utf-8') + def iterdic(self): for element in self.dic: value = getattr(self, element)