3 from rss_sqlite import Listing
7 from htmlentitydefs import name2codepoint
8 from gconf import client_get_default
9 from urllib2 import ProxyHandler
10 from threading import Thread
11 from os.path import isfile, isdir, exists
12 from os import mkdir, remove, stat, environ
15 debugging.init(dot_directory=".feedingit", program_name="feedingit-web")
17 CONFIGDIR = environ.get("HOME", "/home/user") + "/.feedingit"
18 #CONFIGDIR = "/home/user/.feedingit/"
21 #commands = [("addFeed","httpwww"), ("openFeed", "xxxx"), ("openArticle", ("feedid","artid"))]
31 return unichr(int(text[3:-1], 16))
33 return unichr(int(text[2:-1]))
39 text = unichr(name2codepoint[text[1:-1]])
42 return text # leave as is
43 return sub("&#?\w+;", fixup, text)
46 from cgi import escape
47 return escape(text).encode('ascii', 'xmlcharrefreplace')
51 listing = Listing(config, CONFIGDIR)
52 httpd = BaseHTTPServer.HTTPServer(("127.0.0.1", PORT), Handler)
56 def addFeed(self, url):
57 commands.append(("addFeed",url))
62 def openFeed(self, key):
63 commands.append( ("openFeed", key) )
65 def openArticle(self, key, id):
66 commands.append( ("openArticle", key, id) )
68 def automaticUpdate(self):
69 commands.append(("updateAll",))
70 # for cat in listing.getListOfCategories():
71 # for feed in listing.getSortedListOfKeys("Manual", category=cat):
73 # download = Download(listing, feeds)
76 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
78 for cat in listing.getListOfCategories():
79 for feed in listing.getSortedListOfKeys("Manual", category=cat):
80 listing.updateFeed(feed)
82 def openTaskSwitch(self):
84 subprocess.Popen("dbus-send /com/nokia/hildon_desktop com.nokia.hildon_desktop.exit_app_view", shell=True)
86 def getCommands(self):
87 commandXml = "<commands>"
89 if item[0]=="addFeed":
90 commandXml += "<command c='addFeed'>%s</command>" %(sanitize(item[1]))
91 if item[0]=="openFeed":
93 cat = str(listing.getFeedCategory(key))
94 commandXml += "<command c='openFeed' cat='%s'>%s</command>" % (sanitize(cat), sanitize(key) )
95 if item[0]=="openArticle":
97 cat = str(listing.getFeedCategory(key))
99 commandXml += "<command c='openArticle' cat='%s' key='%s'>%s</command>" %(sanitize(cat), sanitize(key), sanitize(articleid) )
100 if item[0]=="updateAll":
102 commands.remove(item)
103 commandXml += "</commands>"
106 def getConfigXml(self):
107 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
108 xml += "<hideReadFeed>True</hideReadFeed>"
109 xml += "<hideReadArticles>True</hideReadArticles>"
113 def generateCategoryXml(self):
114 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
115 for cat in listing.getListOfCategories():
117 xml += "<catname>%s</catname>" %sanitize(listing.getCategoryTitle(cat))
118 xml += "<catid>%s</catid>" % cat
123 def fix_title(self, title):
124 return escape(unescape(title).replace("<em>","").replace("</em>","").replace("<nobr>","").replace("</nobr>","").replace("<wbr>","").replace("—","-"))
126 def generateFeedsXml(self, catid):
127 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
128 for key in listing.getSortedListOfKeys("Manual", category=catid):
130 xml += "<feedname>%s</feedname>" %sanitize(listing.getFeedTitle(key))
131 xml += "<feedid>%s</feedid>" %key
132 xml += "<unread>%s</unread>" %listing.getFeedNumberOfUnreadItems(key)
133 xml += "<updatedDate>%s</updatedDate>" %listing.getFeedUpdateTime(key)
134 xml += "<icon>%s</icon>" %listing.getFavicon(key)
135 if key in updatingFeeds:
136 xml += "<updating>True</updating>"
138 xml += "<updating>False</updating>"
143 def generateArticlesXml(self, key, onlyUnread, markAllAsRead):
144 feed = listing.getFeed(key)
145 if markAllAsRead=="True":
147 listing.updateUnread(key)
148 updateDbusHandler.ArticleCountUpdated()
149 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
150 if onlyUnread == "False":
152 for id in feed.getIds(onlyUnread):
154 xml += "<title>%s</title>" %self.fix_title(feed.getTitle(id))
155 xml += "<articleid>%s</articleid>" %id
156 xml += "<unread>%s</unread>" %str(feed.isEntryRead(id))
157 xml += "<updatedDate>%s</updatedDate>" %feed.getDateStamp(id)
158 xml += "<path>%s</path>" %feed.getContentLink(id)
164 (req, sep, arg) = self.path.partition("?")
165 request = req.split("/")
168 args = arg.split("&")
171 arguments[ele[0]] = ele[1]
172 if request[1] == "categories":
173 xml = self.generateCategoryXml()
174 elif request[1] == "feeds":
176 xml = self.generateFeedsXml(catid)
177 elif request[1] == "articles":
179 onlyUnread = arguments.get("onlyUnread","False")
180 markAllAsRead = arguments.get("markAllAsRead", "False")
181 xml = self.generateArticlesXml(key, onlyUnread, markAllAsRead)
182 elif request[1] == "html":
185 feed = listing.getFeed(key)
187 file = open(feed.getContentLink(article))
188 html = file.read().replace("body", "body bgcolor='#ffffff'", 1)
191 html = "<html><body>Error retrieving article</body></html>"
192 self.send_response(200)
193 self.send_header("Content-type", "text/html")
195 self.wfile.write(html)
196 #listing.updateUnread(key)
198 elif request[1] == "isUpdating":
201 if (key in updatingFeeds) or ((key=="") and (len(updatingFeeds)>0)):
202 xml += "<updating>True</updating>"
204 xml += "<updating>False</updating>"
205 xml += self.getCommands()
207 elif request[1] == "read":
210 feed = listing.getFeed(key)
211 feed.setEntryRead(article)
212 listing.updateUnread(key)
213 updateDbusHandler.ArticleCountUpdated()
214 self.send_response(200)
215 self.send_header("Content-type", "text/html")
217 self.wfile.write("OK")
219 elif request[1] == "config":
220 xml = self.getConfigXml()
221 elif request[1] == "home":
222 file = open(self.path)
223 self.send_response(200)
224 self.send_header("Content-type", "text/html")
226 self.wfile.write(file.read())
229 elif request[1] == "task":
230 self.openTaskSwitch()
231 xml = "<xml>OK</xml>"
232 elif request[1] == "deleteCat":
234 listing.removeCategory(key)
235 xml = "<xml>OK</xml>"
236 elif request[1] == "deleteFeed":
238 listing.removeFeed(key)
239 xml = "<xml>OK</xml>"
240 elif request[1] == "addFeed":
243 url = arguments.get("url","")
244 listing.addFeed(name, url, category=cat)
245 xml = "<xml>OK</xml>"
246 elif request[1] == "updateFeed":
248 download = Download(listing, [key,])
250 xml = "<xml>OK</xml>"
251 elif request[1]=="updateAll":
252 #app.automaticUpdate()
254 updateDbusHandler.ArticleCountUpdated()
255 xml = "<xml>OK</xml>"
256 elif request[1] == "addCat":
258 listing.addCategory(catName)
259 xml = "<xml>OK</xml>"
261 self.send_error(404, "File not found")
263 self.send_response(200)
264 self.send_header("Content-type", "text/xml")
266 self.wfile.write(xml.encode("utf-8"))
270 if not isdir(CONFIGDIR):
274 print "Error: Can't create configuration directory"
278 from config import Config
279 config = Config(None,CONFIGDIR+"config.ini")
285 # Initialize the glib mainloop, for dbus purposes
286 from feedingitdbus import ServerObject
287 from updatedbus import UpdateServerObject, get_lock
290 gobject.threads_init()
291 import dbus.mainloop.glib
292 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
295 from jobmanager import JobManager
298 global updateDbusHandler, dbusHandler
300 dbusHandler = ServerObject(app)
301 updateDbusHandler = UpdateServerObject(app)
303 # Start the HTTP server in a new thread
304 thread.start_new_thread(start_server, ())
306 mainloop = gobject.MainLoop()