You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

111 lines
2.4 KiB

package utils
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
// RemoveFile is removing file with delay
func RemoveFile(delaySecond int, paths ...string) error {
if delaySecond > 0 {
time.Sleep(time.Duration(delaySecond) * time.Second)
}
for _, path := range paths {
if path != "" {
err := os.Remove(path)
if err != nil {
return err
}
}
}
return nil
}
// CreateFolder create new folder and sub folder if not exist
func CreateFolder(folderPath ...string) error {
for _, folder := range folderPath {
newFolder := filepath.Join(".", folder)
err := os.MkdirAll(newFolder, os.ModePerm)
if err != nil {
return err
}
}
return nil
}
// PanicIfNeeded is panic if error is not nil
func PanicIfNeeded(err any, message ...string) {
if err != nil {
if fmt.Sprintf("%s", err) == "record not found" && len(message) > 0 {
panic(message[0])
} else {
panic(err)
}
}
}
func StrToFloat64(text string) float64 {
var result float64
if text != "" {
result, _ = strconv.ParseFloat(strings.TrimSpace(text), 64)
}
return result
}
type Metadata struct {
Title string
Description string
}
func GetMetaDataFromURL(url string) (meta Metadata) {
// Send an HTTP GET request to the website
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// Parse the HTML document
document, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Fatal(err)
}
document.Find("meta[name='description']").Each(func(index int, element *goquery.Selection) {
meta.Description, _ = element.Attr("content")
})
// find title
document.Find("title").Each(func(index int, element *goquery.Selection) {
meta.Title = element.Text()
})
// Print the meta description
fmt.Println("Meta data:", meta)
return meta
}
// ContainsMention is checking if message contains mention, then return only mention without @
func ContainsMention(message string) []string {
// Regular expression to find all phone numbers after the @ symbol
re := regexp.MustCompile(`@(\d+)`)
matches := re.FindAllStringSubmatch(message, -1)
var phoneNumbers []string
// Loop through the matches and extract the phone numbers
for _, match := range matches {
if len(match) > 1 {
phoneNumbers = append(phoneNumbers, match[1])
}
}
return phoneNumbers
}