func removeFilterHandler(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() utils.Checkerr(err) filter := r.Form["filter"][0] err = db.RemoveFilter(filter) utils.Checkerr(err) cache.Empty() http.Redirect(w, r, "/settings", http.StatusFound) }
func settingsHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, xml.Header) ismobile := utils.IsMobile(r.Header) xmlheader, err := buildXMLHeader("settings", ismobile) utils.Checkerr(err) fmt.Fprintf(w, "%s", xmlheader) enc := xml.NewEncoder(w) p := buildBasicPage() enc.Encode(p) }
func convertAtomEntryToDBEntry(entry feedparser.Entry) structure.Entry { var newentry structure.Entry newentry.Title = entry.Title newentry.Summary = entry.Summary if len(entry.Summary) >= len(entry.Content) { article, err := siteparser.GetArticleForSite(entry.Link.Href) utils.Checkerr(err) if len(article) > 0 && len(article) > len(entry.Summary) { newentry.Content = article } else { newentry.Content = entry.Summary } } else { newentry.Content = entry.Content } newentry.ID = entry.ID newentry.Updated = entry.Updated if !entry.GUID.IsPermaLink && len(entry.GUID.Content) > 0 { newentry.PermaLink = entry.GUID.Content } else { newentry.PermaLink = entry.Link.Href } newentry.Author = entry.Author.Name meta, err := siteparser.GetMetaForSite(newentry.PermaLink) if err != nil { newentry.PermaLink = entry.Link.Href meta, err = siteparser.GetMetaForSite(entry.Link.Href) } utils.Checkerr(err) newentry.Twitter = meta.TwitterCreator newentry.ArticleID = createNewID() hasImage, isPortrait, err := SyncImage(meta.Image, newentry.ArticleID) utils.Checkerr(err) newentry.HasImage = hasImage switch isPortrait { case false: newentry.ImageRotation = "landscape" default: newentry.ImageRotation = "portrait" } return newentry }
func Sync() { var feedsToUpdate []structure.Feed feeds, err := db.GetAllFeeds() utils.Checkerr(err) for _, feed := range feeds { maxTimeSinceUpdate := 24 / feed.ChangeFrequency if time.Now().Sub(feed.LastUpdate).Hours() > maxTimeSinceUpdate { feedsToUpdate = append(feedsToUpdate, feed) } } processFeeds(feedsToUpdate) }
func addFeedHandler(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() utils.Checkerr(err) feed := r.Form["feed"][0] go func(feed string) { newfeed, _ := db.AddFeed(feed) skywalker.UpdateFeed(newfeed) cache.Empty() }(feed) time.Sleep(4 * time.Second) http.Redirect(w, r, "/settings", http.StatusFound) }
func latestHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, xml.Header) ismobile := utils.IsMobile(r.Header) xmlheader, err := buildXMLHeader("feed", ismobile) utils.Checkerr(err) fmt.Fprintf(w, "%s", xmlheader) enc := xml.NewEncoder(w) var p Page nav := buildNavigation() p.Navigation = nav c, _ := cache.Get("all") content := Content{Title: "Latest", Entries: c.Entries} p.Content = content enc.Encode(p) }
func UpdateFeed(feed structure.Feed) { update, err := feedparser.ParseURL(feed.URL) utils.Checkerr(err) var link string for _, li := range update.Link { if li.Rel == "alternate" { link = li.Href } } if len(link) == 0 { link = update.Link[0].Href } meta, err := siteparser.GetMetaForSite(link) utils.Checkerr(err) err = db.UpdateFeedInformation(update.Title, update.Subtitle, meta.Icon, meta.TwitterSite, feed.URL, update.Updated) utils.Checkerr(err) var entries []structure.Entry for _, entry := range update.Entries { exists := false for _, oldentry := range feed.Entries { if strings.Compare(entry.ID, oldentry.ID) == 0 && entry.Updated.Equal(oldentry.Updated) { exists = true } } if !exists { newentry := convertAtomEntryToDBEntry(entry) entries = append(entries, newentry) } } err = db.UpdateFeedEntryList(entries, feed.URL) utils.Checkerr(err) feed, err = db.GetFeedByURL(feed.URL) utils.Checkerr(err) sortedentries := structure.SortEntriesByDate(feed.Entries) var occasions []time.Time for _, entry := range sortedentries { occasions = append(occasions, entry.Updated) } changefrequency := calculateChangeFrequency(occasions) err = db.UpdateFeedChangeFrequency(feed.URL, changefrequency) utils.Checkerr(err) err = db.UpdateFeedLastUpdate(feed.URL, time.Now()) utils.Checkerr(err) }