From 7fbe728f93a17e2f0b040a1e5f88f116ac97efe3 Mon Sep 17 00:00:00 2001 From: pictuga Date: Mon, 13 Jan 2014 00:08:03 +0100 Subject: [PATCH] Feeds: allow json, csv export Uses OrderedDict --- morss/feeds.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/morss/feeds.py b/morss/feeds.py index 2d7f132..86d63a8 100644 --- a/morss/feeds.py +++ b/morss/feeds.py @@ -6,6 +6,17 @@ import dateutil.parser from dateutil import tz import re +from StringIO import StringIO +import json +import csv + +json.encoder.c_make_encoder = None + +try: + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict + Element = etree.Element NSMAP = {'atom': 'http://www.w3.org/2005/Atom', @@ -91,7 +102,7 @@ class FeedBase(object): value = self[element] if isinstance(value, FeedList): - value = [dict(x) for x in value] + value = [OrderedDict(x) for x in value] elif isinstance(value, datetime): value = value.isoformat() @@ -330,6 +341,18 @@ class FeedParser(FeedBase): def tostring(self, **k): return etree.tostring(self.xml.getroottree(), pretty_print=True, **k) + def tojson(self, indent=None): + return json.dumps(OrderedDict(self), indent=indent) + + def tocsv(self): + out = StringIO() + c = csv.writer(out, dialect=csv.excel) + for item in self.items: + row = [x[1].encode('utf-8') if isinstance(x[1], unicode) else x[1] for x in item if isinstance(x[1], basestring)] + c.writerow(row) + out.seek(0) + return out.read() + class FeedParserRSS(FeedParser): """ RSS Parser