Refactored FeedDiscovery, now it uses Fetcher; added support for HTTP Basic Auth...
[straw.git] / straw / FeedUpdater.py
blobdd396b5ba4c70a64e1a2ffe820ae2f2b9ddae763
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.result)
50 else:
51 update_result = FeedUpdateResult(None, feed, fetch_result.result)
52 feed = SummaryParser.parse(fetch_result.result.content, feed)
54 self._notify("update-done", update_result)
56 def _on_fetch_done(self, handler, data):
57 pass
59 def _prepare(self):
60 self.fetch_tasks = []
62 for feed in self.job.tasks:
63 credentials = None
65 if feed.pdict.has_key("username"):
66 credentials = feed.pdict["username"], feed.pdict["password"], ""
68 task = Fetcher.create_task(url = feed.location, credentials = credentials, user_data = feed)
70 self.fetch_tasks.append(task)
72 self.observers = [{ "task-done": [ self._on_url_fetched ],
73 "task-start": [ self._on_fetch_started ],
74 "job-done": [ self._on_fetch_done ]}]
76 def _run(self):
77 print "starting fetch..."
78 self.fetcher_id = Fetcher.fetch(self.fetch_tasks, observers = self.observers, wait = True)
79 print "fetch finished!"
81 class FeedUpdateResult(object):
82 def __init__(self, error, feed, fetch_result):
83 self.error = error
84 self.feed = feed
85 self.fetch_result = fetch_result
87 JobManager.register_handler(FeedUpdateJobHandler)
89 def update(feeds, observers):
90 update = Job("feed-update")
91 update.tasks = feeds
92 update.observers = observers
93 return JobManager.start(update)
95 def stop(id):
96 JobManager.stop(id)