0.9beta
[feedingit] / src / FeedingIt-Web.py
1 import BaseHTTPServer
2 import sys
3 from rss_sqlite import Listing
4 from xml import sax
5 from cgi import escape
6 from re import sub
7 from htmlentitydefs import name2codepoint
8
9 CONFIGDIR = "/home/user/.feedingit/"
10
11 def unescape(text):
12     def fixup(m):
13         text = m.group(0)
14         if text[:2] == "&#":
15             # character reference
16             try:
17                 if text[:3] == "&#x":
18                     return unichr(int(text[3:-1], 16))
19                 else:
20                     return unichr(int(text[2:-1]))
21             except ValueError:
22                 pass
23         else:
24             # named entity
25             try:
26                 text = unichr(name2codepoint[text[1:-1]])
27             except KeyError:
28                 pass
29         return text # leave as is
30     return sub("&#?\w+;", fixup, text)
31
32 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
33     
34     def openTaskSwitch(self):
35         import subprocess
36         subprocess.Popen("dbus-send /com/nokia/hildon_desktop com.nokia.hildon_desktop.exit_app_view", shell=True)
37     
38     def getConfigXml(self):
39         xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
40         xml += "<hideReadFeed>True</hideReadFeed>"
41         xml += "<hideReadArticles>True</hideReadArticles>"
42         xml += "</xml>"
43         return xml
44     
45     def generateCategoryXml(self):
46         xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
47         for cat in listing.getListOfCategories():
48             xml += "<category>"
49             xml += "<catname>%s</catname>" %listing.getCategoryTitle(cat)
50             xml += "<catid>%s</catid>" % cat
51             xml += "</category>"
52         xml += "</xml>"
53         return xml
54
55     def fix_title(self, title):
56         return escape(unescape(title).replace("<em>","").replace("</em>","").replace("<nobr>","").replace("</nobr>","").replace("<wbr>","").replace("&mdash;","-"))
57     
58     def generateFeedsXml(self, catid):
59         xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
60         for key in listing.getSortedListOfKeys("Manual", category=catid):
61             xml += "<feed>"
62             xml += "<feedname>%s</feedname>" %listing.getFeedTitle(key)
63             xml += "<feedid>%s</feedid>" %key
64             xml += "<unread>%s</unread>" %listing.getFeedNumberOfUnreadItems(key)
65             xml += "<updatedDate>%s</updatedDate>" %listing.getFeedUpdateTime(key)
66             xml += "<icon>%s</icon>" %listing.getFavicon(key)
67             xml += "</feed>"
68         xml += "</xml>"
69         return xml
70     
71     def generateArticlesXml(self, key, onlyUnread, markAllAsRead):
72         feed = listing.getFeed(key)
73         if markAllAsRead=="True":
74             feed.markAllAsRead()
75             listing.updateUnread(key)
76         xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
77         if onlyUnread == "False":
78             onlyUnread = False
79         for id in feed.getIds(onlyUnread):
80             xml += "<article>"
81             xml += "<title>%s</title>" %self.fix_title(feed.getTitle(id))
82             xml += "<articleid>%s</articleid>" %id
83             xml += "<unread>%s</unread>" %str(feed.isEntryRead(id))
84             xml += "<updatedDate>%s</updatedDate>" %feed.getDateStamp(id)
85             xml += "<path>%s</path>" %feed.getContentLink(id)
86             xml += "</article>"
87         xml += "</xml>"
88         return xml
89
90     def do_GET(self):
91         (req, sep, arg) = self.path.partition("?")
92         request = req.split("/")
93         arguments = {}
94         if arg != "":
95             args = arg.split("&")
96             print args
97             for arg in args:
98                 ele = arg.split("=")
99                 print ele
100                 #try:
101                 arguments[ele[0]] = ele[1]
102                 #except:
103                 #    pass
104         if request[1] == "categories":
105             xml = self.generateCategoryXml()
106         elif request[1] == "feeds":
107             catid = request[2]
108             xml = self.generateFeedsXml(catid)
109         elif request[1] == "articles":
110             key = request[2]
111             onlyUnread = arguments.get("onlyUnread","False")
112             markAllAsRead = arguments.get("markAllAsRead", "False")
113             xml = self.generateArticlesXml(key, onlyUnread, markAllAsRead)
114         elif request[1] == "html":
115             key = request[2]
116             article = request[3]
117             feed = listing.getFeed(key)
118             file = open(feed.getContentLink(article))
119             feed.setEntryRead(article)
120             html = file.read().replace("body", "body bgcolor='#ffffff'", 1)
121             file.close()
122             self.send_response(200)
123             self.send_header("Content-type", "text/html")
124             self.end_headers()
125             self.wfile.write(html)
126             listing.updateUnread(key)
127             return
128         elif request[1] == "config":
129             xml = self.getConfigXml()
130         elif request[1] == "home":
131             file = open(self.path)
132             self.send_response(200)
133             self.send_header("Content-type", "text/html")
134             self.end_headers()
135             self.wfile.write(file.read())
136             file.close()
137             return
138         elif request[1] == "task":
139             self.openTaskSwitch()
140             xml = "<xml>OK</xml>"
141         else:
142             self.send_error(404, "File not found")
143             return
144         self.send_response(200)
145         self.send_header("Content-type", "text/xml")
146         self.end_headers()
147         self.wfile.write(xml.encode("utf-8"))
148
149 PORT = 8000
150
151 listing = Listing(CONFIGDIR)
152
153 httpd = BaseHTTPServer.HTTPServer(("127.0.0.1", PORT), Handler)
154 print "serving at port", PORT
155 httpd.serve_forever()