summaryrefslogtreecommitdiff
path: root/main.go
blob: e5cfd93de4dfa64cc02ff15451bd11000e75fd79 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main

import (
	"crypto/tls"
	"flag"
	"fmt"
	"net/http"
	"os"
	"time"

	"ght/chardet"

	"golang.org/x/net/html"
)

func findTitleTag(n *html.Node) string {
	if n.Type == html.ElementNode && n.Data == "title" {
		if n.FirstChild != nil {
			return n.FirstChild.Data
		}
	}
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		if result := findTitleTag(c); result != "" {
			return result
		}
	}
	return ""
}

func fetchAndParse(client *http.Client, url string, useRange bool) (string, error) {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return "", fmt.Errorf("failed to create request: %w", err)
	}
	if useRange {
		req.Header.Set("Range", "bytes=0-4096")
	}

	resp, err := client.Do(req)
	if err != nil {
		return "", fmt.Errorf("failed to fetch URL: %w", err)
	}
	defer resp.Body.Close()

	// encoding and decode
	body, err := chardet.DetectAndDecode(resp.Body)
	if err != nil {
		return "", fmt.Errorf("failed to decode response body: %w", err)
	}

	doc, err := html.Parse(body)
	if err != nil {
		return "", fmt.Errorf("failed to parse HTML: %w", err)
	}

	return findTitleTag(doc), nil
}

func fetchTitle(url string) (string, error) {
	client := &http.Client{
		Timeout: 5 * time.Second,
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		},
	}

	// range limit : get request
	title, err := fetchAndParse(client, url, true)
	if err != nil {
		return "", err
	}
	if title != "" {
		return title, nil
	}

	// no range limit : get request
	title, err = fetchAndParse(client, url, false)
	if err != nil {
		return "", err
	}
	if title == "" {
		return "", fmt.Errorf("no title found: %s", url)
	}

	return title, nil
}

func main() {
	markdown := flag.Bool("m", false, "Output the URL in Markdown format")

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options] <URL>\n", os.Args[0])
		fmt.Fprintln(os.Stderr, "Options:")
		flag.PrintDefaults()
	}

	flag.Parse()

	if flag.NArg() < 1 {
		flag.Usage()
		os.Exit(1)
	}

	url := flag.Arg(0)
	title, err := fetchTitle(url)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(2)
	}

	// output md
	if *markdown {
		fmt.Printf("[%s](%s)\n", title, url)
	} else {
		fmt.Printf("%s\n", title)
	}
}