Map space bar to scroll_unread_items (thanks estar).
[straw.git] / straw / FeedUpdater.py
blob6176ab2e4b501823d364a151151cf1a51cb2f0b5
1 """ FeedUpdater.py
3 """
5 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__ = """
7 Straw is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
12 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA. """
20 from JobManager import Job, TaskResult, TaskThread, JobHandler
21 import Config
22 import Fetcher
23 import JobManager
24 import SummaryParser
25 import gobject
26 import httplib2
27 import os
28 import urlparse
30 class FeedUpdateJobHandler(JobHandler):
31 job_id = "feed-update"
33 __gsignals__ = {
34 "update-started" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
35 "update-done" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
38 def __init__(self, id, job):
39 JobHandler.__init__(self, id, job)
41 def _on_fetch_started(self, handler, task):
42 self._notify("update-started", task.user_data)
44 def _on_url_fetched(self, handler, fetch_result):
45 feed = fetch_result.task.user_data
46 update_result = None
48 if fetch_result.result.error:
49 update_result = FeedUpdateResult("fetch-error", feed, fetch_result = fetch_result.result)
50 else:
51 try:
52 feed = SummaryParser.parse(fetch_result.result.content, feed)
53 update_result = FeedUpdateResult(None, feed, fetch_result = fetch_result.result)
54 except Exception, e:
55 update_result = FeedUpdateResult("parse-error", feed,
56 fetch_result = fetch_result.result, parse_error = e)
58 self._notify("update-done", update_result)
60 def _on_fetch_done(self, handler, data):
61 pass
63 def _prepare(self):
64 self.fetch_tasks = []
66 for feed in self.job.tasks:
67 credentials = None
69 if feed.pdict.has_key("username"):
70 credentials = feed.pdict["username"], feed.pdict["password"], ""
72 task = Fetcher.create_task(url = feed.location, credentials = credentials, user_data = feed)
74 self.fetch_tasks.append(task)
76 self.observers = [{ "task-done": [ self._on_url_fetched ],
77 "task-start": [ self._on_fetch_started ],
78 "job-done": [ self._on_fetch_done ]}]
80 def _run(self):
81 self.fetcher_id = Fetcher.fetch(self.fetch_tasks, observers = self.observers, wait = True)
83 class FeedUpdateResult(object):
84 def __init__(self, error, feed, fetch_result = None, parse_error = None):
85 self.error = error
86 self.feed = feed
87 self.fetch_result = fetch_result
89 JobManager.register_handler(FeedUpdateJobHandler)
91 def update(feeds, observers):
92 update = Job("feed-update")
93 update.tasks = feeds
94 update.observers = observers
95 return JobManager.start(update)
97 def stop(id):
98 JobManager.stop(id)