func Subscribe(opmlPath, page string) error { feed, err := findFeed(page) if err != nil { return err } outline, err := opml.Load(opmlPath) if err != nil { return err } newfeed, err := getData(feed) if err != nil { return err } outline.Body.Outline = append(outline.Body.Outline, newfeed) file, err := os.OpenFile(opmlPath, os.O_WRONLY|os.O_TRUNC, 0) if err != nil { return err } defer file.Close() return outline.WriteTo(file) }
func (h *listHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { outline, err := opml.Load(h.opmlPath) if err != nil { log.Println("list", err) w.WriteHeader(500) return } var list feeds for _, line := range outline.Body.Outline { if line.Type == "rss" { f := feed{ FeedUrl: line.XmlUrl, WebsiteUrl: line.HtmlUrl, FeedTitle: line.Title, FeedDescription: line.Description, } if f.FeedTitle == "" { f.FeedTitle = f.FeedUrl } list = append(list, f) } } sort.Sort(list) w.Header().Add("Content-Type", "text/html") views.Index.Execute(w, struct { Url string PathPrefix string Feeds []feed }{h.url, h.pathPrefix, list}) }
func main() { flag.Parse() if *help || flag.NArg() == 0 { printHelp() return } var wg sync.WaitGroup waitFor := func(name string, f func() error) { log.Println(name, "waiting to close") wg.Add(1) if err := f(); err != nil { log.Println("waitFor:", err) } wg.Done() log.Println(name, "closed") } opmlPath := flag.Arg(0) duration, err := time.ParseDuration(*cutOff) if err != nil { log.Println(err) return } cacheTimeout, err := time.ParseDuration(*refresh) if err != nil { log.Println(err) return } store, err := loadDatastore() if err != nil { log.Println(err) return } defer waitFor("datastore", store.Close) outline, err := opml.Load(opmlPath) if err != nil { log.Println(err) return } feeds := river.New(store, river.Options{ Mapping: river.DefaultMapping, CutOff: duration, Refresh: cacheTimeout, LogLength: 500, }) defer waitFor("feeds", feeds.Close) subs := subscriptions.FromOpml(outline) for _, sub := range subs.List() { feeds.Add(sub.Uri) } watcher, err := watchFile(opmlPath, func() { log.Printf("reading %s\n", opmlPath) outline, err := opml.Load(opmlPath) if err != nil { log.Printf("could not read %s: %s\n", opmlPath, err) return } added, removed := subscriptions.Diff(subs, subscriptions.FromOpml(outline)) for _, uri := range added { feeds.Add(uri) subs.Add(uri) } for _, uri := range removed { feeds.Remove(uri) subs.Remove(uri) } }) if err != nil { log.Printf("could not start watching %s: %v\n", opmlPath, err) } defer waitFor("watcher", watcher.Close) http.Handle("/river/", http.StripPrefix("/river", newRiverHandler(feeds))) serve.Serve(*port, *socket, http.DefaultServeMux) wg.Wait() }