File size: 3,677 Bytes
ca7217f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | package parser
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/araddon/dateparse"
"golang.org/x/net/html"
dt "gorm.io/datatypes"
"github.com/metatube-community/metatube-sdk-go/common/convertor"
)
// ParseInt parses string to int regardless.
func ParseInt(s string) int {
s = strings.TrimSpace(s)
n, _ := strconv.Atoi(s)
return n
}
// ParseTime parses a string with a valid time format into time.Time.
func ParseTime(s string) time.Time {
s = strings.TrimSpace(s)
if ss := regexp.MustCompile(`([\s\d]+)年([\s\d]+)月([\s\d]+)日`).
FindStringSubmatch(s); len(ss) == 4 {
s = fmt.Sprintf("%s-%s-%s",
strings.TrimSpace(ss[1]),
strings.TrimSpace(ss[2]),
strings.TrimSpace(ss[3]))
}
t, _ := dateparse.ParseAny(s)
return t
}
// ParseDate parses a string with a valid date format into Date.
func ParseDate(s string) dt.Date {
return dt.Date(ParseTime(s))
}
// ParseDuration parses a string with valid duration format into time.Duration.
func ParseDuration(s string) time.Duration {
s = convertor.ReplaceSpaceAll(s)
s = strings.ToLower(s)
s = strings.ReplaceAll(s, "秒", "s")
s = strings.ReplaceAll(s, "分", "m")
s = strings.ReplaceAll(s, "時", "h")
s = strings.ReplaceAll(s, "时", "h")
s = strings.ReplaceAll(s, "sec", "s")
s = strings.ReplaceAll(s, "min", "m")
if ss := regexp.MustCompile(`(?i)(\d+):(\d+):(\d+)`).FindStringSubmatch(s); len(ss) > 0 {
s = fmt.Sprintf("%02sh%02sm%02ss", ss[1], ss[2], ss[3])
} else if ss := regexp.MustCompile(`(?i)(\d+):(\d+)`).FindStringSubmatch(s); len(ss) > 0 {
s = fmt.Sprintf("%02sm%02ss", ss[1], ss[2])
} else if ss := regexp.MustCompile(`(?i)(\d+[mhs]?)`).FindAllStringSubmatch(s, -1); len(ss) > 0 {
ds := make([]string, 0, 3)
for _, d := range ss {
ds = append(ds, d[1])
}
s = strings.Join(ds, "")
}
d, _ := time.ParseDuration(s)
return d
}
// ParseRuntime parses a string into time.Duration and converts it to minutes as integer.
func ParseRuntime(s string) int {
return int(ParseDuration(s).Minutes())
}
// ParseScore parses a string into a float-based score.
func ParseScore(s string) float64 {
s = strings.ReplaceAll(s, "点", "")
fields := strings.Fields(s)
if len(fields) == 0 {
return 0
}
s = strings.TrimSpace(fields[0])
n, _ := strconv.ParseFloat(s, 64)
return n
}
// ParseTexts parses all plaintext from the given *html.Node.
func ParseTexts(n *html.Node, texts *[]string) {
if n.Type == html.TextNode {
if text := strings.TrimSpace(n.Data); text != "" {
*texts = append(*texts, text)
}
}
for n := n.FirstChild; n != nil; n = n.NextSibling {
ParseTexts(n, texts)
}
}
func ParseActorNames(s string) (names []string) {
add := func(name string) {
if name = strings.TrimSpace(name); len(name) > 0 {
names = append(names, name)
}
}
sb := &strings.Builder{}
for _, r := range s {
switch r {
case '、', ';', ',':
fallthrough
case '(', '(':
fallthrough
case ')', ')':
add(sb.String())
sb.Reset()
default:
sb.WriteRune(r)
}
}
add(sb.String())
return
}
func ParseIDToNumber(s string) string {
s = strings.ToUpper(s)
if ss := regexp.MustCompile(`(\d*[A-Z]+)(\d+)`).FindStringSubmatch(s); len(ss) >= 3 {
return fmt.Sprintf("%s-%s", ss[1], ss[2])
}
return s
}
func ParseBustCupSize(s string) (int, string, error) {
sizeRe := regexp.MustCompile(`^(\d+)\s?([A-Z])$`)
match := sizeRe.FindStringSubmatch(s)
if len(match) != 3 {
return 0, "", fmt.Errorf("invalid format: %s", s)
}
num := match[1]
unit := match[2]
value, err := strconv.Atoi(num)
if err != nil {
return 0, "", fmt.Errorf("failed to parse numeric part '%s': %w", num, err)
}
return value, unit, nil
}
|