tipitypy/cmd/scrapper/main.go

60 lines
1.2 KiB
Go
Raw Permalink Normal View History

2025-02-15 18:06:22 +01:00
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"tipitypy/db"
)
func AddToDbNWords(n int64) (int, error) {
url := fmt.Sprintf("https://random-word-api.herokuapp.com/word?number=%d", n)
resp, err := http.Get(url)
if err != nil {
return 0, fmt.Errorf("http.Get: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("io.ReadAll: %w", err)
}
var words []string
err = json.Unmarshal(body, &words)
if err != nil {
return 0, fmt.Errorf("json.Unmarshal: %w", err)
}
dbc := db.Connect()
successes := 0
for _, wordValue := range words {
word := &db.Word{Value: wordValue}
if err := dbc.Create(word).Error; err != nil {
continue
}
successes++
}
return successes, nil
}
func main() {
if len(os.Args) > 1 {
if val, err := strconv.ParseInt(os.Args[1], 10, 64); err == nil {
res, err := AddToDbNWords(val)
if err != nil {
fmt.Printf("ERROR: %v", err)
os.Exit(1)
}
fmt.Printf("Successfully added %d words to database!\n", res)
os.Exit(0)
}
}
res, err := AddToDbNWords(10)
if err != nil {
fmt.Printf("ERROR: %v", err)
os.Exit(1)
}
fmt.Printf("Successfully added %d words to database!\n", res)
}