Add Google search integration with CLI support
Introduce a Google search integration, including a Go package for performing searches with configurable parameters (e.g., language, region) and a CLI tool for executing search queries. Refactor archive CLI import ordering for consistency.
This commit is contained in:
96
sites/google/cmd/google/main.go
Normal file
96
sites/google/cmd/google/main.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/go-extractor/cmd/browser/pkg/browser"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/go-extractor/sites/google"
|
||||
)
|
||||
|
||||
type GoogleFlags []cli.Flag
|
||||
|
||||
var Flags = GoogleFlags{
|
||||
&cli.StringFlag{
|
||||
Name: "domain",
|
||||
Aliases: []string{"d"},
|
||||
Usage: "The base domain to use",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "language",
|
||||
Aliases: []string{"l"},
|
||||
Usage: "The language to use",
|
||||
},
|
||||
}
|
||||
|
||||
func (f GoogleFlags) ToConfig(_ context.Context, cmd *cli.Command) google.Config {
|
||||
c := google.DefaultConfig
|
||||
|
||||
if d := cmd.String("domain"); d != "" {
|
||||
c.BaseURL = d
|
||||
}
|
||||
|
||||
if l := cmd.String("language"); l != "" {
|
||||
c.Language = l
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func deferClose(cl io.Closer) {
|
||||
if cl != nil {
|
||||
_ = cl.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var flags []cli.Flag
|
||||
|
||||
flags = append(flags, browser.Flags...)
|
||||
flags = append(flags, Flags...)
|
||||
|
||||
cli := &cli.Command{
|
||||
Name: "google",
|
||||
Usage: "Search Google",
|
||||
Flags: flags,
|
||||
Action: func(ctx context.Context, cli *cli.Command) error {
|
||||
query := strings.Join(cli.Args().Slice(), " ")
|
||||
|
||||
if query == "" {
|
||||
return fmt.Errorf("usage: google <query>")
|
||||
}
|
||||
|
||||
b, err := browser.FromCommand(ctx, cli)
|
||||
|
||||
defer deferClose(b)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg := Flags.ToConfig(ctx, cli)
|
||||
|
||||
res, err := cfg.Search(ctx, b, query)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(res)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
err := cli.Run(context.Background(), os.Args)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user