fix formatting
[feedingit] / src / update_feeds.py
1 #!/usr/bin/env python2.5
2
3
4 # Copyright (c) 2007-2008 INdT.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # ============================================================================
20 # Name        : update_feeds.py
21 # Author      : Yves Marcoz
22 # Version     : 0.6.1
23 # Description : Simple RSS Reader
24 # ============================================================================
25
26 from rss import Listing
27 from config import Config
28
29 import threading
30 import os
31 import gobject
32
33 CONFIGDIR="/home/user/.feedingit/"
34 #DESKTOP_FILE = "/usr/share/applications/hildon-status-menu/feedingit_status.desktop"
35 dbug = False
36
37 from socket import setdefaulttimeout
38 timeout = 5
39 setdefaulttimeout(timeout)
40 del timeout
41
42 from updatedbus import UpdateServerObject, get_lock
43
44 class Download(threading.Thread):
45     def __init__(self, listing, config, dbusHandler):
46         global dbug
47         threading.Thread.__init__(self)
48         self.running = True
49         self.listing = listing
50         self.config = config
51         self.dbusHandler = dbusHandler
52         if dbug:
53             self.dbug = open(CONFIGDIR+"dbug.log", "w")
54         
55     def run(self):
56         global dbug
57         if dbug:
58             self.dbug.write("Starting updates\n")
59         try:
60             self.dbusHandler.UpdateStarted()
61             (use_proxy, proxy) = self.config.getProxy()
62             for key in self.listing.getListOfFeeds():
63                 if dbug:
64                     self.dbug.write("updating %s\n" %key)
65                 try:
66                     if use_proxy:
67                         from urllib2 import install_opener, build_opener
68                         install_opener(build_opener(proxy))
69                         self.listing.updateFeed(key, self.config.getExpiry(), proxy=proxy, imageCache=self.config.getImageCache() )
70                     else:
71                         self.listing.updateFeed(key, self.config.getExpiry(), imageCache=self.config.getImageCache() )
72                 except:
73                     import traceback
74                     file = open("/home/user/.feedingit/feedingit_update.log", "a")
75                     traceback.print_exc(file=file)
76                     file.close()
77                 if not self.running:
78                     if dbug:
79                         self.dbug.write("received stopUpdate after %s\n" %key)
80                     break
81             self.dbusHandler.UpdateFinished()
82             self.dbusHandler.ArticleCountUpdated()
83             if dbug:
84                 self.dbug.write("Dbus ArticleCountUpdated signal sent\n")
85         except:
86             pass
87         self.listing.saveConfig()
88         if dbug:
89             self.dbug.write("About to main_quit\n")
90         mainloop.quit()
91         if dbug:
92             self.dbug.write("After main_quit\n")
93             self.dbug.close()
94
95 class FeedUpdate():
96     def __init__(self):
97         self.listing = Listing(CONFIGDIR)
98         self.config = Config(self, CONFIGDIR+"config.ini")
99         self.dbusHandler = UpdateServerObject(self)
100         self.updateThread = False
101         
102     def automaticUpdate(self):
103         #self.listing.updateFeeds()
104         if self.updateThread == False:
105             self.updateThread = Download(self.listing, self.config, self.dbusHandler)
106             self.updateThread.start()
107         
108     def stopUpdate(self):
109         try:
110             self.updateThread.running = False
111         except:
112             pass
113
114 import dbus.mainloop.glib
115 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
116
117 gobject.threads_init()
118 mainloop = gobject.MainLoop()
119
120 app_lock = get_lock("app_lock")
121
122 if app_lock != None:
123     try:
124         feed = FeedUpdate()
125         mainloop.run()
126         del app_lock
127     except:
128         import traceback
129         file = open("/home/user/.feedingit/feedingit_update.log", "w")
130         traceback.print_exc(file=file)
131         file.close()
132 else:
133     file = open("/home/user/.feedingit/feedingit_update.log", "a")
134     file.write("Update in progress")
135     file.close()
136