|
| 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