opml.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package opml // import "miniflux.app/reader/opml"
  5. import (
  6. "encoding/xml"
  7. )
  8. type opml struct {
  9. XMLName xml.Name `xml:"opml"`
  10. Version string `xml:"version,attr"`
  11. Outlines []outline `xml:"body>outline"`
  12. }
  13. type outline struct {
  14. Title string `xml:"title,attr,omitempty"`
  15. Text string `xml:"text,attr"`
  16. FeedURL string `xml:"xmlUrl,attr,omitempty"`
  17. SiteURL string `xml:"htmlUrl,attr,omitempty"`
  18. Outlines []outline `xml:"outline,omitempty"`
  19. }
  20. func (o *outline) GetTitle() string {
  21. if o.Title != "" {
  22. return o.Title
  23. }
  24. if o.Text != "" {
  25. return o.Text
  26. }
  27. if o.SiteURL != "" {
  28. return o.SiteURL
  29. }
  30. if o.FeedURL != "" {
  31. return o.FeedURL
  32. }
  33. return ""
  34. }
  35. func (o *outline) GetSiteURL() string {
  36. if o.SiteURL != "" {
  37. return o.SiteURL
  38. }
  39. return o.FeedURL
  40. }
  41. func (o *outline) Append(subscriptions SubcriptionList, category string) SubcriptionList {
  42. if o.FeedURL != "" {
  43. subscriptions = append(subscriptions, &Subcription{
  44. Title: o.GetTitle(),
  45. FeedURL: o.FeedURL,
  46. SiteURL: o.GetSiteURL(),
  47. CategoryName: category,
  48. })
  49. }
  50. return subscriptions
  51. }
  52. func (o *opml) Transform() SubcriptionList {
  53. var subscriptions SubcriptionList
  54. for _, outline := range o.Outlines {
  55. if len(outline.Outlines) > 0 {
  56. for _, element := range outline.Outlines {
  57. // outline.Text is only available in OPML v2.
  58. subscriptions = element.Append(subscriptions, outline.Text)
  59. }
  60. } else {
  61. subscriptions = outline.Append(subscriptions, "")
  62. }
  63. }
  64. return subscriptions
  65. }