iteration 1 - Use gobject for events in feed, summaryitem and feedlist
[straw.git] / src / lib / OPMLImport.py
blob763c99e9ca4c4e29f290d00f5003734b08631cee
1 """ OPMLImport.py
2 """
4 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
5 __license__ = """
6 Straw is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2 of the License, or (at your option) any later
9 version.
11 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 Place - Suite 330, Boston, MA 02111-1307, USA. """
20 import OPML
21 import feeds
23 class BlogListEntry(object):
24 __slots__ = ('text', 'url')
26 def _find_entries(outline):
27 entries = []
28 for c in outline.children:
29 entries += _find_entries(c)
30 type = outline.get('type', '')
31 text = outline.get('text', '')
32 e = None
33 if type == 'link':
34 url = outline.get('url', '')
35 if url != '':
36 e = BlogListEntry()
37 e.text = text
38 e.url = url
39 else:
40 xmlurl = outline.get('xmlUrl', '')
41 e = BlogListEntry()
42 e.text = text
43 if text == '':
44 title = outline.get('title', '')
45 if title == '':
46 e = None
47 e.text = title
48 if e != None:
49 if xmlurl != '':
50 # there's something in xmlurl. There's a good chance that's
51 # our feed's URL
52 e.url = xmlurl
53 else:
54 htmlurl = outline.get('htmlUrl', '')
55 if htmlurl != '':
56 # there's something in htmlurl, and xmlurl is empty. This
57 # might be our feed's URL.
58 e.url = htmlurl
59 else:
60 # nothing else to try.
61 e = None
62 if e is not None:
63 entries[0:0] = [e]
64 return entries
66 def find_entries(outlines):
67 entries = []
68 for o in outlines:
69 entries += _find_entries(o)
70 return entries
72 def read(stream):
73 try:
74 o = OPML.parse(stream)
75 except ValueError:
76 return None
77 entries = find_entries(o.outlines)
78 ret = list()
79 edict = dict()
80 # avoid duplicates.
81 for e in entries:
82 ek = (e.text, e.url)
83 edict[ek] = edict.get(ek, 0) + 1
84 if edict[ek] < 2:
85 ret.append(e)
86 return ret
88 def import_opml(filename,category=None):
89 feedlist = feeds.get_instance()
90 fstream = open (filename)
91 opml = read(fstream)
92 if not opml:
93 return
94 feeds = [feed.access_info[0] for feed in feedlist]
95 newitems = [feeds.Feed.create_new_feed(b.text, b.url) for b in opml if b.url not in feeds]
96 feedlist.extend(category, newitems)
97 return