summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorhaturatu <taro@eyes4you.org>2024-12-03 00:25:50 +0900
committerhaturatu <taro@eyes4you.org>2024-12-03 00:25:50 +0900
commitf98589664b8e99d803f06c54094e632793158f08 (patch)
tree2f024aed343436fea6189a464084daeba626a904 /main.go
Testin
Diffstat (limited to 'main.go')
-rw-r--r--main.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..8031dc1
--- /dev/null
+++ b/main.go
@@ -0,0 +1,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)
+}