Skip to content

Commit

Permalink
added google
Browse files Browse the repository at this point in the history
  • Loading branch information
Xatta-Trone committed Jun 30, 2023
1 parent 658d8b1 commit 9088380
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=8081
SCRAPPER_API="****"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
# vendor/

# Go workspace file
go.work
go.work
.env
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"cSpell.words": [
"Puerkito"
"godotenv",
"Puerkito",
"splitted"
]
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/gobwas/ws v1.2.1 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down
93 changes: 90 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"errors"
"fmt"
"log"
"math"
"net/http"
"os"
"runtime"
"strings"

Expand All @@ -13,16 +15,87 @@ import (
"github.com/geziyor/geziyor/client"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/xatta-trone/thesaurus-scrapper/scrapper"
)

func main() {
const PORT = ":8081"
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}

p := os.Getenv("PORT")

if p != "" {
p = "8081"
}

PORT := fmt.Sprintf(":%s", p)

r := gin.Default()
r.Use(cors.Default())

r.GET("/",func(c *gin.Context) {
type KeyManager struct {
Idx int
Min int
Max int
}

r.GET("/", func(c *gin.Context) {
key := ""

API_KEY := "key1,key2,key3,key4"

splittedKeys := strings.Split(API_KEY, ",")
keysLength := len(splittedKeys)

if keysLength == 1 {
key = API_KEY
}

if keysLength > 1 {

keysRange := []KeyManager{}

keysPerDay := int(math.Ceil(30.0 / float64(keysLength)))

fmt.Println(keysPerDay)

for i := 0; i < keysLength; i++ {
max := (i + 1) * keysPerDay
// set the max dey
if i == keysLength-1 {
max = 32
}

keyRange := KeyManager{
Idx: i,
Min: i*keysPerDay + 1,
Max: max,
}

keysRange = append(keysRange, keyRange)

}

fmt.Println(keysRange)

// now based on current date set the key
// _, _, day := time.Now().UTC().Date()
day := 12

for _, keyRange := range keysRange {
if day >= keyRange.Min && day <= keyRange.Max {
key = splittedKeys[keyRange.Idx]
}

}

}

c.JSON(http.StatusOK, gin.H{
"message": "/ping",
"message": key,
})
})

Expand Down Expand Up @@ -55,6 +128,20 @@ func main() {
c.JSON(200, gin.H{"data": data})
})

r.GET("/g/:word", func(c *gin.Context) {

data, status := scrapper.GetGoogleResult(c.Param("word"))

fmt.Println(data, status)

if status != 200 {
c.JSON(status, gin.H{"error": "could not find data"})
return
}

c.JSON(200, gin.H{"data": data})
})

r.GET("/mw/:word", func(c *gin.Context) {

data, err := GetMWData(c.Param("word"))
Expand Down
Loading

0 comments on commit 9088380

Please sign in to comment.