bind4comm.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // 绑定公共接口
  2. package main
  3. import (
  4. "container/list"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/bmaupin/go-epub"
  8. "github.com/wailsapp/wails/v2/pkg/runtime"
  9. "github.com/xuri/excelize/v2"
  10. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "log"
  12. "os"
  13. "os/exec"
  14. sysruntim "runtime"
  15. be "spider_creator/backend"
  16. "strconv"
  17. "strings"
  18. )
  19. var FileType = map[string]runtime.FileFilter{
  20. "epub": runtime.FileFilter{Pattern: "*.epub", DisplayName: "epub file *.epub"},
  21. "xlsx": runtime.FileFilter{Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  22. "json": runtime.FileFilter{Pattern: "*.json", DisplayName: "json file *.json"},
  23. }
  24. // Greet returns a greeting for the given name
  25. func (a *App) Dispatch(event string, data interface{}) error {
  26. runtime.EventsEmit(a.ctx, event, data)
  27. return nil
  28. }
  29. // SelectSaveFilePath
  30. func (a *App) SelectSaveFilePath(defaultDirectory, defaultFileName, defaulFileType string) string {
  31. qu.Debug("导出文件位置:", defaultDirectory, defaultFileName, defaulFileType)
  32. path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{Filters: []runtime.FileFilter{
  33. FileType[defaulFileType],
  34. },
  35. DefaultFilename: defaultFileName,
  36. DefaultDirectory: defaultDirectory,
  37. })
  38. if err != nil {
  39. qu.Debug(err.Error())
  40. return ""
  41. }
  42. return path
  43. }
  44. // SelectOpenFilePath
  45. func (a *App) SelectOpenFilePath() string {
  46. path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{Filters: []runtime.FileFilter{
  47. {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  48. }})
  49. if err != nil {
  50. qu.Debug(err.Error())
  51. return ""
  52. }
  53. return path
  54. }
  55. // RunExportExcelFile 数据集导出到excel文件中
  56. func (a *App) RunExportExcelFile(filepath, code string, currentResult *list.List) error {
  57. qu.Debug("filepath---", filepath)
  58. f := excelize.NewFile()
  59. defer f.Close()
  60. f.SetCellStr("Sheet1", "A1", "站点")
  61. f.SetCellStr("Sheet1", "B1", "栏目")
  62. f.SetCellStr("Sheet1", "C1", "爬虫")
  63. //写入数据
  64. f.SetCellStr("Sheet1", "D1", "标题")
  65. f.SetCellStr("Sheet1", "E1", "链接")
  66. f.SetCellStr("Sheet1", "F1", "发布单位")
  67. f.SetCellStr("Sheet1", "G1", "发布时间")
  68. f.SetCellStr("Sheet1", "H1", "正文")
  69. f.SetCellStr("Sheet1", "I1", "附件")
  70. i := 0
  71. for el := currentResult.Front(); el != nil; el = el.Next() {
  72. r, _ := el.Value.(*be.ResultItem)
  73. //写入站点信息
  74. iStr := strconv.Itoa(i + 2)
  75. f.SetCellStr("Sheet1", "A"+iStr, r.Site)
  76. f.SetCellStr("Sheet1", "B"+iStr, r.Channel)
  77. f.SetCellStr("Sheet1", "C"+iStr, code)
  78. //写入数据
  79. f.SetCellStr("Sheet1", "D"+iStr, r.Title)
  80. f.SetCellStr("Sheet1", "E"+iStr, r.Href)
  81. f.SetCellStr("Sheet1", "F"+iStr, r.PublishUnit)
  82. f.SetCellStr("Sheet1", "G"+iStr, r.ListPubTime)
  83. f.SetCellStr("Sheet1", "H"+iStr, r.Content)
  84. f.SetCellStr("Sheet1", "I"+iStr, "")
  85. if len(r.AttachLinks) > 0 {
  86. bs, err := json.Marshal(r.AttachLinks)
  87. if err == nil {
  88. f.SetCellStr("Sheet1", "I"+iStr, string(bs))
  89. }
  90. }
  91. i += 1
  92. }
  93. err := f.SaveAs(filepath)
  94. if err != nil {
  95. return err
  96. }
  97. return nil
  98. }
  99. func (a *App) RunExportJsonFile(filepath, code string, currentResult *list.List) error {
  100. qu.Debug("filepath---", filepath)
  101. var result []map[string]interface{}
  102. for el := currentResult.Front(); el != nil; el = el.Next() {
  103. r, _ := el.Value.(*be.ResultItem)
  104. rmap := map[string]interface{}{
  105. "site": r.Site,
  106. "channel": r.Channel,
  107. "code": code,
  108. "title": r.Title,
  109. "href": r.Href,
  110. "publishdept": r.PublishUnit,
  111. "publishtime": r.ListPubTime,
  112. "detail": r.Content,
  113. "attachLinks": r.AttachLinks,
  114. }
  115. result = append(result, rmap)
  116. }
  117. jsonData, err := json.MarshalIndent(result, "", " ")
  118. if err != nil {
  119. return err
  120. }
  121. fo, err := os.Create(filepath)
  122. if err != nil {
  123. return err
  124. }
  125. defer fo.Close()
  126. if _, err := fo.Write(jsonData); err != nil {
  127. return fmt.Errorf("failed to write data to file: %w", err)
  128. }
  129. return nil
  130. }
  131. // RunExportEpubFile 导出epub文件
  132. func (a *App) RunExportEpubFile(bookname, filepath string, currentResult *list.List) error {
  133. qu.Debug("filepath---", filepath)
  134. output := epub.NewEpub(bookname)
  135. output.SetTitle(bookname)
  136. output.SetDescription(bookname)
  137. output.SetAuthor("unknow")
  138. i := 1
  139. for el := currentResult.Front(); el != nil; el = el.Next() {
  140. art, _ := el.Value.(*be.ResultItem)
  141. body := "<h2>" + art.Title + "</h2><p>" + strings.Join(strings.Split(art.Content, "\n"), "</p><p>") + "</p>"
  142. output.AddSection(body, art.Title, fmt.Sprintf("%06d.xhtml", i+1), "")
  143. i += 1
  144. }
  145. fo, err := os.Create(filepath)
  146. if err != nil {
  147. a.Dispatch("debug_event", err.Error())
  148. }
  149. output.WriteTo(fo)
  150. fo.Close()
  151. return nil
  152. }
  153. // 杀死所有chrome进程
  154. func (a *App) KillAllChrome() string {
  155. killChrome()
  156. return "ok"
  157. }
  158. // 杀死chrome进程
  159. func killChrome() {
  160. // 根据操作系统选择不同的命令
  161. var cmd *exec.Cmd
  162. qu.Debug("电脑系统:", sysruntim.GOOS)
  163. if sysruntim.GOOS == "windows" {
  164. // 在Windows上使用taskkill命令
  165. cmd = exec.Command("taskkill", "/F", "/IM", "chrome.exe")
  166. } else {
  167. // 在类Unix系统上使用pkill命令
  168. cmd = exec.Command("pkill", "-f", "chrome")
  169. }
  170. // 执行命令
  171. err := cmd.Run()
  172. if err != nil {
  173. log.Println("Error killing process:", err)
  174. return
  175. }
  176. log.Println("Chrome process killed successfully.")
  177. }