mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-12 01:19:41 +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
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user