feat: enhance text message handling and parser configuration

This commit is contained in:
krau
2025-08-23 20:17:56 +08:00
parent b87dd68880
commit 3aa1e2eaed
2 changed files with 48 additions and 30 deletions

View File

@@ -6,32 +6,16 @@ import (
"sync"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/parsers/kemono"
"github.com/krau/SaveAny-Bot/parsers/twitter"
"github.com/krau/SaveAny-Bot/pkg/parser"
)
var (
parsers []parser.Parser
parsersMu sync.Mutex
doConfig sync.Once
)
func AddParser(p ...parser.Parser) {
parsersMu.Lock()
defer parsersMu.Unlock()
parsers = append(parsers, p...)
}
func init() {
AddParser(new(twitter.TwitterParser))
}
var (
ErrNoParserFound = fmt.Errorf("no parser found for the given URL")
)
func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
doConfig.Do(func() {
parsers []parser.Parser
parsersMu sync.Mutex
doConfig sync.Once
configParsers = func() {
if len(parsers) == 0 {
return
}
@@ -45,7 +29,25 @@ func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
}
}
}
})
}
)
func AddParser(p ...parser.Parser) {
parsersMu.Lock()
defer parsersMu.Unlock()
parsers = append(parsers, p...)
}
func init() {
AddParser(new(twitter.TwitterParser), new(kemono.KemonoParser))
}
var (
ErrNoParserFound = fmt.Errorf("no parser found for the given URL")
)
func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
doConfig.Do(configParsers)
ch := make(chan *parser.Item, 1)
errCh := make(chan error, 1)
@@ -74,3 +76,13 @@ func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
return nil, ctx.Err()
}
}
func CanHandle(url string) (bool, parser.Parser) {
doConfig.Do(configParsers)
for _, pser := range parsers {
if pser.CanHandle(url) {
return true, pser
}
}
return false, nil
}