Skip to content

Commit

Permalink
yarr notation
Browse files Browse the repository at this point in the history
  • Loading branch information
trinidz committed Feb 16, 2025
1 parent a18ac90 commit 61edd29
Show file tree
Hide file tree
Showing 20 changed files with 37 additions and 109 deletions.
6 changes: 6 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ func deleteRemoteFollow(pubkeyHex string) nostr.Tags {

// TRUE if feed exists in bookmark event
func feedExists(pubkeyHex, privKeyHex, feedUrl string) (bool, error) {

if feedUrl == "" {
log.Printf("[ERROR] feedURL is empty")
return false, fmt.Errorf("feedURL is empty")
}

var bookMarkTags nostr.Tags

var bookmarkFilter nostr.Filter = nostr.Filter{
Expand Down
48 changes: 0 additions & 48 deletions feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"time"

md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
"github.com/microcosm-cc/bluemonday"
"github.com/mmcdole/gofeed"
"github.com/nbd-wtf/go-nostr"
Expand All @@ -36,53 +35,6 @@ var (
}
)

var rss_types = []string{
"rss+xml",
"atom+xml",
"feed+json",
"text/xml",
"application/xml",
}

func getFeedUrl(url string) string {
resp, err := client.Get(url)
if err != nil {
log.Printf("[ERROR] %s", err)
return ""
} else if resp.StatusCode >= 300 {
log.Printf("[ERROR] status code: %d", resp.StatusCode)
return ""
}

ct := resp.Header.Get("Content-Type")
for _, typ := range rss_types {
if strings.Contains(ct, typ) {
return url
}
}

if strings.Contains(ct, "text/html") {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Printf("[ERROR] %s", err)
return ""
}

for _, typ := range rss_types {
href, _ := doc.Find(fmt.Sprintf("link[type*='%s']", typ)).Attr("href")
if href == "" {
continue
}
if !strings.HasPrefix(href, "http") && !strings.HasPrefix(href, "https") {
href, _ = UrlJoin(url, href)
}
return href
}
}

return ""
}

func parseFeedForUrl(url string) (*gofeed.Feed, error) {
//metrics.CacheMiss.Inc()
fp.RSSTranslator = NewCustomTranslator()
Expand Down
44 changes: 7 additions & 37 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"rssnotes/metrics"

"rssnotes/rssworker"
"rssnotes/yarrworker"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -140,32 +140,15 @@ func createFeed(r *http.Request, secret *string) *GUIEntry {
Error: false,
}

discFeed, err := rssworker.DiscoverRssFeed(urlParam)
if err != nil {
discFeed, err := yarrworker.DiscoverRssFeed(urlParam)
if err != nil || discFeed.FeedLink == "" {
guientry.ErrorCode = http.StatusBadRequest
guientry.Error = true
guientry.ErrorMessage = "Could not find a feed URL in there..."
return &guientry
}

feedUrl := discFeed.FeedLink

// if !IsValidHttpUrl(urlParam) {
// log.Printf("[DEBUG] tried to create feed from invalid feed url '%q' skipping...", urlParam)
// guientry.ErrorCode = http.StatusBadRequest
// guientry.Error = true
// guientry.ErrorMessage = "Invalid URL provided (must be in absolute format and with https or https scheme)..."
// return &guientry
// }

// feedUrl := getFeedUrl(urlParam)
// if feedUrl == "" {
// guientry.ErrorCode = http.StatusBadRequest
// guientry.Error = true
// guientry.ErrorMessage = "Could not find a feed URL in there..."
// return &guientry
// }

sk := getPrivateKeyFromFeedUrl(feedUrl, *secret)
publicKey, err := nostr.GetPublicKey(sk)
if err != nil {
Expand Down Expand Up @@ -205,7 +188,7 @@ func createFeed(r *http.Request, secret *string) *GUIEntry {
guientry.NPubKey, _ = nip19.EncodePublicKey(publicKey)
guientry.BookmarkEntity.ImageURL = s.DefaultProfilePicUrl

faviconUrl, err := rssworker.FindFaviconURL(parsedFeed.Link, feedUrl)
faviconUrl, err := yarrworker.FindFaviconURL(parsedFeed.Link, feedUrl)
if err != nil {
log.Print("[ERROR] FindFavicon", err)
} else if faviconUrl != "" {
Expand Down Expand Up @@ -323,8 +306,8 @@ func importFeeds(opmlUrls []opml.Outline, secret *string) []*GUIEntry {
continue
}

discFeed, err := rssworker.DiscoverRssFeed(urlParam.XMLURL)
if err != nil {
discFeed, err := yarrworker.DiscoverRssFeed(urlParam.XMLURL)
if err != nil || discFeed.FeedLink == "" {
importedEntries = append(importedEntries, &GUIEntry{
BookmarkEntity: Entity{URL: urlParam.XMLURL},
ErrorMessage: "Could not find a feed URL in there...",
Expand All @@ -337,19 +320,6 @@ func importFeeds(opmlUrls []opml.Outline, secret *string) []*GUIEntry {
}
feedUrl := discFeed.FeedLink

// feedUrl := getFeedUrl(urlParam.XMLURL)
// if feedUrl == "" {
// importedEntries = append(importedEntries, &GUIEntry{
// BookmarkEntity: Entity{URL: urlParam.XMLURL},
// ErrorMessage: "Could not find a feed URL in there...",
// Error: true,
// ErrorCode: http.StatusBadRequest,
// })
// importProgressCh <- ImportProgressStruct{entryIndex: urlIndex, totalEntries: len(opmlUrls)}
// log.Printf("[DEBUG] Could not find a feed URL in %s", feedUrl)
// continue
// }

sk := getPrivateKeyFromFeedUrl(feedUrl, *secret)
publicKey, err := nostr.GetPublicKey(sk)
if err != nil {
Expand Down Expand Up @@ -416,7 +386,7 @@ func importFeeds(opmlUrls []opml.Outline, secret *string) []*GUIEntry {
}

localImageURL := s.DefaultProfilePicUrl
faviconUrl, err := rssworker.FindFaviconURL(parsedFeed.Link, feedUrl)
faviconUrl, err := yarrworker.FindFaviconURL(parsedFeed.Link, feedUrl)
if err != nil {
log.Print("[ERROR] FindFavicon", err)
} else if faviconUrl != "" {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func main() {
mux.HandleFunc(" /delete", handleDeleteFeed)
mux.Handle("GET /metrics", promhttp.Handler())

mux.HandleFunc(" /metricsDisplay", handleMetricsDisplay)
mux.HandleFunc("GET /metricsDisplay", handleMetricsDisplay)

mux.HandleFunc("GET /health", handleHealth)
mux.HandleFunc("GET /log", handleLog)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions rssparser/atom.go → yarrparser/atom.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Atom 1.0 parser
package rssparser
package yarrparser

import (
"encoding/xml"
"io"
"strings"

htmlutil "rssnotes/rsshtmlutil"
htmlutil "rssnotes/yarrhtmlutil"
)

type atomFeed struct {
Expand Down
2 changes: 1 addition & 1 deletion rssparser/date.go → yarrparser/date.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rssparser
package yarrparser

import "time"

Expand Down
4 changes: 2 additions & 2 deletions rssparser/feed.go → yarrparser/feed.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rssparser
package yarrparser

import (
"bytes"
Expand All @@ -11,7 +11,7 @@ import (
"strings"
"time"

htmlutil "rssnotes/rsshtmlutil"
htmlutil "rssnotes/yarrhtmlutil"

"golang.org/x/net/html/charset"
)
Expand Down
2 changes: 1 addition & 1 deletion rssparser/json.go → yarrparser/json.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// JSON 1.0 parser
package rssparser
package yarrparser

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion rssparser/media.go → yarrparser/media.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rssparser
package yarrparser

type media struct {
MediaGroups []mediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
Expand Down
2 changes: 1 addition & 1 deletion rssparser/models.go → yarrparser/models.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rssparser
package yarrparser

import "time"

Expand Down
2 changes: 1 addition & 1 deletion rssparser/rdf.go → yarrparser/rdf.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Parser for RSS versions:
// - 0.90
// - 1.0
package rssparser
package yarrparser

import (
"encoding/xml"
Expand Down
2 changes: 1 addition & 1 deletion rssparser/rss.go → yarrparser/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// - 0.91 netscape
// - 0.91 userland
// - 2.0
package rssparser
package yarrparser

import (
"encoding/xml"
Expand Down
2 changes: 1 addition & 1 deletion rssparser/util.go → yarrparser/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rssparser
package yarrparser

import (
"bufio"
Expand Down
4 changes: 2 additions & 2 deletions rssscraper/finder.go → yarrscraper/finder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package rssscraper
package yarrscraper

import (
"strings"

htmlutil "rssnotes/rsshtmlutil"
htmlutil "rssnotes/yarrhtmlutil"

"golang.org/x/net/html"
)
Expand Down
2 changes: 1 addition & 1 deletion rssworker/client.go → yarrworker/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rssworker
package yarrworker

import (
"net"
Expand Down
6 changes: 3 additions & 3 deletions rssworker/crawler.go → yarrworker/crawler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package rssworker
package yarrworker

import (
"fmt"
"io"
"net/http"
"net/url"
"rssnotes/rssscraper"
"rssnotes/yarrscraper"
)

var imageTypes = map[string]bool{
Expand All @@ -30,7 +30,7 @@ func FindFaviconURL(siteUrl, feedUrl string) (string, error) {
if res, err := client.get(siteUrl); err == nil {
defer res.Body.Close()
if body, err := io.ReadAll(res.Body); err == nil {
urls = append(urls, rssscraper.FindIcons(string(body), siteUrl)...)
urls = append(urls, yarrscraper.FindIcons(string(body), siteUrl)...)
if c := favicon(siteUrl); c != "" {
urls = append(urls, c)
}
Expand Down
12 changes: 6 additions & 6 deletions rssworker/discoverfeed.go → yarrworker/discoverfeed.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rssworker
package yarrworker

import (
"bytes"
Expand All @@ -8,8 +8,8 @@ import (
"mime"
"net/http"

"rssnotes/rssparser"
"rssnotes/rssscraper"
"rssnotes/yarrparser"
"rssnotes/yarrscraper"

"golang.org/x/net/html/charset"
)
Expand All @@ -20,7 +20,7 @@ type FeedSource struct {
}

type DiscoverResult struct {
Feed *rssparser.Feed
Feed *yarrparser.Feed
FeedLink string
Sources []FeedSource
}
Expand All @@ -44,7 +44,7 @@ func DiscoverRssFeed(candidateUrl string) (*DiscoverResult, error) {
}

// Try to feed into parser
feed, err := rssparser.ParseAndFix(bytes.NewReader(body), candidateUrl, cs)
feed, err := yarrparser.ParseAndFix(bytes.NewReader(body), candidateUrl, cs)
if err == nil {
result.Feed = feed
result.FeedLink = candidateUrl
Expand All @@ -61,7 +61,7 @@ func DiscoverRssFeed(candidateUrl string) (*DiscoverResult, error) {
}
}
sources := make([]FeedSource, 0)
for url, title := range rssscraper.FindFeeds(content, candidateUrl) {
for url, title := range yarrscraper.FindFeeds(content, candidateUrl) {
sources = append(sources, FeedSource{Title: title, Url: url})
}
switch {
Expand Down

0 comments on commit 61edd29

Please sign in to comment.