Implemented Fetcher job, refactored JobManager, code cleanup.
[straw.git] / straw / FeedUpdater.py
blob9ea4efbbcfd089939095fdd878b3845448c2880e
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 Fetcher import FetchTask
21 from JobManager import Job, TaskResult, TaskThread, JobHandler
22 import Config
23 import Fetcher
24 import JobManager
25 import SummaryParser
26 import gobject
27 import httplib2
28 import os
29 import urlparse
31 class FeedUpdateJobHandler(JobHandler):
32 job_id = "feed-update"
34 __gsignals__ = {
35 "update-started" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
36 "update-done" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
39 def __init__(self, id, job):
40 JobHandler.__init__(self, id, job)
42 def _on_fetch_started(self, handler, task):
43 self._notify("update-started", task.user_data)
45 def _on_url_fetched(self, handler, fetch_result):
46 feed = fetch_result.task.user_data
47 update_result = None
49 if fetch_result.result.error:
50 update_result = FeedUpdateResult("fetch-error", feed, fetch_result.result)
51 else:
52 update_result = FeedUpdateResult(None, feed, fetch_result.result)
53 feed = SummaryParser.parse(fetch_result.result.content, feed)
55 self._notify("update-done", update_result)
57 def _on_fetch_done(self, handler, data):
58 pass
60 def _prepare(self):
61 self.fetch_tasks = [Fetcher.create_task(url = feed.location, user_data = feed) for feed in self.job.tasks]
63 self.observers = [{ "task-done": [ self._on_url_fetched ],
64 "task-start": [ self._on_fetch_started ],
65 "job-done": [ self._on_fetch_done ]}]
67 def _run(self):
68 print "starting fetch..."
69 self.fetcher_id = Fetcher.fetch(self.fetch_tasks, observers = self.observers, wait = True)
70 print "fetch finished!"
72 class FeedUpdateResult(object):
73 def __init__(self, error, feed, fetch_result):
74 self.error = error
75 self.feed = feed
76 self.fetch_result = fetch_result
78 JobManager.register_handler(FeedUpdateJobHandler)
80 def update(feeds, observers):
81 update = Job("feed-update")
82 update.tasks = feeds
83 update.observers = observers
84 return JobManager.start(update)
86 def stop(id):
87 JobManager.stop(id)