~handlerug/handlebot

7a6c8b4a09a72ba2fb7880695c2a7211911c68fd — Umar Getagazov 15 days ago 7a89998
urlpreview: User agent overrides
5 files changed, 14 insertions(+), 5 deletions(-)

M config.example.json
M config.go
M main.go
M urlpreview/generic.go
M urlpreview/urlpreview.go
M config.example.json => config.example.json +2 -1
@@ 17,6 17,7 @@
			"wolframalpha": "",
			"mapquest": "",
			"openweathermap": ""
		}
		},
		"ua_overrides": {}
	}
}

M config.go => config.go +4 -3
@@ 24,9 24,10 @@ type IRCConfig struct {
}

type HandlebotCfg struct {
	CommandPrefix string  `json:"command_prefix"`
	Database      string  `json:"database"`
	APIKeys       APIKeys `json:"keys"`
	CommandPrefix string            `json:"command_prefix"`
	Database      string            `json:"database"`
	APIKeys       APIKeys           `json:"keys"`
	UAOverrides   map[string]string `json:"ua_overrides"`
}

type APIKeys struct {

M main.go => main.go +4 -1
@@ 82,7 82,10 @@ func main() {
			userAgent: UserAgent,
		},
	}
	previewer = &urlpreview.Previewer{HTTPClient: httpClient}
	previewer = &urlpreview.Previewer{
		HTTPClient:  httpClient,
		UAOverrides: cfg.UAOverrides,
	}
	if cfg.APIKeys.OpenWeatherMap != "" && cfg.APIKeys.MapQuest != "" {
		forecaster = &weather.Forecaster{
			Credentials: weather.APICredentials{

M urlpreview/generic.go => urlpreview/generic.go +3 -0
@@ 20,6 20,9 @@ import (

func (p *Previewer) generic(ctx context.Context, u *url.URL) (string, error) {
	req, _ := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
	if p.UAOverrides != nil && p.UAOverrides[u.Host] != "" {
		req.Header.Set("User-Agent", p.UAOverrides[u.Host])
	}
	resp, err := p.httpClient().Do(req)
	if err != nil {
		return "", err

M urlpreview/urlpreview.go => urlpreview/urlpreview.go +1 -0
@@ 14,6 14,7 @@ var geminiDefaultClient = &gemini.Client{}
type Previewer struct {
	HTTPClient   *http.Client
	GeminiClient *gemini.Client
	UAOverrides  map[string]string
}

func (p *Previewer) Preview(ctx context.Context, u *url.URL) (string, error) {