working thing
authorMart Lubbers <mart@martlubbers.net>
Mon, 5 May 2014 13:45:22 +0000 (15:45 +0200)
committerMart Lubbers <mart@martlubbers.net>
Mon, 5 May 2014 13:45:22 +0000 (15:45 +0200)
program/hypfront/hyper.py

index 4405580..e773f86 100644 (file)
@@ -2,8 +2,9 @@
 # -*- coding: utf-8 -*-
 
 import feedparser
-import HTMLParser
-import feed2html
+import urllib
+import pprint
+import re
 from mod_python import apache
 
 
@@ -11,7 +12,8 @@ def req_pre(req):
     req.log_error('handler')
     req.content_type = 'text/html'
     req.send_http_header()
-    req.write("""<html>
+    req.write("""\
+<html>
     <head>
         <title>HyperFrontend RSS feed input</title>
     </head>
@@ -20,12 +22,27 @@ def req_pre(req):
 
 
 def req_post(req):
-    req.write("""
+    req.write("""\
     </body>
 </html>
 """)
 
 
+def feed2html(req, url, name):
+    url = urllib.unquote(url)
+    url = url if re.match('https?://', url) else 'http://{}'.format(url)
+    req.write('\tLoading "{}" as "{}"<br />\n'.format(url, name))
+    feed = feedparser.parse(url)
+    channel = feed.feed
+    req.write('\t<table border="1">\n')
+    req.write('\t\t<tr><th>Title</th><th>...</th></tr>\n')
+    for i in feed.entries:
+        req.write('\t\t<tr><td>{}</td><td>...</td></tr>\n'.
+                  format(i['title'].encode('ascii', 'xmlcharrefreplace')))
+
+    req.write('\t</table>\n')
+
+
 def handler(req):
     req_pre(req)
     args = dict(filter(lambda x: x[1],
@@ -35,6 +52,6 @@ def handler(req):
         req.write('Something went wrong, empty fields?<br />')
         req.write('<a href="index.html">back</a>')
     else:
-        req.write(str(args))
+        feed2html(req, args['url'], args['name'])
     req_post(req)
     return apache.OK