upadted to 1.24.0; fixed stb and spiddped doubles in precene
This commit is contained in:
@@ -2,7 +2,6 @@ package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
@@ -15,7 +14,7 @@ import (
|
||||
|
||||
func setClient(url string) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
@@ -27,9 +26,12 @@ func setClient(url string) (*http.Response, error) {
|
||||
|
||||
func HTMLSourceFromURL(url string) (*html.Node, error) {
|
||||
resp, err := setClient(url)
|
||||
defer func(Body io.ReadCloser) {
|
||||
fmt.Printf("%v\n", Body == nil)
|
||||
if resp == nil {
|
||||
slog.Error("client return nil response", "err", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
err = Body.Close()
|
||||
if err != nil {
|
||||
slog.Error("closing response body", "err", err)
|
||||
|
||||
19
helper/sugar/console.go
Normal file
19
helper/sugar/console.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package sugar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/logrusorgru/aurora/v4"
|
||||
)
|
||||
|
||||
func LogSuccess(msg string) {
|
||||
fmt.Printf("%s: %s\n", aurora.Green("SUCCESS"), aurora.Yellow(msg))
|
||||
}
|
||||
|
||||
func LogError(msg string) {
|
||||
fmt.Printf("%s: %s\n", aurora.Red("ERROR"), aurora.White(msg))
|
||||
}
|
||||
|
||||
func LogWarning(msg string) {
|
||||
fmt.Printf("%s: %s\n", aurora.Magenta("WARNING"), aurora.White(msg))
|
||||
}
|
||||
11
helper/sugar/env.go
Normal file
11
helper/sugar/env.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package sugar
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"git.amok.space/yevhen/resource-scraper/types/constant"
|
||||
)
|
||||
|
||||
func IsDev() bool {
|
||||
return viper.GetString(constant.FlagEnv) == constant.DefaultEnvDev
|
||||
}
|
||||
63
helper/sugar/fs.go
Normal file
63
helper/sugar/fs.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package sugar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func EnsureDir(dirPath ...string) (string, error) {
|
||||
outPath := filepath.Join(dirPath...)
|
||||
if len(outPath) < 3 {
|
||||
return "", fmt.Errorf("the path is short to be a folder: %s", outPath)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(outPath); os.IsNotExist(err) {
|
||||
var dirMod uint64
|
||||
if dirMod, err = strconv.ParseUint("0775", 8, 32); err == nil {
|
||||
err = os.Mkdir(outPath, os.FileMode(dirMod))
|
||||
}
|
||||
|
||||
if err != nil && !os.IsExist(err) {
|
||||
slog.Error("error creating tmp dir", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
return outPath, nil
|
||||
}
|
||||
|
||||
func WriteDataToTmpFile(content, fp string) (int, string, error) {
|
||||
dir, err := EnsureDir(os.TempDir(), "mdb")
|
||||
if err != nil {
|
||||
slog.Error("EnsureDir", "err", err.Error())
|
||||
return 0, dir, err
|
||||
}
|
||||
|
||||
fp = filepath.Join(dir, fp)
|
||||
|
||||
f, err := os.Create(fp)
|
||||
if err != nil {
|
||||
slog.Error("CreateFile", "e", err)
|
||||
return 0, fp, err
|
||||
}
|
||||
|
||||
filesize, err := f.WriteString(content)
|
||||
if err != nil {
|
||||
slog.Error("WriteString", "e", err)
|
||||
return 0, fp, err
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
slog.Error("CloseFile", "e", err)
|
||||
return 0, fp, err
|
||||
}
|
||||
|
||||
return filesize, fp, nil
|
||||
}
|
||||
|
||||
/*
|
||||
REMOVED_FROM_MMT_DUE_TO_REDOING_OF_THE_EVENT_LOG
|
||||
*/
|
||||
8
helper/sugar/string.go
Normal file
8
helper/sugar/string.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package sugar
|
||||
|
||||
import "regexp"
|
||||
|
||||
// SqueezeLine remove extra spaces and line breaks from a string
|
||||
func SqueezeLine(line string) string {
|
||||
return regexp.MustCompile(`(?m)[\n\r\t\s]+`).ReplaceAllString(line, " ")
|
||||
}
|
||||
28
helper/thither/thither.go
Normal file
28
helper/thither/thither.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package thither
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func FieldValueToIntSlice[T interface{}](object []T, key string) []int {
|
||||
fields := make([]int, len(object))
|
||||
|
||||
for i, el := range object {
|
||||
immutable := reflect.ValueOf(el)
|
||||
fields[i] = immutable.FieldByName(key).Interface().(int)
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
func FieldValueToStrSlice[T interface{}](object []T, key string) []string {
|
||||
fields := make([]string, len(object))
|
||||
|
||||
for i, el := range object {
|
||||
immutable := reflect.ValueOf(el)
|
||||
fields[i] = strconv.Itoa(immutable.FieldByName(key).Interface().(int))
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
Reference in New Issue
Block a user