add google search (#242)

* add google search

* fix git ignore

* Charge 1c for search queries
This commit is contained in:
Asim Aslam
2021-10-25 12:16:41 +01:00
committed by GitHub
parent 30673f9837
commit 0bfe1e3c19
13 changed files with 656 additions and 0 deletions

2
google/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
google

3
google/Dockerfile Normal file
View File

@@ -0,0 +1,3 @@
FROM alpine
ADD google /google
ENTRYPOINT [ "/google" ]

28
google/Makefile Normal file
View File

@@ -0,0 +1,28 @@
GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: api
api:
protoc --openapi_out=. --proto_path=. proto/google.proto
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/google.proto
.PHONY: build
build:
go build -o google *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t google:latest

6
google/README.md Normal file
View File

@@ -0,0 +1,6 @@
Google search service
# Google Service
Search for anything via Google. That's it.

47
google/examples.json Normal file
View File

@@ -0,0 +1,47 @@
{
"search": [{
"title": "Search for videos",
"run_check": false,
"description": "Search for videos on google",
"request": {
"query": "how to make donuts"
},
"response": {
"results": [
{
"id": "g0h-szLkVy8J",
"kind": "result",
"title": "How to Make Homemade Glazed Doughnuts - Sally's Baking Addiction",
"snippet": "May 2, 2016 ... Ingredients · 1 cup (240ml) whole milk, warmed to about 110°F (43°C)* · 1 Tablespoon active dry yeast* · 1/3 cup (65g) granulated sugar · 2 large ...",
"url": "https://sallysbakingaddiction.com/how-to-make-homemade-glazed-doughnuts/",
"display_url": "sallysbakingaddiction.com"
},
{
"id": "7Rg9Zme3k3EJ",
"kind": "result",
"title": "How to Make Donuts at Home - Homemade Doughnuts Recipe",
"snippet": "Apr 12, 2021 ... In a medium bowl, whisk together flour and salt. In a large bowl, whisk together remaining sugar, butter, eggs, and vanilla with a wooden spoon.",
"url": "https://www.delish.com/cooking/recipe-ideas/a24788319/how-to-make-donuts-at-home/",
"display_url": "www.delish.com"
},
{
"id": "y9u1t370RBoJ",
"kind": "result",
"title": "How to Make Homemade Donuts in 15 Minutes - Cooking Classy",
"snippet": "May 3, 2020 ... Ingredients · 1 1/4 cups (176g) all-purpose flour (scoop and level to measure) · 2 tsp baking powder · 1/4 tsp salt · 1/2 cup (120 ml) buttermilk* ...",
"url": "https://www.cookingclassy.com/15-minute-donuts-from-scratch/",
"display_url": "www.cookingclassy.com"
},
{
"id": "u30VwVPlat8J",
"kind": "result",
"title": "How to Make Homemade Donuts | The Recipe Critic",
"snippet": "Oct 24, 2020 ... How to Make Homemade Donuts · Heat up the cream, milk and sugar and allow it to cool down a bit before adding the yeast to activate it. · Once the ...",
"url": "https://therecipecritic.com/homemade-donuts/",
"display_url": "therecipecritic.com"
}
]
}
}]
}

3
google/generate.go Normal file
View File

@@ -0,0 +1,3 @@
package main
//go:generate make proto

53
google/handler/google.go Normal file
View File

@@ -0,0 +1,53 @@
package handler
import (
"context"
"strings"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/google/proto"
"google.golang.org/api/customsearch/v1"
"google.golang.org/api/option"
)
type Google struct {
Client *customsearch.Service
CxId string
}
func New(apiKey, cxId string) *Google {
ctx := context.TODO()
cs, _ := customsearch.NewService(ctx, option.WithAPIKey(apiKey))
return &Google{
Client: cs,
CxId: cxId,
}
}
func (g *Google) Search(ctx context.Context, req *pb.SearchRequest, rsp *pb.SearchResponse) error {
if len(req.Query) == 0 {
return errors.BadRequest("google.search", "missing query")
}
resp, err := g.Client.Cse.List().Cx(g.CxId).Q(req.Query).Num(10).Do()
if err != nil {
logger.Errorf("failed to search google for %v: %v", req.Query, err)
return errors.InternalServerError("google.search", "Failed to search for "+req.Query)
}
for _, item := range resp.Items {
kind := strings.Split(item.Kind, "#")[1]
rsp.Results = append(rsp.Results, &pb.SearchResult{
Id: item.CacheId,
Kind: kind,
Title: item.Title,
Url: item.Link,
DisplayUrl: item.DisplayLink,
Snippet: item.Snippet,
})
}
return nil
}

44
google/main.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/services/google/handler"
pb "github.com/micro/services/google/proto"
)
func main() {
// Create service
srv := service.New(
service.Name("google"),
service.Version("latest"),
)
// Setup google maps
c, err := config.Get("google.apikey")
if err != nil {
logger.Fatalf("Error loading config: %v", err)
}
apiKey := c.String("")
if len(apiKey) == 0 {
logger.Fatalf("Missing required config: google.apikey")
}
// Setup google maps
c, err = config.Get("google.cx_id")
if err != nil {
logger.Fatalf("Error loading config: %v", err)
}
cxId := c.String("")
if len(cxId) == 0 {
logger.Fatalf("Missing required config: google.cxId")
}
// Register handler
pb.RegisterGoogleHandler(srv.Server(), handler.New(apiKey, cxId))
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

1
google/micro.mu Normal file
View File

@@ -0,0 +1 @@
service google

332
google/proto/google.pb.go Normal file
View File

@@ -0,0 +1,332 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.27.1
// protoc v3.15.6
// source: proto/google.proto
package google
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type SearchResult struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// id of the result
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
// kind of result; "search"
Kind string `protobuf:"bytes,2,opt,name=kind,proto3" json:"kind,omitempty"`
// title of the result
Title string `protobuf:"bytes,3,opt,name=title,proto3" json:"title,omitempty"`
// the result snippet
Snippet string `protobuf:"bytes,4,opt,name=snippet,proto3" json:"snippet,omitempty"`
// the full url for the result
Url string `protobuf:"bytes,5,opt,name=url,proto3" json:"url,omitempty"`
// abridged version of this search results URL, e.g. www.exampe.com
DisplayUrl string `protobuf:"bytes,6,opt,name=display_url,json=displayUrl,proto3" json:"display_url,omitempty"`
}
func (x *SearchResult) Reset() {
*x = SearchResult{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_google_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SearchResult) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SearchResult) ProtoMessage() {}
func (x *SearchResult) ProtoReflect() protoreflect.Message {
mi := &file_proto_google_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SearchResult.ProtoReflect.Descriptor instead.
func (*SearchResult) Descriptor() ([]byte, []int) {
return file_proto_google_proto_rawDescGZIP(), []int{0}
}
func (x *SearchResult) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *SearchResult) GetKind() string {
if x != nil {
return x.Kind
}
return ""
}
func (x *SearchResult) GetTitle() string {
if x != nil {
return x.Title
}
return ""
}
func (x *SearchResult) GetSnippet() string {
if x != nil {
return x.Snippet
}
return ""
}
func (x *SearchResult) GetUrl() string {
if x != nil {
return x.Url
}
return ""
}
func (x *SearchResult) GetDisplayUrl() string {
if x != nil {
return x.DisplayUrl
}
return ""
}
// Search for videos on Google
type SearchRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Query to search for
Query string `protobuf:"bytes,1,opt,name=query,proto3" json:"query,omitempty"`
}
func (x *SearchRequest) Reset() {
*x = SearchRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_google_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SearchRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SearchRequest) ProtoMessage() {}
func (x *SearchRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_google_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SearchRequest.ProtoReflect.Descriptor instead.
func (*SearchRequest) Descriptor() ([]byte, []int) {
return file_proto_google_proto_rawDescGZIP(), []int{1}
}
func (x *SearchRequest) GetQuery() string {
if x != nil {
return x.Query
}
return ""
}
type SearchResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// List of results for the query
Results []*SearchResult `protobuf:"bytes,1,rep,name=results,proto3" json:"results,omitempty"`
}
func (x *SearchResponse) Reset() {
*x = SearchResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_google_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SearchResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SearchResponse) ProtoMessage() {}
func (x *SearchResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_google_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SearchResponse.ProtoReflect.Descriptor instead.
func (*SearchResponse) Descriptor() ([]byte, []int) {
return file_proto_google_proto_rawDescGZIP(), []int{2}
}
func (x *SearchResponse) GetResults() []*SearchResult {
if x != nil {
return x.Results
}
return nil
}
var File_proto_google_proto protoreflect.FileDescriptor
var file_proto_google_proto_rawDesc = []byte{
0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x22, 0x95, 0x01, 0x0a,
0x0c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x0e, 0x0a,
0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a,
0x04, 0x6b, 0x69, 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6b, 0x69, 0x6e,
0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09,
0x52, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x6e, 0x69, 0x70, 0x70,
0x65, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x6e, 0x69, 0x70, 0x70, 0x65,
0x74, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03,
0x75, 0x72, 0x6c, 0x12, 0x1f, 0x0a, 0x0b, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x5f, 0x75,
0x72, 0x6c, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61,
0x79, 0x55, 0x72, 0x6c, 0x22, 0x25, 0x0a, 0x0d, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79, 0x22, 0x40, 0x0a, 0x0e, 0x53,
0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2e, 0x0a,
0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x14,
0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65,
0x73, 0x75, 0x6c, 0x74, 0x52, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x32, 0x43, 0x0a,
0x06, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x12, 0x39, 0x0a, 0x06, 0x53, 0x65, 0x61, 0x72, 0x63,
0x68, 0x12, 0x15, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63,
0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
0x65, 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x22, 0x00, 0x42, 0x10, 0x5a, 0x0e, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x67, 0x6f,
0x6f, 0x67, 0x6c, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_google_proto_rawDescOnce sync.Once
file_proto_google_proto_rawDescData = file_proto_google_proto_rawDesc
)
func file_proto_google_proto_rawDescGZIP() []byte {
file_proto_google_proto_rawDescOnce.Do(func() {
file_proto_google_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_google_proto_rawDescData)
})
return file_proto_google_proto_rawDescData
}
var file_proto_google_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_proto_google_proto_goTypes = []interface{}{
(*SearchResult)(nil), // 0: google.SearchResult
(*SearchRequest)(nil), // 1: google.SearchRequest
(*SearchResponse)(nil), // 2: google.SearchResponse
}
var file_proto_google_proto_depIdxs = []int32{
0, // 0: google.SearchResponse.results:type_name -> google.SearchResult
1, // 1: google.Google.Search:input_type -> google.SearchRequest
2, // 2: google.Google.Search:output_type -> google.SearchResponse
2, // [2:3] is the sub-list for method output_type
1, // [1:2] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_proto_google_proto_init() }
func file_proto_google_proto_init() {
if File_proto_google_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_google_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchResult); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_google_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_google_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SearchResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_google_proto_rawDesc,
NumEnums: 0,
NumMessages: 3,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_google_proto_goTypes,
DependencyIndexes: file_proto_google_proto_depIdxs,
MessageInfos: file_proto_google_proto_msgTypes,
}.Build()
File_proto_google_proto = out.File
file_proto_google_proto_rawDesc = nil
file_proto_google_proto_goTypes = nil
file_proto_google_proto_depIdxs = nil
}

View File

@@ -0,0 +1,93 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/google.proto
package google
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Google service
func NewGoogleEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Google service
type GoogleService interface {
Search(ctx context.Context, in *SearchRequest, opts ...client.CallOption) (*SearchResponse, error)
}
type googleService struct {
c client.Client
name string
}
func NewGoogleService(name string, c client.Client) GoogleService {
return &googleService{
c: c,
name: name,
}
}
func (c *googleService) Search(ctx context.Context, in *SearchRequest, opts ...client.CallOption) (*SearchResponse, error) {
req := c.c.NewRequest(c.name, "Google.Search", in)
out := new(SearchResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Google service
type GoogleHandler interface {
Search(context.Context, *SearchRequest, *SearchResponse) error
}
func RegisterGoogleHandler(s server.Server, hdlr GoogleHandler, opts ...server.HandlerOption) error {
type google interface {
Search(ctx context.Context, in *SearchRequest, out *SearchResponse) error
}
type Google struct {
google
}
h := &googleHandler{hdlr}
return s.Handle(s.NewHandler(&Google{h}, opts...))
}
type googleHandler struct {
GoogleHandler
}
func (h *googleHandler) Search(ctx context.Context, in *SearchRequest, out *SearchResponse) error {
return h.GoogleHandler.Search(ctx, in, out)
}

35
google/proto/google.proto Normal file
View File

@@ -0,0 +1,35 @@
syntax = "proto3";
package google;
option go_package = "./proto;google";
service Google {
rpc Search(SearchRequest) returns (SearchResponse) {}
}
message SearchResult {
// id of the result
string id = 1;
// kind of result; "search"
string kind = 2;
// title of the result
string title = 3;
// the result snippet
string snippet = 4;
// the full url for the result
string url = 5;
// abridged version of this search results URL, e.g. www.exampe.com
string display_url = 6;
}
// Search for videos on Google
message SearchRequest {
// Query to search for
string query = 1;
}
message SearchResponse {
// List of results for the query
repeated SearchResult results = 1;
}

9
google/publicapi.json Normal file
View File

@@ -0,0 +1,9 @@
{
"name": "google",
"icon": "🔍",
"category": "search",
"display_name": "Google",
"pricing": {
"Google.Search": 10000
}
}