bind4comm.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "os"
  12. be "spider_creator/backend"
  13. "strconv"
  14. "strings"
  15. )
  16. var FileType = map[string]runtime.FileFilter{
  17. "epub": runtime.FileFilter{Pattern: "*.epub", DisplayName: "epub file *.epub"},
  18. "xlsx": runtime.FileFilter{Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  19. "json": runtime.FileFilter{Pattern: "*.json", DisplayName: "json file *.json"},
  20. }
  21. // Greet returns a greeting for the given name
  22. func (a *App) Dispatch(event string, data interface{}) error {
  23. runtime.EventsEmit(a.ctx, event, data)
  24. return nil
  25. }
  26. // SelectSaveFilePath
  27. func (a *App) SelectSaveFilePath(defaultDirectory, defaultFileName, defaulFileType string) string {
  28. qu.Debug("导出文件位置:", defaultDirectory, defaultFileName, defaulFileType)
  29. path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{Filters: []runtime.FileFilter{
  30. FileType[defaulFileType],
  31. },
  32. DefaultFilename: defaultFileName,
  33. DefaultDirectory: defaultDirectory,
  34. })
  35. if err != nil {
  36. qu.Debug(err.Error())
  37. return ""
  38. }
  39. return path
  40. }
  41. // SelectOpenFilePath
  42. func (a *App) SelectOpenFilePath() string {
  43. path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{Filters: []runtime.FileFilter{
  44. {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  45. }})
  46. if err != nil {
  47. qu.Debug(err.Error())
  48. return ""
  49. }
  50. return path
  51. }
  52. // RunExportExcelFile 数据集导出到excel文件中
  53. func (a *App) RunExportExcelFile(filepath, code string, currentResult *list.List) error {
  54. qu.Debug("filepath---", filepath)
  55. f := excelize.NewFile()
  56. defer f.Close()
  57. f.SetCellStr("Sheet1", "A1", "站点")
  58. f.SetCellStr("Sheet1", "B1", "栏目")
  59. f.SetCellStr("Sheet1", "C1", "爬虫")
  60. //写入数据
  61. f.SetCellStr("Sheet1", "D1", "标题")
  62. f.SetCellStr("Sheet1", "E1", "链接")
  63. f.SetCellStr("Sheet1", "F1", "发布单位")
  64. f.SetCellStr("Sheet1", "G1", "发布时间")
  65. f.SetCellStr("Sheet1", "H1", "正文")
  66. f.SetCellStr("Sheet1", "I1", "附件")
  67. i := 0
  68. for el := currentResult.Front(); el != nil; el = el.Next() {
  69. r, _ := el.Value.(*be.ResultItem)
  70. //写入站点信息
  71. iStr := strconv.Itoa(i + 2)
  72. f.SetCellStr("Sheet1", "A"+iStr, r.Site)
  73. f.SetCellStr("Sheet1", "B"+iStr, r.Channel)
  74. f.SetCellStr("Sheet1", "C"+iStr, code)
  75. //写入数据
  76. f.SetCellStr("Sheet1", "D"+iStr, r.Title)
  77. f.SetCellStr("Sheet1", "E"+iStr, r.Href)
  78. f.SetCellStr("Sheet1", "F"+iStr, r.PublishUnit)
  79. f.SetCellStr("Sheet1", "G"+iStr, r.ListPubTime)
  80. f.SetCellStr("Sheet1", "H"+iStr, r.Content)
  81. f.SetCellStr("Sheet1", "I"+iStr, "")
  82. if len(r.AttachLinks) > 0 {
  83. bs, err := json.Marshal(r.AttachLinks)
  84. if err == nil {
  85. f.SetCellStr("Sheet1", "I"+iStr, string(bs))
  86. }
  87. }
  88. i += 1
  89. }
  90. err := f.SaveAs(filepath)
  91. if err != nil {
  92. return err
  93. }
  94. return nil
  95. }
  96. func (a *App) RunExportJsonFile(filepath, code string, currentResult *list.List) error {
  97. qu.Debug("filepath---", filepath)
  98. var result []map[string]interface{}
  99. for el := currentResult.Front(); el != nil; el = el.Next() {
  100. r, _ := el.Value.(*be.ResultItem)
  101. rmap := map[string]interface{}{
  102. "site": r.Site,
  103. "channel": r.Channel,
  104. "code": code,
  105. "title": r.Title,
  106. "href": r.Href,
  107. "publishdept": r.PublishUnit,
  108. "publishtime": r.ListPubTime,
  109. "detail": r.Content,
  110. "attachLinks": r.AttachLinks,
  111. }
  112. result = append(result, rmap)
  113. }
  114. jsonData, err := json.MarshalIndent(result, "", " ")
  115. if err != nil {
  116. return err
  117. }
  118. fo, err := os.Create(filepath)
  119. if err != nil {
  120. return err
  121. }
  122. defer fo.Close()
  123. if _, err := fo.Write(jsonData); err != nil {
  124. return fmt.Errorf("failed to write data to file: %w", err)
  125. }
  126. return nil
  127. }
  128. // RunExportEpubFile 导出epub文件
  129. func (a *App) RunExportEpubFile(bookname, filepath string, currentResult *list.List) error {
  130. qu.Debug("filepath---", filepath)
  131. output := epub.NewEpub(bookname)
  132. output.SetTitle(bookname)
  133. output.SetDescription(bookname)
  134. output.SetAuthor("unknow")
  135. i := 1
  136. for el := currentResult.Front(); el != nil; el = el.Next() {
  137. art, _ := el.Value.(*be.ResultItem)
  138. body := "<h2>" + art.Title + "</h2><p>" + strings.Join(strings.Split(art.Content, "\n"), "</p><p>") + "</p>"
  139. output.AddSection(body, art.Title, fmt.Sprintf("%06d.xhtml", i+1), "")
  140. i += 1
  141. }
  142. fo, err := os.Create(filepath)
  143. if err != nil {
  144. a.Dispatch("debug_event", err.Error())
  145. }
  146. output.WriteTo(fo)
  147. fo.Close()
  148. return nil
  149. }