1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package main
import (
"fmt"
"net/http"
"os"
"golang.org/x/net/html"
)
func fetchOGP(url string) (siteName string, title string, err error) {
resp, err := http.Get(url)
if err != nil {
return "", "", fmt.Errorf("failed to fetch OGP URL: %w", err)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
return "", "", fmt.Errorf("failed to parse HTML: %w", err)
}
var findOGP func(*html.Node)
findOGP = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "meta" {
var property, content string
for _, attr := range n.Attr {
if attr.Key == "property" {
property = attr.Val
} else if attr.Key == "content" {
content = attr.Val
}
}
switch property {
case "og:site_name":
siteName = content
case "og:title":
title = content
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
findOGP(c)
}
}
findOGP(doc)
if siteName == "" && title == "" {
return "", "",
fmt.Errorf("OGP data none")
}
return siteName, title, nil
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go https://soulminingrig.com/")
return
}
url := os.Args[1]
siteName, title, err := fetchOGP(url)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Site Name: %s\n", siteName)
fmt.Printf("Title: %s\n", title)
}
|