~doctormo/gnome-wallchanger/trunk

« back to all changes in this revision

Viewing changes to gwc/rssbase.py

  • Committer: Martin Owens (DoctorMO)
  • Date: 2009-10-11 19:32:33 UTC
  • Revision ID: doctormo@gmail.com-20091011193233-15p70kim7zsjyhqx
Add new modules for ver 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2009, Martin Owens.
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
#
 
17
"""
 
18
Plugin base for RSS based wallpaper sources.
 
19
"""
 
20
 
 
21
from gwc.base import ListerBase
 
22
 
 
23
class rssBase(ListerBase):
 
24
    """Get a random wallpaper from an RSS feed"""
 
25
    def __init__(self, folder, url):
 
26
        self._url = unicode(url)
 
27
        lister.__init__(self, folder)
 
28
 
 
29
    def getList(self):
 
30
        """Download rss feed, turn into list of addresses"""
 
31
        feedxml = self.get_url(self._url)
 
32
        if True:
 
33
            xmldoc = minidom.parseString(feedxml)
 
34
            for part in xmldoc.childNodes:
 
35
                if part.nodeType==1 and part.tagName=='rss':
 
36
                    for channel in part.childNodes:
 
37
                        if channel.nodeType==1 and channel.tagName=='channel':
 
38
                            for item in self.listFromRSS(channel):
 
39
                                yield item
 
40
 
 
41
    def listFromRSS(self, channel):
 
42
        for item in channel.childNodes:
 
43
            if item.nodeType==1 and item.tagName=='item':
 
44
                item = self.rssItem(item)
 
45
                if item:
 
46
                    yield item
 
47
 
 
48
    def rssItem(self, item):
 
49
        link = None
 
50
        for attr in item.childNodes:
 
51
            if attr.nodeType==1:
 
52
                if attr.tagName=='link':
 
53
                    # This allows feeds that put their images as links, or for hops.
 
54
                    link = ''
 
55
                    for text in attr.childNodes:
 
56
                        if text.nodeType==3:
 
57
                            link += text.data
 
58
                elif attr.tagName=='media:content' and attr.getAttribute('type')=='image/jpeg':
 
59
                    # This allows rss feeds targeting media content to be used.
 
60
                    return attr.getAttribute('url')
 
61
        return link
 
62