mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-14 03:49:36 +08:00
feat: parse url with js plugins support (#96)
* 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
This commit is contained in:
65
parsers/parser.go
Normal file
65
parsers/parser.go
Normal file
@@ -0,0 +1,65 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user