app.go 5.1 KB

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