2022-11-04 18:24:08 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/dlclark/regexp2"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SubscriptionInfo struct {
|
2022-11-07 14:34:30 +00:00
|
|
|
Upload uint64
|
|
|
|
Download uint64
|
|
|
|
Total uint64
|
|
|
|
Expire uint64
|
2022-11-04 18:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSubscriptionInfo(str string) (si *SubscriptionInfo, err error) {
|
|
|
|
si = &SubscriptionInfo{}
|
|
|
|
str = strings.ToLower(str)
|
|
|
|
reTraffic := regexp2.MustCompile("upload=(\\d+); download=(\\d+); total=(\\d+)", 0)
|
|
|
|
reExpire := regexp2.MustCompile("expire=(\\d+)", 0)
|
|
|
|
|
|
|
|
match, err := reTraffic.FindStringMatch(str)
|
|
|
|
if err != nil || match == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
group := match.Groups()
|
2022-11-07 14:34:30 +00:00
|
|
|
si.Upload, err = str2uint64(group[1].String())
|
2022-11-04 18:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-07 14:34:30 +00:00
|
|
|
|
|
|
|
si.Download, err = str2uint64(group[2].String())
|
2022-11-04 18:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-07 14:34:30 +00:00
|
|
|
|
|
|
|
si.Total, err = str2uint64(group[3].String())
|
2022-11-04 18:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
match, _ = reExpire.FindStringMatch(str)
|
|
|
|
if match != nil {
|
|
|
|
group = match.Groups()
|
2022-11-07 14:34:30 +00:00
|
|
|
si.Expire, err = str2uint64(group[1].String())
|
2022-11-04 18:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 14:34:30 +00:00
|
|
|
|
|
|
|
func str2uint64(str string) (uint64, error) {
|
|
|
|
i, err := strconv.ParseInt(str, 10, 64)
|
|
|
|
return uint64(i), err
|
|
|
|
}
|