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(config, 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 commands.append( ("openFeed", key) )
61 def openArticle(self, key, id):
62 commands.append( ("openArticle", key, id) )
64 def automaticUpdate(self):
65 commands.append(("updateAll",))
66 # for cat in listing.getListOfCategories():
67 # for feed in listing.getSortedListOfKeys("Manual", category=cat):
69 # download = Download(listing, feeds)
72 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
74 for cat in listing.getListOfCategories():
75 for feed in listing.getSortedListOfKeys("Manual", category=cat):
76 listing.updateFeed(feed)
78 def openTaskSwitch(self):
80 subprocess.Popen("dbus-send /com/nokia/hildon_desktop com.nokia.hildon_desktop.exit_app_view", shell=True)
82 def getCommands(self):
83 commandXml = "<commands>"
85 if item[0]=="addFeed":
86 commandXml += "<command c='addFeed'>%s</command>" %(sanitize(item[1]))
87 if item[0]=="openFeed":
89 cat = str(listing.getFeedCategory(key))
90 commandXml += "<command c='openFeed' cat='%s'>%s</command>" % (sanitize(cat), sanitize(key) )
91 if item[0]=="openArticle":
93 cat = str(listing.getFeedCategory(key))
95 commandXml += "<command c='openArticle' cat='%s' key='%s'>%s</command>" %(sanitize(cat), sanitize(key), sanitize(articleid) )
96 if item[0]=="updateAll":
99 commandXml += "</commands>"
102 def getConfigXml(self):
103 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
104 xml += "<hideReadFeed>True</hideReadFeed>"
105 xml += "<hideReadArticles>True</hideReadArticles>"
109 def generateCategoryXml(self):
110 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
111 for cat in listing.getListOfCategories():
113 xml += "<catname>%s</catname>" %sanitize(listing.getCategoryTitle(cat))
114 xml += "<catid>%s</catid>" % cat
119 def fix_title(self, title):
120 return escape(unescape(title).replace("<em>","").replace("</em>","").replace("<nobr>","").replace("</nobr>","").replace("<wbr>","").replace("—","-"))
122 def generateFeedsXml(self, catid):
123 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
124 for key in listing.getSortedListOfKeys("Manual", category=catid):
126 xml += "<feedname>%s</feedname>" %sanitize(listing.getFeedTitle(key))
127 xml += "<feedid>%s</feedid>" %key
128 xml += "<unread>%s</unread>" %listing.getFeedNumberOfUnreadItems(key)
129 xml += "<updatedDate>%s</updatedDate>" %listing.getFeedUpdateTime(key)
130 xml += "<icon>%s</icon>" %listing.getFavicon(key)
131 if key in updatingFeeds:
132 xml += "<updating>True</updating>"
134 xml += "<updating>False</updating>"
139 def generateArticlesXml(self, key, onlyUnread, markAllAsRead):
140 feed = listing.getFeed(key)
141 if markAllAsRead=="True":
143 listing.updateUnread(key)
144 updateDbusHandler.ArticleCountUpdated()
145 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml>"
146 if onlyUnread == "False":
148 for id in feed.getIds(onlyUnread):
150 xml += "<title>%s</title>" %self.fix_title(feed.getTitle(id))
151 xml += "<articleid>%s</articleid>" %id
152 xml += "<unread>%s</unread>" %str(feed.isEntryRead(id))
153 xml += "<updatedDate>%s</updatedDate>" %feed.getDateStamp(id)
154 xml += "<path>%s</path>" %feed.getContentLink(id)
160 (req, sep, arg) = self.path.partition("?")
161 request = req.split("/")
164 args = arg.split("&")
167 arguments[ele[0]] = ele[1]
168 if request[1] == "categories":
169 xml = self.generateCategoryXml()
170 elif request[1] == "feeds":
172 xml = self.generateFeedsXml(catid)
173 elif request[1] == "articles":
175 onlyUnread = arguments.get("onlyUnread","False")
176 markAllAsRead = arguments.get("markAllAsRead", "False")
177 xml = self.generateArticlesXml(key, onlyUnread, markAllAsRead)
178 elif request[1] == "html":
181 feed = listing.getFeed(key)
183 file = open(feed.getContentLink(article))
184 html = file.read().replace("body", "body bgcolor='#ffffff'", 1)
187 html = "<html><body>Error retrieving article</body></html>"
188 self.send_response(200)
189 self.send_header("Content-type", "text/html")
191 self.wfile.write(html)
192 #listing.updateUnread(key)
194 elif request[1] == "isUpdating":
197 if (key in updatingFeeds) or ((key=="") and (len(updatingFeeds)>0)):
198 xml += "<updating>True</updating>"
200 xml += "<updating>False</updating>"
201 xml += self.getCommands()
203 elif request[1] == "read":
206 feed = listing.getFeed(key)
207 feed.setEntryRead(article)
208 listing.updateUnread(key)
209 updateDbusHandler.ArticleCountUpdated()
210 self.send_response(200)
211 self.send_header("Content-type", "text/html")
213 self.wfile.write("OK")
215 elif request[1] == "config":
216 xml = self.getConfigXml()
217 elif request[1] == "home":
218 file = open(self.path)
219 self.send_response(200)
220 self.send_header("Content-type", "text/html")
222 self.wfile.write(file.read())
225 elif request[1] == "task":
226 self.openTaskSwitch()
227 xml = "<xml>OK</xml>"
228 elif request[1] == "deleteCat":
230 listing.removeCategory(key)
231 xml = "<xml>OK</xml>"
232 elif request[1] == "deleteFeed":
234 listing.removeFeed(key)
235 xml = "<xml>OK</xml>"
236 elif request[1] == "addFeed":
239 url = arguments.get("url","")
240 listing.addFeed(name, url, category=cat)
241 xml = "<xml>OK</xml>"
242 elif request[1] == "updateFeed":
244 download = Download(listing, [key,])
246 xml = "<xml>OK</xml>"
247 elif request[1]=="updateAll":
248 #app.automaticUpdate()
250 updateDbusHandler.ArticleCountUpdated()
251 xml = "<xml>OK</xml>"
252 elif request[1] == "addCat":
254 listing.addCategory(catName)
255 xml = "<xml>OK</xml>"
257 self.send_error(404, "File not found")
259 self.send_response(200)
260 self.send_header("Content-type", "text/xml")
262 self.wfile.write(xml.encode("utf-8"))
266 if not isdir(CONFIGDIR):
270 print "Error: Can't create configuration directory"
274 from config import Config
275 config = Config(None,CONFIGDIR+"config.ini")
281 # Initialize the glib mainloop, for dbus purposes
282 from feedingitdbus import ServerObject
283 from updatedbus import UpdateServerObject, get_lock
286 gobject.threads_init()
287 import dbus.mainloop.glib
288 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
291 from jobmanager import JobManager
294 global updateDbusHandler, dbusHandler
296 dbusHandler = ServerObject(app)
297 updateDbusHandler = UpdateServerObject(app)
299 # Start the HTTP server in a new thread
300 thread.start_new_thread(start_server, ())
302 mainloop = gobject.MainLoop()