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
14 CONFIGDIR = "/home/user/.feedingit/"
17 #commands = [("addFeed","httpwww"), ("openFeed", "xxxx"), ("openArticle", ("feedid","artid"))]
27 return unichr(int(text[3:-1], 16))
29 return unichr(int(text[2:-1]))
35 text = unichr(name2codepoint[text[1:-1]])
38 return text # leave as is
39 return sub("&#?\w+;", fixup, text)
42 from cgi import escape
43 return escape(text).encode('ascii', 'xmlcharrefreplace')
47 listing = Listing(CONFIGDIR)
48 httpd = BaseHTTPServer.HTTPServer(("127.0.0.1", PORT), Handler)
52 def addFeed(self, url):
53 commands.append(("addFeed",url))
58 def openFeed(self, key):
59 cat = listing.getFeedCategory(key)
60 commands.append( ("openFeed", cat, key) )
62 def OpenArticle(self, key, id):
63 cat = listing.getFeedCategory(key)
64 commands.append( ("openArticle", (cat, key, id)) )
66 class Download(Thread):
67 def __init__(self, listing, keys):
69 self.listing = listing
73 updateDbusHandler.UpdateStarted()
75 print "Start update: %s" % key
76 updatingFeeds.append(key)
77 (use_proxy, proxy) = config.getProxy()
80 self.listing.updateFeed(key, proxy=proxy, imageCache=config.getImageCache() )
82 self.listing.updateFeed(key, imageCache=config.getImageCache() )
84 print "Error updating feed: %s" %key
85 updatingFeeds.remove(key)
86 print "End update: %s" % key
87 updateDbusHandler.UpdateFinished()
89 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
90 def openTaskSwitch(self):
92 subprocess.Popen("dbus-send /com/nokia/hildon_desktop com.nokia.hildon_desktop.exit_app_view", shell=True)
94 def automaticUpdate(self):
96 for cat in listing.getListOfCategories():
97 for feed in listing.getSortedListOfKeys("Manual", category=cat):
100 download = Download(listing, feeds)
103 def getCommands(self):
105 commandXml = "<commands>"
106 for item in commands:
107 if item[0]=="addFeed":
108 commandXml += "<command c='addFeed'>%s</command>" %(sanitize(item[1]))
109 if item[0]=="openFeed":
110 commandXml += "<command c='openFeed' cat='%s'>%s</command>" % (sanitize(item[1]), sanitize(item[2]) )
111 if item[0]=="openArticle":
112 commandXml += "<command c='openArticle' cat='%s' key='%s'>%s</command>" %(sanitize(item[1], sanitize(item[2][0]), sanitize(item[2][1])) )
113 commands.remove(item)
114 commandXml += "</commands>"
117 def getConfigXml(self):
118 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
119 xml += "<hideReadFeed>True</hideReadFeed>"
120 xml += "<hideReadArticles>True</hideReadArticles>"
124 def generateCategoryXml(self):
125 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
126 for cat in listing.getListOfCategories():
128 xml += "<catname>%s</catname>" %listing.getCategoryTitle(cat)
129 xml += "<catid>%s</catid>" % cat
134 def fix_title(self, title):
135 return escape(unescape(title).replace("<em>","").replace("</em>","").replace("<nobr>","").replace("</nobr>","").replace("<wbr>","").replace("—","-"))
137 def generateFeedsXml(self, catid):
138 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
139 for key in listing.getSortedListOfKeys("Manual", category=catid):
141 xml += "<feedname>%s</feedname>" %listing.getFeedTitle(key)
142 xml += "<feedid>%s</feedid>" %key
143 xml += "<unread>%s</unread>" %listing.getFeedNumberOfUnreadItems(key)
144 xml += "<updatedDate>%s</updatedDate>" %listing.getFeedUpdateTime(key)
145 xml += "<icon>%s</icon>" %listing.getFavicon(key)
146 if key in updatingFeeds:
147 xml += "<updating>True</updating>"
149 xml += "<updating>False</updating>"
154 def generateArticlesXml(self, key, onlyUnread, markAllAsRead):
155 feed = listing.getFeed(key)
156 if markAllAsRead=="True":
158 listing.updateUnread(key)
159 updateDbusHandler.ArticleCountUpdated()
160 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
161 if onlyUnread == "False":
163 for id in feed.getIds(onlyUnread):
165 xml += "<title>%s</title>" %self.fix_title(feed.getTitle(id))
166 xml += "<articleid>%s</articleid>" %id
167 xml += "<unread>%s</unread>" %str(feed.isEntryRead(id))
168 xml += "<updatedDate>%s</updatedDate>" %feed.getDateStamp(id)
169 xml += "<path>%s</path>" %feed.getContentLink(id)
175 (req, sep, arg) = self.path.partition("?")
176 request = req.split("/")
179 args = arg.split("&")
182 arguments[ele[0]] = ele[1]
183 if request[1] == "categories":
184 xml = self.generateCategoryXml()
185 elif request[1] == "feeds":
187 xml = self.generateFeedsXml(catid)
188 elif request[1] == "articles":
190 onlyUnread = arguments.get("onlyUnread","False")
191 markAllAsRead = arguments.get("markAllAsRead", "False")
192 xml = self.generateArticlesXml(key, onlyUnread, markAllAsRead)
193 elif request[1] == "html":
196 feed = listing.getFeed(key)
198 file = open(feed.getContentLink(article))
199 html = file.read().replace("body", "body bgcolor='#ffffff'", 1)
202 html = "<html><body>Error retrieving article</body></html>"
203 self.send_response(200)
204 self.send_header("Content-type", "text/html")
206 self.wfile.write(html)
207 #listing.updateUnread(key)
209 elif request[1] == "isUpdating":
212 if (key in updatingFeeds) or ((key=="") and (len(updatingFeeds)>0)):
213 xml += "<updating>True</updating>"
215 xml += "<updating>False</updating>"
216 xml += self.getCommands()
218 elif request[1] == "read":
221 feed = listing.getFeed(key)
222 feed.setEntryRead(article)
223 listing.updateUnread(key)
224 updateDbusHandler.ArticleCountUpdated()
225 self.send_response(200)
226 self.send_header("Content-type", "text/html")
228 self.wfile.write("OK")
230 elif request[1] == "config":
231 xml = self.getConfigXml()
232 elif request[1] == "home":
233 file = open(self.path)
234 self.send_response(200)
235 self.send_header("Content-type", "text/html")
237 self.wfile.write(file.read())
240 elif request[1] == "task":
241 self.openTaskSwitch()
242 xml = "<xml>OK</xml>"
243 elif request[1] == "deleteCat":
245 listing.removeCategory(key)
246 xml = "<xml>OK</xml>"
247 elif request[1] == "deleteFeed":
249 listing.removeFeed(key)
250 xml = "<xml>OK</xml>"
251 elif request[1] == "addFeed":
254 url = arguments.get("url","")
255 listing.addFeed(name, url, category=cat)
256 xml = "<xml>OK</xml>"
257 elif request[1] == "updateFeed":
259 download = Download(listing, [key,])
261 xml = "<xml>OK</xml>"
262 elif request[1]=="updateAll":
263 self.automaticUpdate()
264 updateDbusHandler.ArticleCountUpdated()
265 xml = "<xml>OK</xml>"
266 elif request[1] == "addCat":
268 listing.addCategory(catName)
269 xml = "<xml>OK</xml>"
271 self.send_error(404, "File not found")
273 self.send_response(200)
274 self.send_header("Content-type", "text/xml")
276 self.wfile.write(xml.encode("utf-8"))
280 if not isdir(CONFIGDIR):
284 print "Error: Can't create configuration directory"
288 from config import Config
289 config = Config(None,CONFIGDIR+"config.ini")
293 #print "serving at port", PORT
294 thread.start_new_thread(start_server, ())
296 from feedingitdbus import ServerObject
297 from updatedbus import UpdateServerObject, get_lock
299 gobject.threads_init()
300 import dbus.mainloop.glib
301 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
303 dbusHandler = ServerObject(app)
304 updateDbusHandler = UpdateServerObject(app)
305 mainloop = gobject.MainLoop()