123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- // 绑定公共接口
- 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 := "<h2>" + art.Title + "</h2><p>" + strings.Join(strings.Split(art.Content, "\n"), "</p><p>") + "</p>"
- 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.")
- }
|