app.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. //脚本目录
  52. qlmScriptDir := baseDir + "/special/script"
  53. qlmLogsDir := baseDir + "/special/logs"
  54. if _, err := os.Stat(qlmScriptDir); err != nil {
  55. os.MkdirAll(qlmScriptDir, 0777)
  56. }
  57. if _, err := os.Stat(qlmLogsDir); err != nil {
  58. os.MkdirAll(qlmLogsDir, 0777)
  59. }
  60. var dbfile = baseDir + "/spider.dat"
  61. qu.Debug("db file:", dbfile)
  62. db = bdb.NewSpiderDb(dbfile, a)
  63. bdb.Db = db
  64. vm = bvm.NewVM(attachesDir, a)
  65. glvm = script.NewGLVM(qlmScriptDir, qlmLogsDir, a)
  66. ws = bws.NewWebService(db, a, currentSpiderConfig)
  67. //
  68. go ws.RunHttpServe()
  69. }
  70. // destory
  71. func (a *App) destory(ctx context.Context) {
  72. db.Close()
  73. }
  74. //var (
  75. // //db *SpiderDb
  76. // exitCh chan bool
  77. //)
  78. // App struct
  79. //type App struct {
  80. // ctx context.Context
  81. //}
  82. // NewApp creates a new App application struct
  83. //func NewApp() *App {
  84. // return &App{}
  85. //}
  86. // startup is called when the app starts. The context is saved
  87. // so we can call the runtime methods
  88. //func (a *App) startup(ctx context.Context) {
  89. // a.ctx = ctx
  90. // db = NewSpiderDb("./data.db")
  91. //}
  92. // destory
  93. //func (a *App) destory(ctx context.Context) {
  94. // db.Close()
  95. //}
  96. // Greet returns a greeting for the given name
  97. //func (a *App) Greet(name string) string {
  98. // return fmt.Sprintf("Hello %s, It's show time!", name)
  99. //}
  100. //// LoadSpiderConfigAll,带分页
  101. //func (a *App) LoadSpiderConfigAll(pageSize, pageNo int) []*SpiderConfig {
  102. // return db.LoadAll()
  103. //}
  104. //
  105. //// LoadSpiderConfigAll,带分页
  106. //func (a *App) SaveOrUpdateSpiderConfig(sc *SpiderConfig) string {
  107. // db.SaveOrUpdate(sc)
  108. // return "ok"
  109. //}
  110. // SwitchSpiderConfig
  111. //func (a *App) SwitchSpiderConfig(code string) string {
  112. // qu.Debug("切换当前默认爬虫配置:", code)
  113. // db.Switch(code)
  114. // return "ok"
  115. //}
  116. // SwitchSpiderConfig
  117. //func (a *App) ViewCurrentSpiderConfig() *SpiderConfig {
  118. // return currentSpiderConfig
  119. //}
  120. // SwitchSpiderConfig
  121. //func (a *App) DeleteSpiderConfig(code string) string {
  122. // db.Delete(code)
  123. // return "ok"
  124. //}
  125. // 推送消息
  126. //func (a *App) pushMessage(event string, data interface{}) {
  127. // runtime.EventsEmit(a.ctx, event, data)
  128. //}
  129. // 调试爬虫
  130. //func (a *App) DebugSpider(url string, listDealy int64, contentDelay int64, headless bool, showImage bool, proxyServe string) {
  131. // exitCh = make(chan bool, 1)
  132. // RunSpider(url, listDealy, contentDelay, headless, showImage, proxyServe, exitCh)
  133. //}
  134. // 停止调试
  135. //func (a *App) StopDebugSpider() string {
  136. // defer func() {
  137. // if err := recover(); err != nil {
  138. // qu.Debug(err)
  139. // }
  140. // }()
  141. // exitCh <- true
  142. // return "ok"
  143. //}
  144. // 查看所有结果
  145. //func (a *App) ViewResultItemAll() ResultItems {
  146. // return currentResult
  147. //}
  148. //
  149. //// ExportEpubFile
  150. //func (a *App) ExportEpubFile(filepath string) string {
  151. // ExportEpubFile(filepath)
  152. // return "ok"
  153. //}
  154. // SelectSaveFilePath
  155. //func (a *App) SelectSaveFilePath() string {
  156. // path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{Filters: []runtime.FileFilter{
  157. // {Pattern: "*.epub", DisplayName: "epub file *.epub"},
  158. // {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  159. // {Pattern: "*.json", DisplayName: "json file *.json"},
  160. // }})
  161. // if err != nil {
  162. // qu.Debug(err.Error())
  163. // return ""
  164. // }
  165. // return path
  166. //}
  167. // SelectOpenFilePath
  168. //func (a *App) SelectOpenFilePath() string {
  169. // path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{Filters: []runtime.FileFilter{
  170. // {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
  171. // }})
  172. // if err != nil {
  173. // qu.Debug(err.Error())
  174. // return ""
  175. // }
  176. // return path
  177. //}
  178. // ImportSpiderConfigByExcelFile 通过excel文件导入爬虫配置
  179. //func (a *App) ImportSpiderConfigByExcelFile(filepath string) string {
  180. // db.BatchImport(filepath)
  181. // return "ok"
  182. //}
  183. // 获取login状态
  184. //func (a *App) GetLoginState() bool {
  185. // return loginState
  186. //}
  187. //
  188. //func (a *App) PutLoginState(state bool) string {
  189. // loginState = state
  190. // return "ok"
  191. //}
  192. // CountYestodayArts
  193. //func (a *App) CountYestodayArts(url string, listDealy int64,
  194. // trunPageDelay int64, headless bool, showImage bool) {
  195. // exitCh = make(chan bool, 1)
  196. // CountYestodayArts(url, listDealy, trunPageDelay, headless, showImage, exitCh)
  197. //}