Created
February 14, 2023 00:49
-
-
Save imirzadeh/94bb998642db4dddef5d6b22c1194078 to your computer and use it in GitHub Desktop.
RSS Detection GO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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