Added 144 and 288 hour option for article retention
[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_sqlite 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, config, dbusHandler):
46         global dbug
47         threading.Thread.__init__(self)
48         self.running = True
49         self.config = config
50         self.dbusHandler = dbusHandler
51         if dbug:
52             self.dbug = open(CONFIGDIR+"dbug.log", "w")
53         
54     def run(self):
55         global dbug
56         if dbug:
57             self.dbug.write("Starting updates\n")
58         try:
59             self.dbusHandler.UpdateStarted()
60             (use_proxy, proxy) = self.config.getProxy()
61             listing = Listing(CONFIGDIR)
62             for key in 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                         listing.updateFeed(key, self.config.getExpiry(), proxy=proxy, imageCache=self.config.getImageCache() )
70                     else:
71                         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             import traceback
87             file = open("/home/user/.feedingit/feedingit_update.log", "a")
88             traceback.print_exc(file=file)
89             file.close()
90             #pass
91         if dbug:
92             self.dbug.write("About to main_quit\n")
93         mainloop.quit()
94         if dbug:
95             self.dbug.write("After main_quit\n")
96             self.dbug.close()
97
98 class FeedUpdate():
99     def __init__(self):
100         self.config = Config(self, CONFIGDIR+"config.ini")
101         self.dbusHandler = UpdateServerObject(self)
102         self.updateThread = False
103         
104     def automaticUpdate(self):
105         #self.listing.updateFeeds()
106         if self.updateThread == False:
107             self.updateThread = Download(self.config, self.dbusHandler)
108             self.updateThread.start()
109         
110     def stopUpdate(self):
111         try:
112             self.updateThread.running = False
113         except:
114             mainloop.quit()
115
116 import dbus.mainloop.glib
117 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
118
119 gobject.threads_init()
120 mainloop = gobject.MainLoop()
121
122 app_lock = get_lock("app_lock")
123
124 if app_lock != None:
125     try:
126         feed = FeedUpdate()
127         mainloop.run()
128         del app_lock
129     except:
130         import traceback
131         file = open("/home/user/.feedingit/feedingit_update.log", "w")
132         traceback.print_exc(file=file)
133         file.close()
134 else:
135     file = open("/home/user/.feedingit/feedingit_update.log", "a")
136     file.write("Update in progress")
137     file.close()
138