mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-23 13:36:46 +08:00
feat: update parser interface to include context in Parse method
This commit is contained in:
@@ -52,11 +52,15 @@ func (p *jsParser) CanHandle(url string) bool {
|
|||||||
return resp.ok && resp.err == nil
|
return resp.ok && resp.err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *jsParser) Parse(url string) (*parser.Item, error) {
|
func (p *jsParser) Parse(ctx context.Context, url string) (*parser.Item, error) {
|
||||||
respCh := make(chan jsParserResp, 1)
|
respCh := make(chan jsParserResp, 1)
|
||||||
p.reqCh <- jsParserReq{method: "parse", url: url, respCh: respCh}
|
p.reqCh <- jsParserReq{method: "parse", url: url, respCh: respCh}
|
||||||
resp := <-respCh
|
select {
|
||||||
return resp.item, resp.err
|
case resp := <-respCh:
|
||||||
|
return resp.item, resp.err
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newJSParser(vm *goja.Runtime, canHandleFunc, parseFunc goja.Value, metadata PluginMeta) *jsParser {
|
func newJSParser(vm *goja.Runtime, canHandleFunc, parseFunc goja.Value, metadata PluginMeta) *jsParser {
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
|
|||||||
if !pser.CanHandle(url) {
|
if !pser.CanHandle(url) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
item, err := pser.Parse(url)
|
item, err := pser.Parse(ctx, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errCh <- err
|
errCh <- err
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package twitter
|
package twitter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -34,13 +35,17 @@ func getTweetID(sourceURL string) string {
|
|||||||
return matches[2]
|
return matches[2]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *TwitterParser) Parse(u string) (*parser.Item, error) {
|
func (p *TwitterParser) Parse(ctx context.Context, u string) (*parser.Item, error) {
|
||||||
id := getTweetID(u)
|
id := getTweetID(u)
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return nil, errors.New("invalid Twitter URL")
|
return nil, errors.New("invalid Twitter URL")
|
||||||
}
|
}
|
||||||
apiUrl := fmt.Sprintf("https://%s/_/status/%s", FxTwitterApi, id)
|
apiUrl := fmt.Sprintf("https://%s/_/status/%s", FxTwitterApi, id)
|
||||||
resp, err := p.client.Get(apiUrl)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiUrl, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create request to Twitter API: %w", err)
|
||||||
|
}
|
||||||
|
resp, err := p.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to fetch Twitter API: %w", err)
|
return nil, fmt.Errorf("failed to fetch Twitter API: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parser interface {
|
type Parser interface {
|
||||||
CanHandle(url string) bool
|
CanHandle(url string) bool
|
||||||
Parse(url string) (*Item, error)
|
Parse(ctx context.Context, url string) (*Item, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resource is a single downloadable resource with metadata.
|
// Resource is a single downloadable resource with metadata.
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ SaveAnyBot 可通过插件扩展功能, 目前仅支持 Parser (解析器)插件
|
|||||||
```go
|
```go
|
||||||
type Parser interface {
|
type Parser interface {
|
||||||
CanHandle(url string) bool // 判断是否能处理给定的 URL
|
CanHandle(url string) bool // 判断是否能处理给定的 URL
|
||||||
Parse(url string) (*Item, error) // 解析 URL, 返回 Item
|
Parse(ctx context.Context, url string) (*Item, error) // 解析 URL, 返回 Item
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resource is a single downloadable resource with metadata.
|
// Resource is a single downloadable resource with metadata.
|
||||||
|
|||||||
Reference in New Issue
Block a user