opml.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Specs: http://opml.org/spec2.opml
  9. type opmlDocument struct {
  10. XMLName xml.Name `xml:"opml"`
  11. Version string `xml:"version,attr"`
  12. Header opmlHeader `xml:"head"`
  13. Outlines []opmlOutline `xml:"body>outline"`
  14. }
  15. func NewOPMLDocument() *opmlDocument {
  16. return &opmlDocument{}
  17. }
  18. func (o *opmlDocument) GetSubscriptionList() SubcriptionList {
  19. var subscriptions SubcriptionList
  20. for _, outline := range o.Outlines {
  21. if len(outline.Outlines) > 0 {
  22. for _, element := range outline.Outlines {
  23. // outline.Text is only available in OPML v2.
  24. subscriptions = element.Append(subscriptions, outline.Text)
  25. }
  26. } else {
  27. subscriptions = outline.Append(subscriptions, "")
  28. }
  29. }
  30. return subscriptions
  31. }
  32. type opmlHeader struct {
  33. Title string `xml:"title,omitempty"`
  34. DateCreated string `xml:"dateCreated,omitempty"`
  35. OwnerName string `xml:"ownerName,omitempty"`
  36. }
  37. type opmlOutline struct {
  38. Title string `xml:"title,attr,omitempty"`
  39. Text string `xml:"text,attr"`
  40. FeedURL string `xml:"xmlUrl,attr,omitempty"`
  41. SiteURL string `xml:"htmlUrl,attr,omitempty"`
  42. Outlines []opmlOutline `xml:"outline,omitempty"`
  43. }
  44. func (o *opmlOutline) GetTitle() string {
  45. if o.Title != "" {
  46. return o.Title
  47. }
  48. if o.Text != "" {
  49. return o.Text
  50. }
  51. if o.SiteURL != "" {
  52. return o.SiteURL
  53. }
  54. if o.FeedURL != "" {
  55. return o.FeedURL
  56. }
  57. return ""
  58. }
  59. func (o *opmlOutline) GetSiteURL() string {
  60. if o.SiteURL != "" {
  61. return o.SiteURL
  62. }
  63. return o.FeedURL
  64. }
  65. func (o *opmlOutline) Append(subscriptions SubcriptionList, category string) SubcriptionList {
  66. if o.FeedURL != "" {
  67. subscriptions = append(subscriptions, &Subcription{
  68. Title: o.GetTitle(),
  69. FeedURL: o.FeedURL,
  70. SiteURL: o.GetSiteURL(),
  71. CategoryName: category,
  72. })
  73. }
  74. return subscriptions
  75. }