Separated helper classes into rss.py. Added test.py with QT gui
[feedingit] / src / rss.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        : FeedingIt.py
21 # Author      : Yves Marcoz
22 # Version     : 0.1
23 # Description : PyGtk Example 
24 # ============================================================================
25
26 CONFIGDIR="/home/user/.feedingit/"
27
28 def getId(string):
29     return md5.new(string).hexdigest()
30
31 class Feed:
32     # Contains all the info about a single feed (articles, ...), and expose the data
33     def __init__(self, name, url):
34         self.feed = []
35         self.name = name
36         self.url = url
37         self.updateTime = "Never"
38         #self.feed=feedparser.parse(url)
39     
40     def updateFeed(self):
41         self.feed=feedparser.parse(self.url)
42         self.updateTime = time.asctime()
43         file = open(CONFIGDIR+getId(self.name), "w")
44         pickle.dump(self, file )
45         file.close()
46     
47     def getUpdateTime(self):
48         return self.updateTime
49     
50     def getEntries(self):
51         try:
52             return self.feed["entries"]
53         except:
54             return []
55     
56     def getItem(self, index):
57         try:
58             return self.feed["entries"][index]
59         except:
60             return []
61     
62     def getArticle(self, index):
63         entry = self.feed["entries"][index]
64         text = "<h4><a href=\"" + entry["link"] + "\">" + entry["title"] + "</a></h4>"
65         text = text + "<small><i>Date: " + time.strftime("%a, %d %b %Y %H:%M:%S",entry["updated_parsed"]) + "</i></small>"
66         text = text + "<BR />"
67         text = text + entry["summary"]
68         return text    
69     
70 class Listing:
71     # Lists all the feeds in a dictionary, and expose the data
72     
73     def updateFeeds(self):
74         for key in self.listOfFeeds.keys():
75             self.feeds[key].updateFeed()
76             
77     def getFeed(self, key):
78         return self.feeds[key]
79     
80     def getFeedUpdateTime(self, key):
81         return self.feeds[key].getUpdateTime()
82    
83     def getFeedTitle(self, key):
84         return self.listOfFeeds[key]["title"]
85     
86     def getFeedUrl(self, key):
87         return self.listOfFeeds[key]["url"]
88     
89     def getListOfFeeds(self):
90         return self.listOfFeeds.keys()
91     
92     def addFeed(self, title, url):
93         self.listOfFeeds[getId(title)] = {"title":title, "url":url}
94         self.saveConfig()
95         self.feeds[getId(title)] = Feed(title, url)
96     
97     def saveConfig(self):
98         file = open(CONFIGDIR+"feeds.pickle", "w")
99         pickle.dump(self.listOfFeeds, file)
100         file.close()
101     
102     def __init__(self):
103         self.feeds = {}
104         if isfile(CONFIGDIR+"feeds.pickle"):
105             file = open(CONFIGDIR+"feeds.pickle")
106             self.listOfFeeds = pickle.load(file)
107             file.close()
108         else:
109             self.listOfFeeds = {getId("Slashdot"):{"title":"Slashdot", "url":"http://rss.slashdot.org/Slashdot/slashdot"}, }
110         for key in self.listOfFeeds.keys():
111             if isfile(CONFIGDIR+key):
112                 file = open(CONFIGDIR+key)
113                 self.feeds[key] = pickle.load(file)
114                 file.close()
115             else:
116                 self.feeds[key] = Feed(self.listOfFeeds[key]["title"], self.listOfFeeds[key]["url"])
117         self.saveConfig()