feat: update parser interface to include context in Parse method

This commit is contained in:
krau
2025-08-23 14:01:00 +08:00
parent 231eb61d25
commit 03eb4f8a18
5 changed files with 18 additions and 8 deletions

View File

@@ -52,11 +52,15 @@ func (p *jsParser) CanHandle(url string) bool {
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)
p.reqCh <- jsParserReq{method: "parse", url: url, respCh: respCh}
resp := <-respCh
return resp.item, resp.err
select {
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 {

View File

@@ -43,7 +43,7 @@ func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
if !pser.CanHandle(url) {
continue
}
item, err := pser.Parse(url)
item, err := pser.Parse(ctx, url)
if err != nil {
errCh <- err
return

View File

@@ -1,6 +1,7 @@
package twitter
import (
"context"
"encoding/json"
"errors"
"fmt"
@@ -34,13 +35,17 @@ func getTweetID(sourceURL string) string {
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)
if id == "" {
return nil, errors.New("invalid Twitter URL")
}
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 {
return nil, fmt.Errorf("failed to fetch Twitter API: %w", err)
}