Group OPML feeds by category in exportUnverified
222591d parent: 1aed63a modified
internal/feed/opml.go +28 -1 | @@ -104,6 +104,8 @@ func GenerateOPML(feeds []FeedURL, title string) ([]byte, error) { | ||
| 104 | 104 | } |
| 105 | 105 | opml.Head.Title = title |
| 106 | 106 | |
| 107 | + categoryMap := make(map[string][]Outline) | |
| 108 | + var categories []string | |
| 107 | 109 | for _, f := range feeds { |
| 108 | 110 | outline := Outline{ |
| 109 | 111 | Text: f.Title, |
| @@ -111,7 +113,23 @@ func GenerateOPML(feeds []FeedURL, title string) ([]byte, error) { | ||
| 111 | 113 | XMLURL: f.URL, |
| 112 | 114 | HTMLURL: f.SiteURL, |
| 113 | 115 | } |
| 114 | - opml.Body.Outlines = append(opml.Body.Outlines, outline) | |
| 116 | + if f.Category != "" { | |
| 117 | + categoryMap[f.Category] = append(categoryMap[f.Category], outline) | |
| 118 | + if !contains(categories, f.Category) { | |
| 119 | + categories = append(categories, f.Category) | |
| 120 | + } | |
| 121 | + } else { | |
| 122 | + opml.Body.Outlines = append(opml.Body.Outlines, outline) | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + for _, cat := range categories { | |
| 127 | + parent := Outline{ | |
| 128 | + Text: cat, | |
| 129 | + Title: cat, | |
| 130 | + Outlines: categoryMap[cat], | |
| 131 | + } | |
| 132 | + opml.Body.Outlines = append(opml.Body.Outlines, parent) | |
| 115 | 133 | } |
| 116 | 134 | |
| 117 | 135 | var buf bytes.Buffer |
| @@ -125,3 +143,12 @@ func GenerateOPML(feeds []FeedURL, title string) ([]byte, error) { | ||
| 125 | 143 | |
| 126 | 144 | return buf.Bytes(), nil |
| 127 | 145 | } |
| 146 | + | |
| 147 | +func contains(slice []string, s string) bool { | |
| 148 | + for _, v := range slice { | |
| 149 | + if v == s { | |
| 150 | + return true | |
| 151 | + } | |
| 152 | + } | |
| 153 | + return false | |
| 154 | +} | |
| @@ -104,6 +104,8 @@ func GenerateOPML(feeds []FeedURL, title string) ([]byte, error) { | |||
| 104 | } | 104 | } |
| 105 | opml.Head.Title = title | 105 | opml.Head.Title = title |
| 106 | 106 | ||
| 107 | + categoryMap := make(map[string][]Outline) | ||
| 108 | + var categories []string | ||
| 107 | for _, f := range feeds { | 109 | for _, f := range feeds { |
| 108 | outline := Outline{ | 110 | outline := Outline{ |
| 109 | Text: f.Title, | 111 | Text: f.Title, |
| @@ -111,7 +113,23 @@ func GenerateOPML(feeds []FeedURL, title string) ([]byte, error) { | |||
| 111 | XMLURL: f.URL, | 113 | XMLURL: f.URL, |
| 112 | HTMLURL: f.SiteURL, | 114 | HTMLURL: f.SiteURL, |
| 113 | } | 115 | } |
| 114 | - opml.Body.Outlines = append(opml.Body.Outlines, outline) | 116 | + if f.Category != "" { |
| 117 | + categoryMap[f.Category] = append(categoryMap[f.Category], outline) | ||
| 118 | + if !contains(categories, f.Category) { | ||
| 119 | + categories = append(categories, f.Category) | ||
| 120 | + } | ||
| 121 | + } else { | ||
| 122 | + opml.Body.Outlines = append(opml.Body.Outlines, outline) | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + for _, cat := range categories { | ||
| 127 | + parent := Outline{ | ||
| 128 | + Text: cat, | ||
| 129 | + Title: cat, | ||
| 130 | + Outlines: categoryMap[cat], | ||
| 131 | + } | ||
| 132 | + opml.Body.Outlines = append(opml.Body.Outlines, parent) | ||
| 115 | } | 133 | } |
| 116 | 134 | ||
| 117 | var buf bytes.Buffer | 135 | var buf bytes.Buffer |
| @@ -125,3 +143,12 @@ func GenerateOPML(feeds []FeedURL, title string) ([]byte, error) { | |||
| 125 | 143 | ||
| 126 | return buf.Bytes(), nil | 144 | return buf.Bytes(), nil |
| 127 | } | 145 | } |
| 146 | + | ||
| 147 | +func contains(slice []string, s string) bool { | ||
| 148 | + for _, v := range slice { | ||
| 149 | + if v == s { | ||
| 150 | + return true | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + return false | ||
| 154 | +} | ||
modified
internal/server/feeds_handler.go +3 -2 | @@ -277,8 +277,9 @@ func (s *Server) handleOPMLDownload(w http.ResponseWriter, r *http.Request) { | ||
| 277 | 277 | var feedURLs []feed.FeedURL |
| 278 | 278 | for _, sub := range subs { |
| 279 | 279 | feedURLs = append(feedURLs, feed.FeedURL{ |
| 280 | - URL: sub.FeedURL, | |
| 281 | - Title: sub.FeedTitle, | |
| 280 | + URL: sub.FeedURL, | |
| 281 | + Title: sub.FeedTitle, | |
| 282 | + Category: sub.Category.String, | |
| 282 | 283 | }) |
| 283 | 284 | } |
| 284 | 285 | |
| @@ -277,8 +277,9 @@ func (s *Server) handleOPMLDownload(w http.ResponseWriter, r *http.Request) { | |||
| 277 | var feedURLs []feed.FeedURL | 277 | var feedURLs []feed.FeedURL |
| 278 | for _, sub := range subs { | 278 | for _, sub := range subs { |
| 279 | feedURLs = append(feedURLs, feed.FeedURL{ | 279 | feedURLs = append(feedURLs, feed.FeedURL{ |
| 280 | - URL: sub.FeedURL, | 280 | + URL: sub.FeedURL, |
| 281 | - Title: sub.FeedTitle, | 281 | + Title: sub.FeedTitle, |
| 282 | + Category: sub.Category.String, | ||
| 282 | }) | 283 | }) |
| 283 | } | 284 | } |
| 284 | 285 | ||