commit 19899f9da5895c6493f499e00cd0feec00732f1f
parent 96b774b6b2233be6b3959e9ceceacdcc91463105
Author: Christoph Lohmann <20h@r-36.net>
Date: Fri, 8 Sep 2017 13:21:59 +0200
twtxt support.
Diffstat:
1 file changed, 43 insertions(+), 0 deletions(-)
diff --git a/zeitungsschau/feed.py b/zeitungsschau/feed.py
@@ -40,6 +40,45 @@ def parsexml(astr):
# Throw XML parsing errors so we can blame the feed authors.
return xml
+def parsetwtxtfeed(astr, uri):
+ feed = {}
+ articles = []
+ now = datetime.now(pytz.utc)
+ now = now.replace(hour=20, minute=20, second=20, microsecond=20)
+
+ feed["title"] = uri
+ feed["link"] = uri
+ feed["updated"] = now
+
+ lines = astr.split("\n");
+ for line in lines:
+ # People already reinterpret the standard. :(
+ if len(line) == 0:
+ continue
+ if line[0] == "#":
+ continue
+
+ createdtxt, ltext = line.split("\t", 1)
+ created = parseiso(createdtxt, now)
+
+ article = {}
+ article["id"] = createdtxt
+ article["title"] = ltext
+ article["text"] = ltext
+ article["uuid"] = createdtxt
+ article["updated"] = created
+
+ if article["updated"] == now:
+ article["uuid"] = ""
+ else:
+ article["uuid"] = "%s" % (article["updated"])
+
+ articles.append(article)
+
+ feed["articles"] = articles
+
+ return feed
+
def parsejsonfeed(astr):
js = json.loads(astr)
@@ -370,9 +409,13 @@ def fetch(uri):
if len(suri) > 1:
if suri[-1] == "json":
ftype = "json"
+ elif suri[-1] == "txt":
+ ftype = "twtxt"
if ftype == "xml":
return (rcode, parseatomfeed(fval))
+ elif ftype == "twtxt":
+ return (rcode, parsetwtxtfeed(fval.decode("utf-8"), uri))
else:
return (rcode, parsejsonfeed(fval.decode("utf-8")))