Browse Source

In the beginning there were feeds...

Matthew Jorgensen 5 years ago
parent
commit
0896966aaf
3 changed files with 141 additions and 0 deletions
  1. 65 0
      config.go
  2. 70 0
      gof.go
  3. 6 0
      gof.yaml.example

+ 65 - 0
config.go

@@ -0,0 +1,65 @@
+package main
+
+import (
+	"io/ioutil"
+	"log"
+	"os/user"
+	"time"
+
+	"gopkg.in/yaml.v2"
+)
+
+var (
+	dir, configFile string
+)
+
+type feed struct {
+	URL, Template string
+	Summary       bool
+}
+
+type config struct {
+	ClientID     string
+	ClientSecret string
+	AccessToken  string
+	Name         string
+	InstanceURL  string
+	Feeds        []feed
+	LastUpdated  time.Time
+}
+
+func readConfig() *config {
+	usr, _ := user.Current()
+	dir = usr.HomeDir
+	log.Println("reading config...")
+	configFile = "gof.yaml"
+	config := new(config)
+	cf, err := ioutil.ReadFile(configFile)
+	if err != nil {
+		log.Fatalln("Failed to read config: ", err)
+	}
+	err = yaml.Unmarshal(cf, &config)
+	if err != nil {
+		log.Panic(err)
+	}
+	return config
+}
+
+func (cf *config) updateLastUpdated() {
+	log.Println("updating lastupdated key...")
+	cf.LastUpdated = time.Now()
+}
+
+func (cf *config) Save() error {
+	log.Println("saving config to file...")
+	cfbytes, err := yaml.Marshal(cf)
+	if err != nil {
+		log.Fatalln("Failed to marshal config: ", err.Error())
+	}
+	err = ioutil.WriteFile(configFile, cfbytes, 0644)
+	if err != nil {
+		log.Fatalf("Failed to save config to file. Error: %s", err.Error())
+	}
+
+	return nil
+}

+ 70 - 0
gof.go

@@ -0,0 +1,70 @@
+package main
+
+import (
+	"log"
+	"net/url"
+
+	//"github.com/McKael/madon"
+	"github.com/SlyMarbo/rss"
+)
+
+func main() {
+	log.Println("gof starting up...")
+	config := readConfig()
+
+	// Get feeds
+	log.Println("Fetching feeds...")
+	var feeds []*rss.Feed
+	for _, source := range config.Feeds {
+		feed, err := rss.Fetch(source.URL)
+		if err != nil {
+			log.Printf("Error fetching %s: %s", source.URL, err.Error())
+			continue
+		}
+		feeds = append(feeds, feed)
+		log.Printf("Fetched %s", feed.Title)
+	}
+	if len(feeds) == 0 {
+		log.Fatal("Expected at least one feed to successfully fetch.")
+	}
+
+	// Loop through feeds
+	for _, feed := range feeds {
+		// Get feed items
+		if len(feed.Items) == 0 {
+			log.Printf("Warning: feed %s has no items.", feed.Title)
+			continue
+		}
+		items := feed.Items
+		if len(items) > 1 {
+			items = items[:1]
+		}
+		base, err := url.Parse(feed.UpdateURL)
+		if err != nil {
+			log.Fatal("failed parsing update URL of the feed")
+		}
+		feedLink, _ := url.Parse(feed.Link)
+		if err != nil {
+			log.Fatal("failed parsing canonical feed URL of the feed")
+		}
+
+		// Loop through items
+		for _, item := range items {
+			itemLink, err := url.Parse(item.Link)
+			if err != nil {
+				log.Fatal("failed parsing article URL of the feed item")
+			}
+
+			// Make sure data looks OK
+			// TODO: remove before release
+			log.Printf("Item Data:\n\tTimestamp: %s\n\tSite URL: %s\n\tFeed Title: %s\n\tItem Title: %s\n\tItem URL: %s\n",
+				item.Date, base.ResolveReference(feedLink).String(),
+				feed.Title, item.Title, base.ResolveReference(itemLink).String())
+		}
+	}
+
+	// update timestamp in config
+	config.updateLastUpdated()
+	// save config
+	config.Save()
+}

+ 6 - 0
gof.yaml.example

@@ -0,0 +1,6 @@
+clientID:
+clientSecret:
+accessToken:
+feeds:
+  - template: '{title} {url}'
+    url: https://yoursite.com/feed.xml