mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-11 22:49:40 +08:00
* feat: WIP. add parser functionality and text message handling * fix: use json to marshal js result * feat: add metadata handling and version validation for jsParser * refactor: rename parser package to parsers and restructure parser handling * refactor: core code struct and impl parse task handle * feat: impl parsed download * fix: seek cache file when processing tph picture * feat: implement parsed task handling and progress tracking * feat: enhance task processing with concurrency control and progress tracking * feat: add resource ID generation and improve resource processing handling * feat: improve message formatting in parsed text and progress completion * feat: add example js plugin * feat: implement Twitter parser * fix: twitter parse video json decode error * feat: impl stream mode for parse task
66 lines
1.0 KiB
Go
66 lines
1.0 KiB
Go
package parsers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/krau/SaveAny-Bot/parsers/twitter"
|
|
"github.com/krau/SaveAny-Bot/pkg/parser"
|
|
)
|
|
|
|
var (
|
|
parsers []parser.Parser
|
|
parsersMu sync.Mutex
|
|
)
|
|
|
|
func GetParsers() []parser.Parser {
|
|
parsersMu.Lock()
|
|
defer parsersMu.Unlock()
|
|
return parsers
|
|
}
|
|
|
|
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) {
|
|
ch := make(chan *parser.Item, 1)
|
|
errCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
for _, pser := range parsers {
|
|
if !pser.CanHandle(url) {
|
|
continue
|
|
}
|
|
item, err := pser.Parse(url)
|
|
if err != nil {
|
|
errCh <- err
|
|
return
|
|
}
|
|
ch <- item
|
|
return
|
|
}
|
|
errCh <- ErrNoParserFound
|
|
}()
|
|
|
|
select {
|
|
case item := <-ch:
|
|
return item, nil
|
|
case err := <-errCh:
|
|
return nil, err
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
}
|