app.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "github.com/wailsapp/wails/v2/pkg/runtime"
  7. )
  8. var (
  9. db *SpiderDb
  10. exitCh chan bool
  11. )
  12. // App struct
  13. type App struct {
  14. ctx context.Context
  15. }
  16. // NewApp creates a new App application struct
  17. func NewApp() *App {
  18. return &App{}
  19. }
  20. // startup is called when the app starts. The context is saved
  21. // so we can call the runtime methods
  22. func (a *App) startup(ctx context.Context) {
  23. a.ctx = ctx
  24. db = NewSpiderDb("./data.db")
  25. }
  26. // destory
  27. func (a *App) destory(ctx context.Context) {
  28. db.Close()
  29. }
  30. // Greet returns a greeting for the given name
  31. func (a *App) Greet(name string) string {
  32. return fmt.Sprintf("Hello %s, It's show time!", name)
  33. }
  34. // LoadSpiderConfigAll,带分页
  35. func (a *App) LoadSpiderConfigAll(pageSize, pageNo int) []*SpiderConfig {
  36. return db.LoadAll()
  37. }
  38. // LoadSpiderConfigAll,带分页
  39. func (a *App) SaveOrUpdateSpiderConfig(sc *SpiderConfig) string {
  40. db.SaveOrUpdate(sc)
  41. return "ok"
  42. }
  43. // SwitchSpiderConfig
  44. func (a *App) SwitchSpiderConfig(code string) string {
  45. log.Println("切换当前默认爬虫配置:", code)
  46. db.Switch(code)
  47. return "ok"
  48. }
  49. // SwitchSpiderConfig
  50. func (a *App) ViewCurrentSpiderConfig() *SpiderConfig {
  51. return currentSpiderConfig
  52. }
  53. // SwitchSpiderConfig
  54. func (a *App) DeleteSpiderConfig(code string) string {
  55. db.Delete(code)
  56. return "ok"
  57. }
  58. // 推送消息
  59. func (a *App) pushMessage(event string, data interface{}) {
  60. runtime.EventsEmit(a.ctx, event, data)
  61. }
  62. // 调试爬虫
  63. func (a *App) DebugSpider(url string, listDealy int64, contentDelay int64, headless bool, showImage bool, proxyServe string) {
  64. exitCh = make(chan bool, 1)
  65. RunSpider(url, listDealy, contentDelay, headless, showImage, proxyServe, exitCh)
  66. }
  67. // 停止调试
  68. func (a *App) StopDebugSpider() string {
  69. defer func() {
  70. if err := recover(); err != nil {
  71. log.Println(err)
  72. }
  73. }()
  74. exitCh <- true
  75. return "ok"
  76. }
  77. // 查看所有结果
  78. func (a *App) ViewResultItemAll() ResultItems {
  79. return currentResult
  80. }
  81. // ExportEpubFile
  82. func (a *App) ExportEpubFile(filepath string) string {
  83. ExportEpubFile(filepath)
  84. return "ok"
  85. }
  86. // SelectSaveFilePath
  87. func (a *App) SelectSaveFilePath() string {
  88. path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{Filters: []runtime.FileFilter{
  89. {Pattern: "*.epub", DisplayName: "epub file *.epub"},
  90. {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  91. {Pattern: "*.json", DisplayName: "json file *.json"},
  92. }})
  93. if err != nil {
  94. log.Println(err.Error())
  95. return ""
  96. }
  97. return path
  98. }
  99. // SelectOpenFilePath
  100. func (a *App) SelectOpenFilePath() string {
  101. path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{Filters: []runtime.FileFilter{
  102. {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  103. }})
  104. if err != nil {
  105. log.Println(err.Error())
  106. return ""
  107. }
  108. return path
  109. }
  110. // ImportSpiderConfigByExcelFile 通过excel文件导入爬虫配置
  111. func (a *App) ImportSpiderConfigByExcelFile(filepath string) string {
  112. db.BatchImport(filepath)
  113. return "ok"
  114. }
  115. // 获取login状态
  116. func (a *App) GetLoginState() bool {
  117. return loginState
  118. }
  119. func (a *App) PutLoginState(state bool) string {
  120. loginState = state
  121. return "ok"
  122. }
  123. // CountYestodayArts
  124. func (a *App) CountYestodayArts(url string, listDealy int64,
  125. trunPageDelay int64, headless bool, showImage bool) {
  126. exitCh = make(chan bool, 1)
  127. CountYestodayArts(url, listDealy, trunPageDelay, headless, showImage, exitCh)
  128. }