gof.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "bytes"
  4. "flag"
  5. "log"
  6. "net/url"
  7. "text/template"
  8. "github.com/SlyMarbo/rss"
  9. )
  10. var (
  11. conf *config
  12. debug bool
  13. debugOrDryRun bool
  14. )
  15. type article struct {
  16. Title, URL, Summary string
  17. }
  18. func main() {
  19. var (
  20. configFile string
  21. dryRun bool
  22. )
  23. flag.StringVar(&configFile, "c", "gof.yaml", "the configuration file to use")
  24. flag.BoolVar(&dryRun, "dry-run", false, "whether to perform a dry run or not")
  25. flag.BoolVar(&debug, "debug", false, "enable debugging")
  26. flag.Parse()
  27. debugOrDryRun = debug || dryRun
  28. log.Println(configFile)
  29. log.Println("gof starting up...")
  30. conf = readConfig(configFile)
  31. log.Printf("Version: %s\n", conf.Meta.Version)
  32. log.Printf("Build time: %s\n", conf.Meta.Buildtime)
  33. var tpls = make(map[string]*template.Template)
  34. var formats = make(map[string]string)
  35. for accountIndex, a := range conf.Accounts {
  36. for feedIndex, f := range a.Feeds {
  37. tmpl, err := template.New(f.URL).Parse(f.Template)
  38. if err != nil {
  39. log.Fatalf("Failed to parse template [%s]. Error: %s", f.Template, err.Error())
  40. }
  41. // Default format to "plain", if blank
  42. if f.Format == "" {
  43. conf.Accounts[accountIndex].Feeds[feedIndex].Format = "plain"
  44. f.Format = "plain"
  45. }
  46. tpls[f.URL] = tmpl
  47. formats[f.URL] = f.Format
  48. }
  49. }
  50. for _, account := range conf.Accounts {
  51. var toot message
  52. // Get feeds
  53. log.Printf("Fetching feeds for account [%s]...", account.Name)
  54. var feeds []*rss.Feed
  55. for _, source := range account.Feeds {
  56. toot.feed = source
  57. if debug {
  58. log.Printf("source:\n\n%v", source)
  59. }
  60. feed, err := rss.Fetch(source.URL)
  61. if err != nil {
  62. log.Printf("Error fetching %s: %s", source.URL, err.Error())
  63. continue
  64. }
  65. feeds = append(feeds, feed)
  66. log.Printf("Fetched [%s]", feed.Title)
  67. }
  68. if len(feeds) == 0 {
  69. log.Fatal("Expected at least one feed to successfully fetch.")
  70. }
  71. // Loop through feeds
  72. for _, feed := range feeds {
  73. // Get feed items
  74. if len(feed.Items) == 0 {
  75. log.Printf("Warning: feed %s has no items.", feed.Title)
  76. continue
  77. }
  78. items := feed.Items
  79. if len(items) > 1 {
  80. items = items[:1]
  81. }
  82. base, err := url.Parse(feed.UpdateURL)
  83. if err != nil {
  84. log.Fatal("failed parsing update URL of the feed")
  85. }
  86. feedLink, _ := url.Parse(feed.Link)
  87. if err != nil {
  88. log.Fatal("failed parsing canonical feed URL of the feed")
  89. }
  90. // Loop through items
  91. for _, item := range items {
  92. if item.Date.Before(conf.LastUpdated) && !debug {
  93. log.Println("No new items. Skipping.")
  94. continue
  95. }
  96. itemLink, err := url.Parse(item.Link)
  97. if err != nil {
  98. log.Fatal("failed parsing article URL of the feed item")
  99. }
  100. // Make sure data looks OK
  101. log.Printf("Item Data:\n\tTimestamp: %s\n\tSite URL: %s\n\tFeed Title: %s\n\tItem Title: %s\n\tItem URL: %s\n",
  102. item.Date, base.ResolveReference(feedLink).String(),
  103. feed.Title, item.Title, base.ResolveReference(itemLink).String())
  104. i := article{
  105. Title: item.Title,
  106. Summary: item.Summary,
  107. URL: base.ResolveReference(itemLink).String(),
  108. }
  109. buf := new(bytes.Buffer)
  110. err = tpls[base.String()].Execute(buf, i)
  111. if err != nil {
  112. log.Fatalf("Error executing template [%v]. Error: %s", tpls[base.String()], err.Error())
  113. }
  114. toot.account = account
  115. toot.content = buf.String()
  116. if err = toot.post(); err != nil {
  117. log.Fatalf("Failed to post message \"%s\". Error: %s", buf.String(), err.Error())
  118. }
  119. }
  120. }
  121. }
  122. if !debugOrDryRun {
  123. // update timestamp in config
  124. conf.updateLastUpdated()
  125. // save config
  126. err := conf.Save()
  127. if err != nil {
  128. log.Fatalf("Failed to save config to file. Error: %s", err.Error())
  129. }
  130. }
  131. }