mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
2 years ago
|
package getter
|
||
2 years ago
|
|
||
|
import (
|
||
2 years ago
|
"fmt"
|
||
2 years ago
|
"io"
|
||
|
"net/http"
|
||
2 years ago
|
"net/url"
|
||
2 years ago
|
|
||
|
"golang.org/x/net/html"
|
||
|
"golang.org/x/net/html/atom"
|
||
|
)
|
||
|
|
||
|
type HTMLMeta struct {
|
||
|
Title string `json:"title"`
|
||
|
Description string `json:"description"`
|
||
|
Image string `json:"image"`
|
||
|
}
|
||
|
|
||
2 years ago
|
func GetHTMLMeta(urlStr string) (*HTMLMeta, error) {
|
||
|
if _, err := url.Parse(urlStr); err != nil {
|
||
2 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
2 years ago
|
response, err := http.Get(urlStr)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer response.Body.Close()
|
||
|
|
||
2 years ago
|
mediatype, err := getMediatype(response)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if mediatype != "text/html" {
|
||
|
return nil, fmt.Errorf("Wrong website mediatype")
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
htmlMeta := extractHTMLMeta(response.Body)
|
||
2 years ago
|
return htmlMeta, nil
|
||
|
}
|
||
|
|
||
|
func extractHTMLMeta(resp io.Reader) *HTMLMeta {
|
||
|
tokenizer := html.NewTokenizer(resp)
|
||
|
htmlMeta := new(HTMLMeta)
|
||
|
|
||
|
for {
|
||
|
tokenType := tokenizer.Next()
|
||
|
if tokenType == html.ErrorToken {
|
||
|
break
|
||
|
} else if tokenType == html.StartTagToken || tokenType == html.SelfClosingTagToken {
|
||
|
token := tokenizer.Token()
|
||
|
if token.DataAtom == atom.Body {
|
||
|
break
|
||
|
}
|
||
|
|
||
|
if token.DataAtom == atom.Title {
|
||
|
tokenizer.Next()
|
||
|
token := tokenizer.Token()
|
||
|
htmlMeta.Title = token.Data
|
||
|
} else if token.DataAtom == atom.Meta {
|
||
|
description, ok := extractMetaProperty(token, "description")
|
||
|
if ok {
|
||
|
htmlMeta.Description = description
|
||
|
}
|
||
|
|
||
|
ogTitle, ok := extractMetaProperty(token, "og:title")
|
||
|
if ok {
|
||
|
htmlMeta.Title = ogTitle
|
||
|
}
|
||
|
|
||
|
ogDescription, ok := extractMetaProperty(token, "og:description")
|
||
|
if ok {
|
||
|
htmlMeta.Description = ogDescription
|
||
|
}
|
||
|
|
||
|
ogImage, ok := extractMetaProperty(token, "og:image")
|
||
|
if ok {
|
||
|
htmlMeta.Image = ogImage
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return htmlMeta
|
||
|
}
|
||
|
|
||
|
func extractMetaProperty(token html.Token, prop string) (content string, ok bool) {
|
||
|
content, ok = "", false
|
||
|
for _, attr := range token.Attr {
|
||
|
if attr.Key == "property" && attr.Val == prop {
|
||
|
ok = true
|
||
|
}
|
||
|
if attr.Key == "content" {
|
||
|
content = attr.Val
|
||
|
}
|
||
|
}
|
||
|
return content, ok
|
||
|
}
|