|
@@ -2,10 +2,25 @@
|
|
|
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"
|
|
|
+ "os"
|
|
|
+ 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)
|
|
@@ -13,12 +28,10 @@ func (a *App) Dispatch(event string, data interface{}) error {
|
|
|
}
|
|
|
|
|
|
// SelectSaveFilePath
|
|
|
-func (a *App) SelectSaveFilePath(defaultDirectory, defaultFileName string) string {
|
|
|
- qu.Debug("导出文件位置:", defaultDirectory, defaultFileName)
|
|
|
+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{
|
|
|
- {Pattern: "*.epub", DisplayName: "epub file *.epub"},
|
|
|
- {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
|
|
|
- {Pattern: "*.json", DisplayName: "json file *.json"},
|
|
|
+ FileType[defaulFileType],
|
|
|
},
|
|
|
DefaultFilename: defaultFileName,
|
|
|
DefaultDirectory: defaultDirectory,
|
|
@@ -41,3 +54,104 @@ func (a *App) SelectOpenFilePath() string {
|
|
|
}
|
|
|
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
|
|
|
+}
|