Skip to content

Instantly share code, notes, and snippets.

@imirzadeh
Created February 14, 2023 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imirzadeh/94bb998642db4dddef5d6b22c1194078 to your computer and use it in GitHub Desktop.
Save imirzadeh/94bb998642db4dddef5d6b22c1194078 to your computer and use it in GitHub Desktop.
RSS Detection GO
func DetectRSSLink(ctx context.Context, URL string, body string) []string {
doc, err := html.Parse(strings.NewReader(body))
var candidates []string
if err != nil {
return candidates
}
links := FindAll(doc, "link")
for _, link := range links {
if GetAttr(link, "rel") == "alternate" {
attrHref := GetAttr(link, "href")
attrType := GetAttr(link, "type")
if len(attrHref) > 0 && len(attrType) > 0 {
if strings.Contains(attrType, "feed") || strings.Contains(attrType, "rss") || strings.Contains(attrType, "atom") {
resURL, err := resolveURL(ctx, URL, attrHref)
if err == nil {
candidates = append(candidates, resURL)
}
}
}
}
}
aTags := FindAll(doc, "a")
for _, aTag := range aTags {
href := GetAttr(aTag, "href")
if len(href) > 0 && (strings.Contains(href, "xml") || strings.Contains(href, "feed") || strings.Contains(href, "rss")) || strings.Contains(href, "atom") {
resURL, err := resolveURL(ctx, URL, href)
if err == nil {
candidates = append(candidates, resURL)
}
}
}
if len(candidates) > 1 {
sort.Slice(candidates, func(i int, j int) bool {
return getScore(candidates[i], URL) < getScore(candidates[j], URL)
})
}
return candidates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment