Documentation ¶
Overview ¶
Package utils provides general helper functions for malscraper.
Some functions are used specifically for malscraper but still can be used by public and independently.
Index ¶
- func ArrayFilter(s []string) []string
- func BuildURL(host string, path ...interface{}) string
- func BuildURLWithQuery(params map[string]interface{}, host string, path ...interface{}) string
- func Ellipsis(str string, l int) string
- func GetCurrentSeason() string
- func GetDuration(duration string) (seconds int)
- func GetMapKey(m map[int]string, value string) (key int, found bool)
- func GetPercent(top interface{}, bottom interface{}, digit ...int) float64
- func GetSeasonName(m int) string
- func GetValueFromSplit(str string, separator string, index int) string
- func ImageURLCleaner(str string) string
- func InArrayInt(array []int, value int) bool
- func InArrayStr(arr []string, v string) bool
- func ParseTime(layout, str string) time.Time
- func SecondToString(seconds int) string
- func StrToDate(str string) (y, m, d int)
- func StrToFloat(strNum string) float64
- func StrToNum(strNum string) int
- func StrToTime(str string) (t time.Time, isValid bool)
- func Thousand(num int) string
- func URLCleaner(str string, URLType string, isNeeded ...bool) string
- func UniqueInt(list []int) []int
- func VideoURLCleaner(str string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArrayFilter ¶
ArrayFilter to remove empty string from slice.
func BuildURLWithQuery ¶
BuildURLWithQuery to build URL with query params.
func GetCurrentSeason ¶
func GetCurrentSeason() string
GetCurrentSeason to get current season ("spring", "summer", "fall", "winter").
func GetDuration ¶
GetDuration to get duration in seconds. Example: "3 hr. 30 min. 20 sec." => 12620.
func GetMapKey ¶
GetMapKey to get key from map.
Example ¶
m := map[int]string{ 1: "a", 2: "b", 3: "c", } fmt.Println(GetMapKey(m, "b")) fmt.Println(GetMapKey(m, "d"))
Output: 2 true 0 false
func GetPercent ¶
GetPercent to calculate percentage result with adjustable number of digit after comma. With default 2 digits after comma.
Example ¶
fmt.Println(GetPercent(10, 0)) fmt.Println(GetPercent(10, 3)) fmt.Println(GetPercent(10.0, 3.0)) fmt.Println(GetPercent(10.0, 3.0, 1))
Output: 0 333.33 333.33 333.3
func GetSeasonName ¶
GetSeasonName to get season name ("spring", "summer", "fall", "winter").
func GetValueFromSplit ¶
GetValueFromSplit to get value from splitted string. Will return empty string if out of index.
Example ¶
url := "https://myanimelist.net/anime/2/abc" fmt.Println(GetValueFromSplit(url, "/", 4)) fmt.Println(GetValueFromSplit(url, "/", 3)) fmt.Println(GetValueFromSplit(url, "/", 10)) // return empty string
Output: 2 anime
func ImageURLCleaner ¶
ImageURLCleaner to clean dirty image url. For examples:
https://cdn.myanimelist.net/r/80x120/images/manga/3/214566.jpg?s=48212bcd0396d503a01166149a29c67e => https://cdn.myanimelist.net/images/manga/3/214566.jpg https://cdn.myanimelist.net/r/76x120/images/userimages/6098374.jpg?s=4b8e4f091fbb3ecda6b9833efab5bd9b => https://cdn.myanimelist.net/images/userimages/6098374.jpg https://cdn.myanimelist.net/r/76x120/images/questionmark_50.gif?s=8e0400788aa6af2a2f569649493e2b0f => empty string
func InArrayInt ¶
InArrayInt to check if int value is in array int.
func InArrayStr ¶
InArrayStr to get if value is in array string.
func SecondToString ¶
SecondToString to convert int seconds to string duration. Example: 12620 => 03:30:20.
func StrToFloat ¶
StrToFloat to convert string number to float64 including comma removal (1,234.56 -> 1234.56).
func StrToNum ¶
StrToNum to convert string number to integer including comma removal (1,234 -> 1234).
func URLCleaner ¶
URLCleaner is wrapper for image and video url cleaner for easier call.
func VideoURLCleaner ¶
VideoURLCleaner to clean dirty video url. For examples:
https://www.youtube.com/embed/qig4KOK2R2g?enablejsapi=1&wmode=opaque&autoplay=1 => https://www.youtube.com/watch?v=qig4KOK2R2g https://www.youtube.com/embed/j2hiC9BmJlQ?enablejsapi=1&wmode=opaque&autoplay=1 => https://www.youtube.com/watch?v=j2hiC9BmJlQ
Types ¶
This section is empty.