Skip to content

Commit c6c2a4d

Browse files
Merge pull request #50 from Keeper-of-the-Keys/opml-section-export
Opml section export and import
2 parents 627b155 + 09b7076 commit c6c2a4d

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/gpodder/opml.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,16 @@ def __init__(self, url, opml_str=None):
7070
else:
7171
doc = xml.dom.minidom.parseString(util.urlopen(url).read())
7272

73+
section = None
7374
for outline in doc.getElementsByTagName('outline'):
7475
# Make sure we are dealing with a valid link type (ignore case)
7576
otl_type = outline.getAttribute('type')
7677
if otl_type is None or otl_type.lower() not in self.VALID_TYPES:
78+
otl_title = outline.getAttribute('title')
79+
otl_text = outline.getAttribute('text')
80+
#gPodder sections will have name == text, if OPML accepts it type=section
81+
if otl_title is not None and otl_title == otl_text:
82+
section = otl_title
7783
continue
7884

7985
if outline.getAttribute('xmlUrl') or outline.getAttribute('url'):
@@ -86,6 +92,7 @@ def __init__(self, url, opml_str=None):
8692
'description': (outline.getAttribute('text') or
8793
outline.getAttribute('xmlUrl') or
8894
outline.getAttribute('url')),
95+
'section': section or 'audio'
8996
}
9097

9198
if channel['description'] == channel['title']:
@@ -141,6 +148,15 @@ def create_outline(self, doc, channel):
141148
outline.setAttribute('type', self.FEED_TYPE)
142149
return outline
143150

151+
def create_section(self, doc, name):
152+
"""
153+
Creates an empty OPML ouline element used to divide sections.
154+
"""
155+
section = doc.createElement('outline')
156+
section.setAttribute('title', name)
157+
section.setAttribute('text', name)
158+
return section
159+
144160
def write(self, channels):
145161
"""
146162
Creates a XML document containing metadata for each
@@ -165,8 +181,16 @@ def write(self, channels):
165181
opml.appendChild(head)
166182

167183
body = doc.createElement('body')
184+
sections = {}
168185
for channel in channels:
169-
body.appendChild(self.create_outline(doc, channel))
186+
if channel.section not in sections.keys():
187+
sections[channel.section] = self.create_section(doc, channel.section)
188+
189+
sections[channel.section].appendChild(self.create_outline(doc, channel))
190+
191+
for section in sections.values():
192+
body.appendChild(section)
193+
170194
opml.appendChild(body)
171195

172196
if self.filename is None:

0 commit comments

Comments
 (0)