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
14 CONFIGDIR = environ.get("HOME", "/home/user") + "/.feedingit"
15 #CONFIGDIR = "/home/user/.feedingit/"
18 #commands = [("addFeed","httpwww"), ("openFeed", "xxxx"), ("openArticle", ("feedid","artid"))]
28 return unichr(int(text[3:-1], 16))
30 return unichr(int(text[2:-1]))
36 text = unichr(name2codepoint[text[1:-1]])
39 return text # leave as is
40 return sub("&#?\w+;", fixup, text)
43 from cgi import escape
44 return escape(text).encode('ascii', 'xmlcharrefreplace')
48 listing = Listing(config, CONFIGDIR)
49 httpd = BaseHTTPServer.HTTPServer(("127.0.0.1", PORT), Handler)
53 def addFeed(self, url):
54 commands.append(("addFeed",url))
59 def openFeed(self, key):
60 commands.append( ("openFeed", key) )
62 def openArticle(self, key, id):
63 commands.append( ("openArticle", key, id) )
65 def automaticUpdate(self):
66 commands.append(("updateAll",))
67 # for cat in listing.getListOfCategories():
68 # for feed in listing.getSortedListOfKeys("Manual", category=cat):
70 # download = Download(listing, feeds)
73 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
75 for cat in listing.getListOfCategories():
76 for feed in listing.getSortedListOfKeys("Manual", category=cat):
77 listing.updateFeed(feed)
79 def openTaskSwitch(self):
81 subprocess.Popen("dbus-send /com/nokia/hildon_desktop com.nokia.hildon_desktop.exit_app_view", shell=True)
83 def getCommands(self):
84 commandXml = "<commands>"
86 if item[0]=="addFeed":
87 commandXml += "<command c='addFeed'>%s</command>" %(sanitize(item[1]))
88 if item[0]=="openFeed":
90 cat = str(listing.getFeedCategory(key))
91 commandXml += "<command c='openFeed' cat='%s'>%s</command>" % (sanitize(cat), sanitize(key) )
92 if item[0]=="openArticle":
94 cat = str(listing.getFeedCategory(key))
96 commandXml += "<command c='openArticle' cat='%s' key='%s'>%s</command>" %(sanitize(cat), sanitize(key), sanitize(articleid) )
97 if item[0]=="updateAll":
100 commandXml += "</commands>"
103 def getConfigXml(self):
104 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
105 xml += "<hideReadFeed>True</hideReadFeed>"
106 xml += "<hideReadArticles>True</hideReadArticles>"
110 def generateCategoryXml(self):
111 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
112 for cat in listing.getListOfCategories():
114 xml += "<catname>%s</catname>" %sanitize(listing.getCategoryTitle(cat))
115 xml += "<catid>%s</catid>" % cat
120 def fix_title(self, title):
121 return escape(unescape(title).replace("<em>","").replace("</em>","").replace("<nobr>","").replace("</nobr>","").replace("<wbr>","").replace("—","-"))
123 def generateFeedsXml(self, catid):
124 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
125 for key in listing.getSortedListOfKeys("Manual", category=catid):
127 xml += "<feedname>%s</feedname>" %sanitize(listing.getFeedTitle(key))
128 xml += "<feedid>%s</feedid>" %key
129 xml += "<unread>%s</unread>" %listing.getFeedNumberOfUnreadItems(key)
130 xml += "<updatedDate>%s</updatedDate>" %listing.getFeedUpdateTime(key)
131 xml += "<icon>%s</icon>" %listing.getFavicon(key)
132 if key in updatingFeeds:
133 xml += "<updating>True</updating>"
135 xml += "<updating>False</updating>"
140 def generateArticlesXml(self, key, onlyUnread, markAllAsRead):
141 feed = listing.getFeed(key)
142 if markAllAsRead=="True":
144 listing.updateUnread(key)
145 updateDbusHandler.ArticleCountUpdated()
146 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
147 if onlyUnread == "False":
149 for id in feed.getIds(onlyUnread):
151 xml += "<title>%s</title>" %self.fix_title(feed.getTitle(id))
152 xml += "<articleid>%s</articleid>" %id
153 xml += "<unread>%s</unread>" %str(feed.isEntryRead(id))
154 xml += "<updatedDate>%s</updatedDate>" %feed.getDateStamp(id)
155 xml += "<path>%s</path>" %feed.getContentLink(id)
161 (req, sep, arg) = self.path.partition("?")
162 request = req.split("/")
165 args = arg.split("&")
168 arguments[ele[0]] = ele[1]
169 if request[1] == "categories":
170 xml = self.generateCategoryXml()
171 elif request[1] == "feeds":
173 xml = self.generateFeedsXml(catid)
174 elif request[1] == "articles":
176 onlyUnread = arguments.get("onlyUnread","False")
177 markAllAsRead = arguments.get("markAllAsRead", "False")
178 xml = self.generateArticlesXml(key, onlyUnread, markAllAsRead)
179 elif request[1] == "html":
182 feed = listing.getFeed(key)
184 file = open(feed.getContentLink(article))
185 html = file.read().replace("body", "body bgcolor='#ffffff'", 1)
188 html = "<html><body>Error retrieving article</body></html>"
189 self.send_response(200)
190 self.send_header("Content-type", "text/html")
192 self.wfile.write(html)
193 #listing.updateUnread(key)
195 elif request[1] == "isUpdating":
198 if (key in updatingFeeds) or ((key=="") and (len(updatingFeeds)>0)):
199 xml += "<updating>True</updating>"
201 xml += "<updating>False</updating>"
202 xml += self.getCommands()
204 elif request[1] == "read":
207 feed = listing.getFeed(key)
208 feed.setEntryRead(article)
209 listing.updateUnread(key)
210 updateDbusHandler.ArticleCountUpdated()
211 self.send_response(200)
212 self.send_header("Content-type", "text/html")
214 self.wfile.write("OK")
216 elif request[1] == "config":
217 xml = self.getConfigXml()
218 elif request[1] == "home":
219 file = open(self.path)
220 self.send_response(200)
221 self.send_header("Content-type", "text/html")
223 self.wfile.write(file.read())
226 elif request[1] == "task":
227 self.openTaskSwitch()
228 xml = "<xml>OK</xml>"
229 elif request[1] == "deleteCat":
231 listing.removeCategory(key)
232 xml = "<xml>OK</xml>"
233 elif request[1] == "deleteFeed":
235 listing.removeFeed(key)
236 xml = "<xml>OK</xml>"
237 elif request[1] == "addFeed":
240 url = arguments.get("url","")
241 listing.addFeed(name, url, category=cat)
242 xml = "<xml>OK</xml>"
243 elif request[1] == "updateFeed":
245 download = Download(listing, [key,])
247 xml = "<xml>OK</xml>"
248 elif request[1]=="updateAll":
249 #app.automaticUpdate()
251 updateDbusHandler.ArticleCountUpdated()
252 xml = "<xml>OK</xml>"
253 elif request[1] == "addCat":
255 listing.addCategory(catName)
256 xml = "<xml>OK</xml>"
258 self.send_error(404, "File not found")
260 self.send_response(200)
261 self.send_header("Content-type", "text/xml")
263 self.wfile.write(xml.encode("utf-8"))
267 if not isdir(CONFIGDIR):
271 print "Error: Can't create configuration directory"
275 from config import Config
276 config = Config(None,CONFIGDIR+"config.ini")
282 # Initialize the glib mainloop, for dbus purposes
283 from feedingitdbus import ServerObject
284 from updatedbus import UpdateServerObject, get_lock
287 gobject.threads_init()
288 import dbus.mainloop.glib
289 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
292 from jobmanager import JobManager
295 global updateDbusHandler, dbusHandler
297 dbusHandler = ServerObject(app)
298 updateDbusHandler = UpdateServerObject(app)
300 # Start the HTTP server in a new thread
301 thread.start_new_thread(start_server, ())
303 mainloop = gobject.MainLoop()