app.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package main
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "log"
  6. "os"
  7. rt "runtime"
  8. bdb "spidercreator/backend/db"
  9. bvm "spidercreator/backend/vm"
  10. bws "spidercreator/backend/webservice"
  11. )
  12. func getDownloadDir() (string, error) {
  13. var downloadDir string
  14. var err error
  15. switch rt.GOOS {
  16. case "windows":
  17. downloadDir = os.Getenv("USERPROFILE") + "\\Downloads"
  18. case "darwin":
  19. downloadDir = os.Getenv("HOME") + "/Downloads"
  20. case "linux":
  21. downloadDir = os.Getenv("HOME") + "/Downloads"
  22. default:
  23. err = fmt.Errorf("unsupported operating system")
  24. }
  25. return downloadDir, err
  26. }
  27. // App struct
  28. type App struct {
  29. ctx context.Context
  30. }
  31. // NewApp creates a new App application struct
  32. func NewApp() *App {
  33. return &App{}
  34. }
  35. // startup is called when the app starts. The context is saved
  36. // so we can call the runtime methods
  37. func (a *App) startup(ctx context.Context) {
  38. a.ctx = ctx
  39. if rt.GOOS == "darwin" {
  40. baseDir = os.Getenv("HOME") + "/Downloads"
  41. } else {
  42. baseDir = "."
  43. }
  44. //创建附件下载目录
  45. attachesDir = baseDir + "/spdier_attaches"
  46. if _, err := os.Stat(attachesDir); err != nil {
  47. os.MkdirAll(attachesDir, 0777)
  48. }
  49. var dbfile = baseDir + "/spider.dat"
  50. log.Println("db file:", dbfile)
  51. db = bdb.NewSpiderDb(dbfile, a)
  52. bdb.Db = db
  53. vm = bvm.NewVM(attachesDir, a)
  54. ws = bws.NewWebService(db, a, currentSpiderConfig)
  55. //
  56. go ws.RunHttpServe()
  57. }
  58. // destory
  59. func (a *App) destory(ctx context.Context) {
  60. db.Close()
  61. }
  62. //var (
  63. // //db *SpiderDb
  64. // exitCh chan bool
  65. //)
  66. // App struct
  67. //type App struct {
  68. // ctx context.Context
  69. //}
  70. // NewApp creates a new App application struct
  71. //func NewApp() *App {
  72. // return &App{}
  73. //}
  74. // startup is called when the app starts. The context is saved
  75. // so we can call the runtime methods
  76. //func (a *App) startup(ctx context.Context) {
  77. // a.ctx = ctx
  78. // db = NewSpiderDb("./data.db")
  79. //}
  80. // destory
  81. //func (a *App) destory(ctx context.Context) {
  82. // db.Close()
  83. //}
  84. // Greet returns a greeting for the given name
  85. //func (a *App) Greet(name string) string {
  86. // return fmt.Sprintf("Hello %s, It's show time!", name)
  87. //}
  88. //// LoadSpiderConfigAll,带分页
  89. //func (a *App) LoadSpiderConfigAll(pageSize, pageNo int) []*SpiderConfig {
  90. // return db.LoadAll()
  91. //}
  92. //
  93. //// LoadSpiderConfigAll,带分页
  94. //func (a *App) SaveOrUpdateSpiderConfig(sc *SpiderConfig) string {
  95. // db.SaveOrUpdate(sc)
  96. // return "ok"
  97. //}
  98. // SwitchSpiderConfig
  99. //func (a *App) SwitchSpiderConfig(code string) string {
  100. // log.Println("切换当前默认爬虫配置:", code)
  101. // db.Switch(code)
  102. // return "ok"
  103. //}
  104. // SwitchSpiderConfig
  105. //func (a *App) ViewCurrentSpiderConfig() *SpiderConfig {
  106. // return currentSpiderConfig
  107. //}
  108. // SwitchSpiderConfig
  109. //func (a *App) DeleteSpiderConfig(code string) string {
  110. // db.Delete(code)
  111. // return "ok"
  112. //}
  113. // 推送消息
  114. //func (a *App) pushMessage(event string, data interface{}) {
  115. // runtime.EventsEmit(a.ctx, event, data)
  116. //}
  117. // 调试爬虫
  118. //func (a *App) DebugSpider(url string, listDealy int64, contentDelay int64, headless bool, showImage bool, proxyServe string) {
  119. // exitCh = make(chan bool, 1)
  120. // RunSpider(url, listDealy, contentDelay, headless, showImage, proxyServe, exitCh)
  121. //}
  122. // 停止调试
  123. //func (a *App) StopDebugSpider() string {
  124. // defer func() {
  125. // if err := recover(); err != nil {
  126. // log.Println(err)
  127. // }
  128. // }()
  129. // exitCh <- true
  130. // return "ok"
  131. //}
  132. // 查看所有结果
  133. //func (a *App) ViewResultItemAll() ResultItems {
  134. // return currentResult
  135. //}
  136. //
  137. //// ExportEpubFile
  138. //func (a *App) ExportEpubFile(filepath string) string {
  139. // ExportEpubFile(filepath)
  140. // return "ok"
  141. //}
  142. // SelectSaveFilePath
  143. //func (a *App) SelectSaveFilePath() string {
  144. // path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{Filters: []runtime.FileFilter{
  145. // {Pattern: "*.epub", DisplayName: "epub file *.epub"},
  146. // {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  147. // {Pattern: "*.json", DisplayName: "json file *.json"},
  148. // }})
  149. // if err != nil {
  150. // log.Println(err.Error())
  151. // return ""
  152. // }
  153. // return path
  154. //}
  155. // SelectOpenFilePath
  156. //func (a *App) SelectOpenFilePath() string {
  157. // path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{Filters: []runtime.FileFilter{
  158. // {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  159. // }})
  160. // if err != nil {
  161. // log.Println(err.Error())
  162. // return ""
  163. // }
  164. // return path
  165. //}
  166. // ImportSpiderConfigByExcelFile 通过excel文件导入爬虫配置
  167. //func (a *App) ImportSpiderConfigByExcelFile(filepath string) string {
  168. // db.BatchImport(filepath)
  169. // return "ok"
  170. //}
  171. // 获取login状态
  172. //func (a *App) GetLoginState() bool {
  173. // return loginState
  174. //}
  175. //
  176. //func (a *App) PutLoginState(state bool) string {
  177. // loginState = state
  178. // return "ok"
  179. //}
  180. // CountYestodayArts
  181. //func (a *App) CountYestodayArts(url string, listDealy int64,
  182. // trunPageDelay int64, headless bool, showImage bool) {
  183. // exitCh = make(chan bool, 1)
  184. // CountYestodayArts(url, listDealy, trunPageDelay, headless, showImage, exitCh)
  185. //}