Skip to content

Commit

Permalink
feature added to select from list
Browse files Browse the repository at this point in the history
  • Loading branch information
SIddharth SIngh committed May 10, 2021
1 parent c5d24f5 commit 210851c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 21 deletions.
94 changes: 77 additions & 17 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -13,35 +15,71 @@ import (
"github.com/spf13/cobra"
)

var listSearch = false
var Reset = "\033[0m"
var Red = "\033[31m"
var Green = "\033[32m"
var Yellow = "\033[33m"
var Blue = "\033[34m"
var Purple = "\033[35m"
var Cyan = "\033[36m"
var Gray = "\033[37m"
var White = "\033[97m"

var searchCmd = &cobra.Command{
Use: "search",
Short: "The command to search the web and the mysteries beyond",
Long: `This command will search the web and put the HTML files in a path of your choice.`,
Run: func(cmd *cobra.Command, args []string) {
opts := &googlesearch.SearchOptions{
Limit: config.Search,
}
var wg sync.WaitGroup
results, err := googlesearch.Search(cmd.Context(), strings.Join(args[:], " "), *opts)
if err != nil {
fmt.Println("Error searching google")
}
for _, s := range results {
if listSearch {
opts := &googlesearch.SearchOptions{
Limit: 3,
}
results, err := googlesearch.Search(cmd.Context(), strings.Join(args[:], " "), *opts)
if err != nil {
fmt.Println("Error searching google")
log.Println(err)
}
for _, s := range results {
fmt.Println(White + strconv.Itoa(s.Rank) + Reset + " " + Green + s.Title + Reset + "\n" + Red + s.Description + Reset + "\n")
}
fmt.Println("Enter the rank of article you want to download: ")
var option int
fmt.Scanln(&option)
link := results[option-1].URL
var wg sync.WaitGroup
wg.Add(1)
getSite(s, &wg)
getListSite(link, &wg)
wg.Wait()
fmt.Println("DONE!")
} else {
opts := &googlesearch.SearchOptions{
Limit: config.Search,
}
var wg sync.WaitGroup
results, err := googlesearch.Search(cmd.Context(), strings.Join(args[:], " "), *opts)
if err != nil {
fmt.Println("Error searching google")
}
for _, s := range results {
wg.Add(1)
getSite(s, &wg)
}
wg.Wait()
loader := termloader.New(termloader.CharsetConfigs["default"])
loader.Text = "Loading ..."
loader.Start()
time.Sleep(time.Duration(config.Search) * 4 * time.Second)
loader.Stop()
fmt.Println("DONE!")
}
wg.Wait()
loader := termloader.New(termloader.CharsetConfigs["default"])
loader.Text = "Loading ..."
loader.Start()
time.Sleep(time.Duration(config.Search) * 4 * time.Second)
loader.Stop()
fmt.Println("DONE!")
},
}

func init() {
rootCmd.AddCommand(searchCmd)
flags := searchCmd.Flags()
flags.BoolVar(&listSearch, "list", true, "Tells list form of search")
}

func getSite(s googlesearch.Result, wg *sync.WaitGroup) {
Expand All @@ -65,3 +103,25 @@ func getSite(s googlesearch.Result, wg *sync.WaitGroup) {
fmt.Println(err)
}
}

func getListSite(url string, wg *sync.WaitGroup) {
defer wg.Done()
resp, err := soup.Get(url)
if err != nil {
fmt.Println("Cant fetch the site")
}
s1 := url
if last := len(s1) - 1; last >= 0 && s1[last] == '/' {
s1 = s1[:last]
}
split := strings.Split(s1, "/")
s1 = split[len(split)-1]
file, err := os.Create(config.KumoPath + "/" + s1 + ".html")
if err != nil {
fmt.Println("Cant create file at path")
}
_, error := file.WriteString(resp)
if error != nil {
fmt.Println(err)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/44t4nk1/kumo
go 1.16

require (
github.com/anaskhan96/soup v1.2.4 // indirect
github.com/bharath-srinivas/termloader v0.2.0 // indirect
github.com/anaskhan96/soup v1.2.4
github.com/bharath-srinivas/termloader v0.2.0
github.com/mitchellh/go-homedir v1.1.0
github.com/rocketlaunchr/google-search v1.1.2
github.com/spf13/cobra v1.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down Expand Up @@ -263,7 +263,6 @@ golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down Expand Up @@ -350,6 +349,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down

0 comments on commit 210851c

Please sign in to comment.