~handlerug/handlebot

ref: e2138e35984f582b50a481b58c332afae2841b1f handlebot/urlpreview/jisho.go -rw-r--r-- 706 bytes
e2138e35Umar Getagazov urlpreview: Parse Content-Disposition 5 months ago
                                                                                
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
package urlpreview

import (
	"context"
	"net/url"
	"strings"

	"git.sr.ht/~handlerug/handlebot/jisho"
	"git.sr.ht/~handlerug/handlebot/presentation"
)

func (p *Previewer) jisho(ctx context.Context, u *url.URL) (string, error) {
	if !strings.HasSuffix(u.Host, "jisho.org") ||
		!strings.HasPrefix(u.Path, "/search/") {
		return "", nil
	}

	segments := strings.Split(u.Path[1:], "/")
	if len(segments) < 2 || segments[1] == "" {
		return "", nil
	}

	lastSegment, err := url.PathUnescape(segments[1])
	if err != nil {
		return "", err
	}

	client := &jisho.Client{p.HttpClient}
	result, err := client.Query(ctx, lastSegment)
	if err != nil {
		return "", err
	}

	return presentation.Jisho(result), nil
}