(resp io.Reader)
| 40 | } |
| 41 | |
| 42 | func extractHTMLMeta(resp io.Reader) *HTMLMeta { |
| 43 | tokenizer := html.NewTokenizer(resp) |
| 44 | htmlMeta := new(HTMLMeta) |
| 45 | |
| 46 | for { |
| 47 | tokenType := tokenizer.Next() |
| 48 | if tokenType == html.ErrorToken { |
| 49 | break |
| 50 | } else if tokenType == html.StartTagToken || tokenType == html.SelfClosingTagToken { |
| 51 | token := tokenizer.Token() |
| 52 | if token.DataAtom == atom.Body { |
| 53 | break |
| 54 | } |
| 55 | |
| 56 | if token.DataAtom == atom.Title { |
| 57 | tokenizer.Next() |
| 58 | token := tokenizer.Token() |
| 59 | htmlMeta.Title = token.Data |
| 60 | } else if token.DataAtom == atom.Meta { |
| 61 | description, ok := extractMetaProperty(token, "description") |
| 62 | if ok { |
| 63 | htmlMeta.Description = description |
| 64 | } |
| 65 | |
| 66 | ogTitle, ok := extractMetaProperty(token, "og:title") |
| 67 | if ok { |
| 68 | htmlMeta.Title = ogTitle |
| 69 | } |
| 70 | |
| 71 | ogDescription, ok := extractMetaProperty(token, "og:description") |
| 72 | if ok { |
| 73 | htmlMeta.Description = ogDescription |
| 74 | } |
| 75 | |
| 76 | ogImage, ok := extractMetaProperty(token, "og:image") |
| 77 | if ok { |
| 78 | htmlMeta.Image = ogImage |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return htmlMeta |
| 85 | } |
| 86 | |
| 87 | func extractMetaProperty(token html.Token, prop string) (content string, ok bool) { |
| 88 | content, ok = "", false |
no test coverage detected