MCPcopy Create free account
hub / github.com/krau/SaveAny-Bot / ExtractTelegraphImages

Function ExtractTelegraphImages

api/tphpics.go:15–47  ·  view source on GitHub ↗

ExtractTelegraphImages 从 Telegraph URL 提取图片

(ctx context.Context, pageURL string)

Source from the content-addressed store, hash-verified

13
14// ExtractTelegraphImages 从 Telegraph URL 提取图片
15func ExtractTelegraphImages(ctx context.Context, pageURL string) ([]string, string, error) {
16 logger := log.FromContext(ctx)
17
18 // 验证 URL 格式
19 if !isValidTelegraphURL(pageURL) {
20 return nil, "", fmt.Errorf("invalid telegraph URL format: %s", pageURL)
21 }
22
23 // 解析 URL 获取页面路径
24 pagepath, err := parseTelegraphPath(pageURL)
25 if err != nil {
26 return nil, "", err
27 }
28
29 logger.Debugf("Fetching telegraph page: %s", pagepath)
30
31 client := telegraph.NewClient()
32 page, err := client.GetPage(ctx, pagepath)
33 if err != nil {
34 return nil, "", fmt.Errorf("failed to get telegraph page: %w", err)
35 }
36
37 var imgs []string
38 for _, elem := range page.Content {
39 imgs = append(imgs, tphutil.GetNodeImages(elem)...)
40 }
41
42 if len(imgs) == 0 {
43 return nil, "", fmt.Errorf("no images found in telegraph page")
44 }
45
46 return imgs, pagepath, nil
47}
48
49// parseTelegraphPath 解析 Telegraph URL 获取页面路径
50func parseTelegraphPath(pageURL string) (string, error) {

Callers 1

createTPHPicsTaskMethod · 0.85

Calls 5

GetPageMethod · 0.95
NewClientFunction · 0.92
GetNodeImagesFunction · 0.92
isValidTelegraphURLFunction · 0.85
parseTelegraphPathFunction · 0.85

Tested by

no test coverage detected