utils

package
v1.2.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2021 License: MIT Imports: 7 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayFilter

func ArrayFilter(s []string) []string

ArrayFilter to remove empty string from slice.

func BuildURL

func BuildURL(host string, path ...interface{}) string

BuildURL to build URL with its paths.

func BuildURLWithQuery

func BuildURLWithQuery(params map[string]interface{}, host string, path ...interface{}) string

BuildURLWithQuery to build URL with query params.

func Ellipsis

func Ellipsis(str string, l int) string

Ellipsis to substring long string.

func GetCurrentSeason

func GetCurrentSeason() string

GetCurrentSeason to get current season ("spring", "summer", "fall", "winter").

func GetDuration

func GetDuration(duration string) (seconds int)

GetDuration to get duration in seconds. Example: "3 hr. 30 min. 20 sec." => 12620.

func GetMapKey

func GetMapKey(m map[int]string, value string) (key int, found bool)

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

func GetPercent(top interface{}, bottom interface{}, digit ...int) float64

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

func GetSeasonName(m int) string

GetSeasonName to get season name ("spring", "summer", "fall", "winter").

func GetValueFromSplit

func GetValueFromSplit(str string, separator string, index int) string

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

func ImageURLCleaner(str string) string

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

func InArrayInt(array []int, value int) bool

InArrayInt to check if int value is in array int.

func InArrayStr

func InArrayStr(arr []string, v string) bool

InArrayStr to get if value is in array string.

func ParseTime

func ParseTime(layout, str string) time.Time

ParseTime is time parser wrapper of built-in library `time.ParseInLocation`.

func SecondToString

func SecondToString(seconds int) string

SecondToString to convert int seconds to string duration. Example: 12620 => 03:30:20.

func StrToDate

func StrToDate(str string) (y, m, d int)

StrToDate to convert MyAnimeList date string to splitted date (year, month, day).

func StrToFloat

func StrToFloat(strNum string) float64

StrToFloat to convert string number to float64 including comma removal (1,234.56 -> 1234.56).

func StrToNum

func StrToNum(strNum string) int

StrToNum to convert string number to integer including comma removal (1,234 -> 1234).

func StrToTime

func StrToTime(str string) (t time.Time, isValid bool)

StrToTime to convert MyAnimeList date string to time.Time type.

func Thousand

func Thousand(num int) string

Thousand to convert int to string with ',' thousand.

func URLCleaner

func URLCleaner(str string, URLType string, isNeeded ...bool) string

URLCleaner is wrapper for image and video url cleaner for easier call.

func UniqueInt

func UniqueInt(list []int) []int

UniqueInt to remove duplicate in slice.

func VideoURLCleaner

func VideoURLCleaner(str string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL