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