fix importing certain opml files

This commit is contained in:
Nazar Kanaev 2021-04-22 11:15:16 +01:00
parent 19889c1457
commit 2ae62855cc
3 changed files with 42 additions and 2 deletions

View File

@ -1,3 +1,7 @@
# upcoming
- (fix) handle opml files not following the spec (thanks to @huangnauh for the report)
# v2.0 (2021-04-18) # v2.0 (2021-04-18)
- (new) user interface tweaks - (new) user interface tweaks

View File

@ -13,6 +13,7 @@ type opml struct {
type outline struct { type outline struct {
Type string `xml:"type,attr,omitempty"` Type string `xml:"type,attr,omitempty"`
Title string `xml:"text,attr"` Title string `xml:"text,attr"`
Title2 string `xml:"title,attr,omitempty"`
FeedUrl string `xml:"xmlUrl,attr,omitempty"` FeedUrl string `xml:"xmlUrl,attr,omitempty"`
SiteUrl string `xml:"htmlUrl,attr,omitempty"` SiteUrl string `xml:"htmlUrl,attr,omitempty"`
Outlines []outline `xml:"outline,omitempty"` Outlines []outline `xml:"outline,omitempty"`
@ -21,14 +22,18 @@ type outline struct {
func buildFolder(title string, outlines []outline) Folder { func buildFolder(title string, outlines []outline) Folder {
folder := Folder{Title: title} folder := Folder{Title: title}
for _, outline := range outlines { for _, outline := range outlines {
if outline.Type == "rss" { if outline.Type == "rss" || outline.FeedUrl != "" {
folder.Feeds = append(folder.Feeds, Feed{ folder.Feeds = append(folder.Feeds, Feed{
Title: outline.Title, Title: outline.Title,
FeedUrl: outline.FeedUrl, FeedUrl: outline.FeedUrl,
SiteUrl: outline.SiteUrl, SiteUrl: outline.SiteUrl,
}) })
} else { } else {
subfolder := buildFolder(outline.Title, outline.Outlines) title := outline.Title
if title == "" {
title = outline.Title2
}
subfolder := buildFolder(title, outline.Outlines)
folder.Folders = append(folder.Folders, subfolder) folder.Folders = append(folder.Folders, subfolder)
} }
} }

View File

@ -56,3 +56,34 @@ func TestParse(t *testing.T) {
t.Fatal("invalid opml") t.Fatal("invalid opml")
} }
} }
func TestParseFallback(t *testing.T) {
// as reported in https://github.com/nkanaev/yarr/pull/56
// the feed below comes without `outline[text]` & `outline[type=rss]` attributes
have, _ := Parse(strings.NewReader(`
<?xml version="1.0" encoding="utf-8"?>
<opml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0">
<head>
<title>Newsflow</title>
</head>
<body>
<outline title="foldertitle">
<outline htmlUrl="https://example.com" text="feedtext" title="feedtitle" xmlUrl="https://example.com/feed.xml" />
</outline>
</body>
</opml>
`))
want := Folder{
Folders: []Folder{{
Title: "foldertitle",
Feeds: []Feed{
{Title: "feedtext", FeedUrl: "https://example.com/feed.xml", SiteUrl: "https://example.com"},
},
}},
}
if !reflect.DeepEqual(want, have) {
t.Logf("want: %#v", want)
t.Logf("have: %#v", have)
t.Fatal("invalid opml")
}
}