Documentation
¶
Index ¶
- Constants
- func AppendUrlQueryString(url string, qs string) string
- func AppendUrlQueryStringDelimiter(url string) string
- func AsNetworkError(err error) bool
- func Assign(dst any, src any, excludeFieldIndexes []int)
- func AssignMap[T1 comparable, T2 any](args ...map[T1]T2) map[T1]T2
- func BytesHasAnyStringPrefix(data []byte, prefixes ...string) bool
- func BytesSize(size float64) string
- func BytesSizeAround(size float64) string
- func Capitalize(str string) string
- func Clean(s string) string
- func ContainsI(str string, substr string) bool
- func CopyFile(srcpath, dstpath string) (err error)
- func CopyMap[T1 comparable, T2 any](m map[T1](T2), nonil bool) map[T1](T2)
- func CopySlice[T any](src []T) []T
- func CountNonZeroVariables(vars ...any) (cnt int)
- func CustomSize(format string, size float64, base float64, _map []string) string
- func DirExists(name string) bool
- func DomHtml(el *goquery.Selection) string
- func DomRemovedSpecialCharsText(node *goquery.Selection) string
- func DomRemovedSpecialCharsTextPreservingTime(node *goquery.Selection) string
- func DomSanitizedText(el *goquery.Selection) string
- func DomSelectorText(el *goquery.Selection, selector string) (text string)
- func DomTime(s *goquery.Selection, location *time.Location) int64
- func EscapeQuotes(s string) string
- func ExistsFileWithAnySuffix(name string, suffixes []string) string
- func ExtractFilenameFromHttpHeader(header http.Header) (filename string)
- func ExtractSizeStr(str string) (int64, error)
- func ExtractTime(str string, location *time.Location) (time int64, offset int64)
- func FetchJson(url string, v any, client *http.Client, header http.Header) error
- func FetchJsonWithAzuretls(url string, v any, client *azuretls.Session, cookie string, ua string, ...) error
- func FetchUrl(url string, client *http.Client, header http.Header) (*http.Response, http.Header, error)
- func FetchUrlWithAzuretls(url string, client *azuretls.Session, cookie string, ua string, ...) (*azuretls.Response, http.Header, error)
- func FileExists(name string) bool
- func FileExistsWithOptionalSuffix(name string, suffixes ...string) bool
- func Filter[T any](ss []T, test func(T) bool) (ret []T)
- func FilterNot[T any](ss []T, test func(T) bool) (ret []T)
- func FindInSlice[T any](slice []T, checker func(T) bool) *T
- func First[T1 any, T2 any](v T1, args ...T2) T1
- func FirstNonZeroIntegerArg[T constraints.Integer](args ...T) T
- func FormatDate(ts int64) string
- func FormatDate2(ts int64) string
- func FormatDuration(seconds int64) (str string)
- func FormatTime(ts int64) string
- func FromHumanSize(size string) (int64, error)
- func GetDurationString(seconds int64) string
- func GetHttpReqHeaders(headers [][]string, cookie string, ua string) azuretls.OrderedHeaders
- func GetNewFilename(filename string) string
- func GetStructFieldValue(obj any, field string, defaultValue any) any
- func GetUrlDocWithAzuretls(url string, client *azuretls.Session, cookie string, ua string, ...) (doc *goquery.Document, res *azuretls.Response, err error)
- func GetUrlDomain(urlStr string) string
- func HasAnySuffix(str string, suffixes ...string) bool
- func HttpRequest(req *http.Request, client *http.Client) (res *http.Response, err error)
- func HumanSize(size float64) string
- func HumanSizeWithPrecision(size float64, precision int) string
- func IsHexString(str string, minLength int) bool
- func IsHostname(str string) bool
- func IsIntString(str string) bool
- func IsPureTorrentUrl(str string) bool
- func IsTorrentUrl(str string) bool
- func IsUrl(str string) bool
- func LinkDir(source string, dest string, limit int64) error
- func LogAzureHttpRequest(req *azuretls.Request)
- func LogAzureHttpRequesyBody(req *azuretls.Request, body []byte)
- func LogAzureHttpResponse(res *azuretls.Response, err error)
- func LogHttpRequest(req *http.Request)
- func LogHttpRequesyBody(req *http.Request, body []byte)
- func LogHttpResponse(res *http.Response, err error)
- func LogHttpResponseBody(res *http.Response, body []byte)
- func Map[T1 any, T2 any](ss []T1, mapper func(T1) T2) (ret []T2)
- func MapKeys[T constraints.Ordered, TV any](input map[T]TV) []T
- func MapMaxElementKey[TK comparable, TV constraints.Ordered](m map[TK](TV)) TK
- func MapString[T fmt.Stringer](ss []T) (ret []string)
- func MatchUrlWithHostOrUrl(urlStr string, hostOrUrl string) bool
- func Now() int64
- func ParseDuration(s string) (time.Duration, error)
- func ParseFutureTime(str string) (int64, error)
- func ParseInt(str string) int64
- func ParseLocalDateTime(str string) (int64, error)
- func ParseProxyFromEnv(urlStr string) string
- func ParseRelativeUrl(relativeUrl string, baseUrl string) string
- func ParseTime(str string, location *time.Location) (int64, error)
- func ParseTimeDuration(str string) (int64, error)
- func ParseTimeWithNow(str string, location *time.Location, now time.Time) (int64, error)
- func ParseUrlHostname(urlStr string) string
- func PostAndFetchJson(url string, reqBody any, resBody any, header http.Header, client *http.Client) (err error)
- func PostUploadFile(client *azuretls.Session, url string, filename string, file io.Reader, ...) (res *azuretls.Response, err error)
- func PostUploadFileForUrl(client *azuretls.Session, url string, filename string, file io.Reader, ...) (fileUrl string, err error)
- func PostUrlForJson(url string, data url.Values, v any, header http.Header, client *http.Client) error
- func PrintJson(output io.Writer, value any) error
- func PrintStringInWidth(output io.Writer, str string, width int64, padRight bool) (remain string)
- func QuoteFilename(str string) string
- func RAMInBytes(size string) (int64, error)
- func ResolvePointerValue(obj any) any
- func SanitizeText(text string) string
- func SetStructFieldValue(obj any, field string, value any)
- func Sha1(s []byte) string
- func Sha1String(s string) string
- func Sleep(seconds int64)
- func SplitCsv(str string) []string
- func String2Any(value string) (any, reflect.Kind)
- func StringPrefixInBytes(str string, max int64) string
- func StringPrefixInWidth(str string, width int64) (string, int64)
- func StructToMap(val interface{}, ignoreNoTagFields bool, ignoreEmptyFields bool) map[string]interface{}
- func ToSlash(path string) string
- func TouchFile(name string) error
- func TrimAnySuffix(str string, suffixes ...string) string
- func UniqueSlice[T comparable](slice []T) []T
- func UniqueSliceFn[TS any, TK comparable](slice []TS, keyFunc func(TS) TK) []TS
Constants ¶
const ( KB = 1000 MB = 1000 * KB GB = 1000 * MB TB = 1000 * GB PB = 1000 * TB KiB = 1024 MiB = 1024 * KiB GiB = 1024 * MiB TiB = 1024 * GiB PiB = 1024 * TiB )
See: http://en.wikipedia.org/wiki/Binary_prefix
const (
// header 占位符。用于保证实际发送 headers 的顺序
HTTP_HEADER_PLACEHOLDER = "\n"
)
Variables ¶
This section is empty.
Functions ¶
func AppendUrlQueryString ¶
Append the qs to the url and return the new url. qs is query string, e.g. "foo=1&bar=2", possibly with a "?" or "&" prefix.
func AppendUrlQueryStringDelimiter ¶
append a proper ? or & to url
func AsNetworkError ¶ added in v0.1.10
Check any error in the err tree is net.Error
func Assign ¶
https://stackoverflow.com/questions/23350173 copy non-empty field values from src to dst. dst and src must be pointors of same type of plain struct
func AssignMap ¶ added in v0.1.8
func AssignMap[T1 comparable, T2 any](args ...map[T1]T2) map[T1]T2
similar to JavaScript's Object.assign(args[0], args[1], args[2]...), update and return args[0]. However, if args[0] is nil, create and return a new map instead; if any other arg is nil, ignore it
func BytesHasAnyStringPrefix ¶ added in v0.1.10
func BytesSize ¶
BytesSize returns a human-readable size in bytes, kibibytes, mebibytes, gibibytes, or tebibytes (e.g. "44kiB", "17MiB").
func BytesSizeAround ¶ added in v0.1.9
Return at most 6 chars, e.g. "123.1G". It removes trailing zero(s) and dot, e.g. "123.0GiB" => "123G".
func Capitalize ¶
func Clean ¶ added in v0.1.10
Clean removes non-graphic characters from the given string. Removable characters are the ones for which unicode.IsGraphic() returns false. For details, see https://stackoverflow.com/a/58994297/1705598
func CopyFile ¶ added in v0.1.9
From https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file . Copy copies the contents of the file at srcpath to a regular file at dstpath. If the file named by dstpath already exists, it is truncated. The function does not copy the file mode, file permission bits, or file attributes.
func CopyMap ¶
func CopyMap[T1 comparable, T2 any](m map[T1](T2), nonil bool) map[T1](T2)
func CountNonZeroVariables ¶ added in v0.1.10
Return count of variable in vars that fulfil the condition that variable is non-zero value
func CustomSize ¶
CustomSize returns a human-readable approximation of a size using custom format.
func DomSanitizedText ¶
func DomSelectorText ¶
DIY 了几个选择器语法(附加在标准CSS选择器字符串末尾). @text 用于选择某个 Element 里的第一个 TEXT_NODE. @after 用于选择某个 Element 后面的 TEXT_NODE.
func EscapeQuotes ¶ added in v0.1.10
func ExistsFileWithAnySuffix ¶ added in v0.1.10
func ExtractFilenameFromHttpHeader ¶ added in v0.1.9
Extract filename from http response "Content-Disposition: attachment; filename=..." header
func ExtractSizeStr ¶
func ExtractTime ¶
offset: if > 0, indicates the bytes offset of the end of found time string in original str
func FetchJsonWithAzuretls ¶ added in v0.1.9
func FetchUrlWithAzuretls ¶ added in v0.1.9
func FetchUrlWithAzuretls(url string, client *azuretls.Session, cookie string, ua string, headers [][]string) (*azuretls.Response, http.Header, error)
If http response status is not 200, it return the response, header and an error
func FileExists ¶ added in v0.1.10
Check whether a file (or dir) with name exists in file system. It treat a file system error as file exits.
func FileExistsWithOptionalSuffix ¶ added in v0.1.10
Check whether a file (or dir) with name or name + suffix exists in file system. suffix could be any one in suffixes.
func FindInSlice ¶
func FirstNonZeroIntegerArg ¶ added in v0.1.10
func FirstNonZeroIntegerArg[T constraints.Integer](args ...T) T
func FormatDate ¶
func FormatDate2 ¶
func FormatDuration ¶
func FormatTime ¶
func FromHumanSize ¶
FromHumanSize returns an integer from a human-readable specification of a size using SI standard (e.g. "44kB", "17MB").
func GetDurationString ¶
func GetHttpReqHeaders ¶ added in v0.1.9
func GetUrlDocWithAzuretls ¶ added in v0.1.9
func GetUrlDomain ¶
Return (top-level) domain of a url. e.g. https://www.google.com/ => google.com. Very few PT sites do NOT use top-level domain, url of those sites are handled specially, with their (second level) site domain returned.
func HasAnySuffix ¶ added in v0.1.10
func HttpRequest ¶ added in v0.1.10
func HumanSize ¶
HumanSize returns a human-readable approximation of a size capped at 4 valid numbers (e.g. "2.746 MB", "796 KB").
func HumanSizeWithPrecision ¶
HumanSizeWithPrecision allows the size to be in any precision, instead of 4 digit precision used in units.HumanSize.
func IsHexString ¶ added in v0.1.8
func IsHostname ¶ added in v0.1.8
func IsIntString ¶
func IsPureTorrentUrl ¶ added in v0.1.9
Check whether str is a url of "magnet:" or "bc://bt/" schema.
func IsTorrentUrl ¶ added in v0.1.9
Check whether str is a normal (http / https schema) or torrent (magnet / bt schema) url
func LinkDir ¶ added in v0.1.9
Create hardlink duplicate for source dir at dest. Recursively process all files and folders inside source. Symbolinks are ignored. For file with size < limit, create a copy instead.
func LogAzureHttpRequest ¶ added in v0.1.10
func LogAzureHttpRequest(req *azuretls.Request)
Log if dump-headers flag is set.
func LogAzureHttpRequesyBody ¶ added in v0.1.10
func LogAzureHttpRequesyBody(req *azuretls.Request, body []byte)
func LogAzureHttpResponse ¶ added in v0.1.10
func LogAzureHttpResponse(res *azuretls.Response, err error)
Log if dump-headers flag is set.
func LogHttpRequest ¶ added in v0.1.10
Log http request info if dump-headers flag is set.
func LogHttpRequesyBody ¶ added in v0.1.10
func LogHttpResponse ¶ added in v0.1.10
Log http response info if dump-headers flag is set.
func LogHttpResponseBody ¶ added in v0.1.10
func MapKeys ¶ added in v0.1.8
func MapKeys[T constraints.Ordered, TV any](input map[T]TV) []T
func MapMaxElementKey ¶
func MapMaxElementKey[TK comparable, TV constraints.Ordered](m map[TK](TV)) TK
func MatchUrlWithHostOrUrl ¶
func ParseDuration ¶
ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d", "w".
func ParseFutureTime ¶
func ParseLocalDateTime ¶
func ParseProxyFromEnv ¶ added in v0.1.9
Parse standard HTTP_PROXY, HTTPS_PROXY, NO_PROXY (and lowercase versions) envs, return proxy for urlStr.
func ParseRelativeUrl ¶ added in v0.1.9
Parse a baseUrl relative relativeUrl, return absolute url. baseUrl could also be a host, in which case https schema is assumed.
func ParseTimeDuration ¶
Return time duration in seconds
func ParseTimeWithNow ¶ added in v0.1.10
Parse time (with date) string. . It try to parse str in any of the below time format: "yyyy-MM-ddHH:mm:ss", "yyyy-MM-dd HH:mm:ss", <integer> (unix timestamp in seconds), "time duration" (e.g. "5d", "6hm5s", "4天5时") (treat as pasted time til now) If location is nil, current local timezone is used.
func ParseUrlHostname ¶
func PostAndFetchJson ¶ added in v0.1.10
func PostUploadFile ¶ added in v0.1.10
func PostUploadFile(client *azuretls.Session, url string, filename string, file io.Reader, fileFieldname string, additionalFields url.Values, headers [][]string) (res *azuretls.Response, err error)
Common func for uploading image or other file to public server using post + multipart/form-data request. fileFieldname is the field name of file binary in post data, default to "file". If file is nil, open and read from filename instead. If file is not nil, filename is only used to derive mime and can be a dummy name.
func PostUploadFileForUrl ¶ added in v0.1.10
func PostUploadFileForUrl(client *azuretls.Session, url string, filename string, file io.Reader, fileFieldname string, additionalFields url.Values, headers [][]string, responseUrlField string) (fileUrl string, err error)
Upload file to server, then extract the "file url" from server response. fileFieldname default to "file". responseUrlField default to "url".
func PostUrlForJson ¶
func PrintStringInWidth ¶
func QuoteFilename ¶
func RAMInBytes ¶
RAMInBytes parses a human-readable string representing an amount of RAM in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and returns the number of bytes, or -1 if the string is unparseable. Units are case-insensitive, and the 'b' suffix is optional. Specially, if size is "-1", return -1,nil
func ResolvePointerValue ¶
func SanitizeText ¶
func SetStructFieldValue ¶
https://stackoverflow.com/questions/6395076/using-reflect-how-do-you-set-the-value-of-a-struct-field
func Sha1String ¶
func SplitCsv ¶ added in v0.1.9
split a csv like line to values. "a, b, c" => [a,b,c]. If str is empty string, return nil.
func StringPrefixInBytes ¶ added in v0.1.9
Return prefix of str that is at most max bytes encoded in UTF-8
func StringPrefixInWidth ¶
Return prefix of string at most width and actual width. ASCII char has 1 width. CJK char has 2 width.
func StructToMap ¶ added in v0.1.8
func StructToMap(val interface{}, ignoreNoTagFields bool, ignoreEmptyFields bool) map[string]interface{}
From https://stackoverflow.com/questions/23589564/function-for-converting-a-struct-to-map-in-golang .
func TrimAnySuffix ¶ added in v0.1.10
func UniqueSlice ¶
func UniqueSlice[T comparable](slice []T) []T
func UniqueSliceFn ¶
func UniqueSliceFn[TS any, TK comparable](slice []TS, keyFunc func(TS) TK) []TS
Return de-duplicated slice that every member has unique key.
Types ¶
This section is empty.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Utilities funcs that have side effects.
|
Utilities funcs that have side effects. |