Skip to content

Commit ebcebd9

Browse files
committed
v0.8.2
- 新增插件调试程序
1 parent b91c3f0 commit ebcebd9

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

assets/plugin/filter/testdata.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[
2+
{
3+
"url": "https://mikanani.me/Home/Episode/dcc28079dfda415cdcdf46159aad0fa94f1a2f11",
4+
"name": "[LoliHouse] 想要成为影之实力者 / 我想成为影之强者 / Kage no Jitsuryokusha ni Naritakute! [01-20 合集][WebRip 1080p HEVC-10bit AAC][简繁内封字幕][Fin]",
5+
"date": "2023-02-22",
6+
"type": "application/x-bittorrent",
7+
"download": "https://mikanani.me/Download/20230222/dcc28079dfda415cdcdf46159aad0fa94f1a2f11.torrent",
8+
"length": 7623566848,
9+
"parsed": null
10+
},
11+
{
12+
"url": "https://mikanani.me/Home/Episode/8411b8de5aecb5be2af78726e5f0abb36a6a9b22",
13+
"name": "[LoliHouse] 想要成为影之实力者 / 我想成为影之强者 / Kage no Jitsuryokusha ni Naritakute! - 20 [WebRip 1080p HEVC-10bit AAC][简繁内封字幕][END]",
14+
"date": "2023-02-16",
15+
"type": "application/x-bittorrent",
16+
"download": "https://mikanani.me/Download/20230216/8411b8de5aecb5be2af78726e5f0abb36a6a9b22.torrent",
17+
"length": 432747328,
18+
"parsed": null
19+
},
20+
{
21+
"url": "https://mikanani.me/Home/Episode/eb98d74dd42effb215dedd11b894105f892946e4",
22+
"name": "[LoliHouse] 想要成为影之实力者 / 我想成为影之强者 / Kage no Jitsuryokusha ni Naritakute! - 19 [WebRip 1080p HEVC-10bit AAC][简繁内封字幕]",
23+
"date": "2023-02-09",
24+
"type": "application/x-bittorrent",
25+
"download": "https://mikanani.me/Download/20230209/eb98d74dd42effb215dedd11b894105f892946e4.torrent",
26+
"length": 319301888,
27+
"parsed": null
28+
},
29+
{
30+
"url": "https://mikanani.me/Home/Episode/0576a4e4f757e2c98f8aa48da4c5236ed6034ee0",
31+
"name": "[LoliHouse] 想要成为影之实力者 / 我想成为影之强者 / Kage no Jitsuryokusha ni Naritakute! - 14 [WebRip 1080p HEVC-10bit AAC][简繁内封字幕]",
32+
"date": "2023-01-05",
33+
"type": "application/x-bittorrent",
34+
"download": "https://mikanani.me/Download/20230105/0576a4e4f757e2c98f8aa48da4c5236ed6034ee0.torrent",
35+
"length": 461216160,
36+
"parsed": null
37+
},
38+
{
39+
"url": "https://mikanani.me/Home/Episode/9fb1b5150d8a00cb8317913bb8b211f82061fa28",
40+
"name": "[LoliHouse] 想要成为影之实力者 / 我想成为影之强者 / Kage no Jitsuryokusha ni Naritakute! - 13 [WebRip 1080p HEVC-10bit AAC][简繁内封字幕]",
41+
"date": "2022-12-29",
42+
"type": "application/x-bittorrent",
43+
"download": "https://mikanani.me/Download/20221229/9fb1b5150d8a00cb8317913bb8b211f82061fa28.torrent",
44+
"length": 396571456,
45+
"parsed": null
46+
},
47+
{
48+
"url": "https://mikanani.me/Home/Episode/99bcd0c631fb70e8700fcd5ee8b3c6c9cf320c8d",
49+
"name": "[LoliHouse] 想要成为影之实力者 / 我想成为影之强者 / Kage no Jitsuryokusha ni Naritakute! - 12 [WebRip 1080p HEVC-10bit AAC][简繁内封字幕]",
50+
"date": "2022-12-22",
51+
"type": "application/x-bittorrent",
52+
"download": "https://mikanani.me/Download/20221222/99bcd0c631fb70e8700fcd5ee8b3c6c9cf320c8d.torrent",
53+
"length": 363834912,
54+
"parsed": null
55+
}
56+
]

cmd/plugin/main.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"os"
7+
"sync"
8+
9+
filterPlugin "github.com/wetor/AnimeGo/internal/animego/filter/plugin"
10+
"github.com/wetor/AnimeGo/internal/models"
11+
"github.com/wetor/AnimeGo/internal/plugin"
12+
"github.com/wetor/AnimeGo/internal/schedule"
13+
"github.com/wetor/AnimeGo/internal/schedule/task"
14+
"github.com/wetor/AnimeGo/pkg/json"
15+
"github.com/wetor/AnimeGo/pkg/log"
16+
"github.com/wetor/AnimeGo/pkg/xpath"
17+
)
18+
19+
var (
20+
wg sync.WaitGroup
21+
ctx context.Context
22+
23+
pFile string
24+
pPlugin string
25+
pType string
26+
pPythonEntryFunc string
27+
pArgsJson string
28+
pVarsJson string
29+
pFilterInputFile string
30+
)
31+
32+
func main() {
33+
flag.StringVar(&pFile, "file", "", "插件脚本文件")
34+
flag.StringVar(&pPlugin, "plugin", "python", "插件类型,支持['python', 'filter', 'schedule', 'feed']")
35+
flag.StringVar(&pType, "type", "python", "插件脚本类型,支持['python']")
36+
flag.StringVar(&pArgsJson, "args", "", "插件入口函数默认参数,json格式字符串")
37+
flag.StringVar(&pVarsJson, "vars", "", "插件全局变量,json格式字符串")
38+
flag.StringVar(&pPythonEntryFunc, "python_entry", "main", "python插件入口函数名")
39+
flag.StringVar(&pFilterInputFile, "filter_input", "", "filter插件输入json文件")
40+
flag.Parse()
41+
42+
if len(pFile) == 0 {
43+
panic("需要参数 file")
44+
}
45+
46+
log.Init(&log.Options{
47+
File: "plugin.log",
48+
Debug: true,
49+
})
50+
ctx = context.Background()
51+
52+
var err error
53+
args := make(map[string]any)
54+
vars := make(map[string]any)
55+
if len(pArgsJson) > 0 {
56+
err = json.Unmarshal([]byte(pArgsJson), &args)
57+
if err != nil {
58+
log.Warnf("插件入口函数默认参数解析错误: %v", err)
59+
}
60+
}
61+
if len(pVarsJson) > 0 {
62+
err = json.Unmarshal([]byte(pVarsJson), &vars)
63+
if err != nil {
64+
log.Warnf("插件全局变量解析错误: %v", err)
65+
}
66+
}
67+
dir, _ := os.Getwd()
68+
plugin.Init(&plugin.Options{
69+
Path: dir,
70+
})
71+
switch pPlugin {
72+
case "python":
73+
p := plugin.LoadPlugin(&plugin.LoadPluginOptions{
74+
Plugin: &models.Plugin{
75+
Enable: true,
76+
Type: pType,
77+
File: pFile,
78+
Args: args,
79+
Vars: vars,
80+
},
81+
EntryFunc: pPythonEntryFunc,
82+
})
83+
result := p.Run(pPythonEntryFunc, map[string]any{})
84+
log.Info(result)
85+
case "filter":
86+
data, err := os.ReadFile(pFilterInputFile)
87+
if err != nil {
88+
panic(err)
89+
}
90+
items := make([]*models.FeedItem, 0)
91+
err = json.Unmarshal(data, &items)
92+
if err != nil {
93+
panic(err)
94+
}
95+
96+
f := filterPlugin.NewFilterPlugin([]models.Plugin{
97+
{
98+
Enable: true,
99+
Type: pType,
100+
File: pFile,
101+
Args: args,
102+
Vars: vars,
103+
},
104+
})
105+
result := f.Filter(items)
106+
for i, item := range result {
107+
jsonData, _ := json.Marshal(item)
108+
log.Infof("[%d] filter结果: %v", i, string(jsonData))
109+
}
110+
case "schedule", "feed":
111+
s := schedule.NewSchedule(&schedule.Options{
112+
WG: &wg,
113+
})
114+
if pPlugin == "schedule" {
115+
s.Add(&schedule.AddTaskOptions{
116+
Name: xpath.Base(pFile),
117+
StartRun: false,
118+
Task: task.NewScheduleTask(&task.ScheduleOptions{
119+
Plugin: &models.Plugin{
120+
Enable: true,
121+
Type: pType,
122+
File: pFile,
123+
Args: args,
124+
Vars: vars,
125+
},
126+
}),
127+
})
128+
} else if pPlugin == "feed" {
129+
s.Add(&schedule.AddTaskOptions{
130+
Name: xpath.Base(pFile),
131+
StartRun: false,
132+
Task: task.NewFeedTask(&task.FeedOptions{
133+
Plugin: &models.Plugin{
134+
Enable: true,
135+
Type: pType,
136+
File: pFile,
137+
Args: args,
138+
Vars: vars,
139+
},
140+
Callback: func(items []*models.FeedItem) {
141+
for i, item := range items {
142+
jsonData, _ := json.Marshal(item)
143+
log.Infof("[%d] 接收到feed下载项: %v", i, string(jsonData))
144+
}
145+
},
146+
}),
147+
})
148+
}
149+
150+
s.Start(ctx)
151+
wg.Wait()
152+
default:
153+
panic("不支持的插件类型 " + pPlugin)
154+
}
155+
156+
}

0 commit comments

Comments
 (0)