// 绑定公共接口 package main import ( "container/list" "encoding/json" "fmt" "github.com/bmaupin/go-epub" "github.com/wailsapp/wails/v2/pkg/runtime" "github.com/xuri/excelize/v2" qu "jygit.jydev.jianyu360.cn/data_processing/common_utils" "log" "os" "os/exec" sysruntim "runtime" be "spider_creator/backend" "strconv" "strings" ) var FileType = map[string]runtime.FileFilter{ "epub": runtime.FileFilter{Pattern: "*.epub", DisplayName: "epub file *.epub"}, "xlsx": runtime.FileFilter{Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"}, "json": runtime.FileFilter{Pattern: "*.json", DisplayName: "json file *.json"}, } // Greet returns a greeting for the given name func (a *App) Dispatch(event string, data interface{}) error { runtime.EventsEmit(a.ctx, event, data) return nil } // SelectSaveFilePath func (a *App) SelectSaveFilePath(defaultDirectory, defaultFileName, defaulFileType string) string { qu.Debug("导出文件位置:", defaultDirectory, defaultFileName, defaulFileType) path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{Filters: []runtime.FileFilter{ FileType[defaulFileType], }, DefaultFilename: defaultFileName, DefaultDirectory: defaultDirectory, }) if err != nil { qu.Debug(err.Error()) return "" } return path } // SelectOpenFilePath func (a *App) SelectOpenFilePath() string { path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{Filters: []runtime.FileFilter{ {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"}, }}) if err != nil { qu.Debug(err.Error()) return "" } return path } // RunExportExcelFile 数据集导出到excel文件中 func (a *App) RunExportExcelFile(filepath, code string, currentResult *list.List) error { qu.Debug("filepath---", filepath) f := excelize.NewFile() defer f.Close() f.SetCellStr("Sheet1", "A1", "站点") f.SetCellStr("Sheet1", "B1", "栏目") f.SetCellStr("Sheet1", "C1", "爬虫") //写入数据 f.SetCellStr("Sheet1", "D1", "标题") f.SetCellStr("Sheet1", "E1", "链接") f.SetCellStr("Sheet1", "F1", "发布单位") f.SetCellStr("Sheet1", "G1", "发布时间") f.SetCellStr("Sheet1", "H1", "正文") f.SetCellStr("Sheet1", "I1", "附件") i := 0 for el := currentResult.Front(); el != nil; el = el.Next() { r, _ := el.Value.(*be.ResultItem) //写入站点信息 iStr := strconv.Itoa(i + 2) f.SetCellStr("Sheet1", "A"+iStr, r.Site) f.SetCellStr("Sheet1", "B"+iStr, r.Channel) f.SetCellStr("Sheet1", "C"+iStr, code) //写入数据 f.SetCellStr("Sheet1", "D"+iStr, r.Title) f.SetCellStr("Sheet1", "E"+iStr, r.Href) f.SetCellStr("Sheet1", "F"+iStr, r.PublishUnit) f.SetCellStr("Sheet1", "G"+iStr, r.ListPubTime) f.SetCellStr("Sheet1", "H"+iStr, r.Content) f.SetCellStr("Sheet1", "I"+iStr, "") if len(r.AttachLinks) > 0 { bs, err := json.Marshal(r.AttachLinks) if err == nil { f.SetCellStr("Sheet1", "I"+iStr, string(bs)) } } i += 1 } err := f.SaveAs(filepath) if err != nil { return err } return nil } func (a *App) RunExportJsonFile(filepath, code string, currentResult *list.List) error { qu.Debug("filepath---", filepath) var result []map[string]interface{} for el := currentResult.Front(); el != nil; el = el.Next() { r, _ := el.Value.(*be.ResultItem) rmap := map[string]interface{}{ "site": r.Site, "channel": r.Channel, "code": code, "title": r.Title, "href": r.Href, "publishdept": r.PublishUnit, "publishtime": r.ListPubTime, "detail": r.Content, "attachLinks": r.AttachLinks, } result = append(result, rmap) } jsonData, err := json.MarshalIndent(result, "", " ") if err != nil { return err } fo, err := os.Create(filepath) if err != nil { return err } defer fo.Close() if _, err := fo.Write(jsonData); err != nil { return fmt.Errorf("failed to write data to file: %w", err) } return nil } // RunExportEpubFile 导出epub文件 func (a *App) RunExportEpubFile(bookname, filepath string, currentResult *list.List) error { qu.Debug("filepath---", filepath) output := epub.NewEpub(bookname) output.SetTitle(bookname) output.SetDescription(bookname) output.SetAuthor("unknow") i := 1 for el := currentResult.Front(); el != nil; el = el.Next() { art, _ := el.Value.(*be.ResultItem) body := "

" + art.Title + "

" + strings.Join(strings.Split(art.Content, "\n"), "

") + "

" output.AddSection(body, art.Title, fmt.Sprintf("%06d.xhtml", i+1), "") i += 1 } fo, err := os.Create(filepath) if err != nil { a.Dispatch("debug_event", err.Error()) } output.WriteTo(fo) fo.Close() return nil } // 杀死所有chrome进程 func (a *App) KillAllChrome() string { killChrome() return "ok" } // 杀死chrome进程 func killChrome() { // 根据操作系统选择不同的命令 var cmd *exec.Cmd qu.Debug("电脑系统:", sysruntim.GOOS) if sysruntim.GOOS == "windows" { // 在Windows上使用taskkill命令 cmd = exec.Command("taskkill", "/F", "/IM", "chrome.exe") } else { // 在类Unix系统上使用pkill命令 cmd = exec.Command("pkill", "-f", "chrome") } // 执行命令 err := cmd.Run() if err != nil { log.Println("Error killing process:", err) return } log.Println("Chrome process killed successfully.") }