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