123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- package main
- import (
- "fmt"
- "golang.org/x/net/context"
- qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
- "os"
- rt "runtime"
- bdb "spider_creator/backend/db"
- bvm "spider_creator/backend/vm"
- bws "spider_creator/backend/webservice"
- )
- func getDownloadDir() (string, error) {
- var downloadDir string
- var err error
- switch rt.GOOS {
- case "windows":
- downloadDir = os.Getenv("USERPROFILE") + "\\Downloads"
- case "darwin":
- downloadDir = os.Getenv("HOME") + "/Downloads"
- case "linux":
- downloadDir = os.Getenv("HOME") + "/Downloads"
- default:
- err = fmt.Errorf("unsupported operating system")
- }
- return downloadDir, err
- }
- // App struct
- type App struct {
- ctx context.Context
- }
- // NewApp creates a new App application struct
- func NewApp() *App {
- return &App{}
- }
- // startup is called when the app starts. The context is saved
- // so we can call the runtime methods
- func (a *App) startup(ctx context.Context) {
- a.ctx = ctx
- if rt.GOOS == "darwin" {
- baseDir = os.Getenv("HOME") + "/Downloads"
- } else {
- baseDir = "."
- }
- //创建附件下载目录
- attachesDir = baseDir + "/spdier_attaches"
- if _, err := os.Stat(attachesDir); err != nil {
- os.MkdirAll(attachesDir, 0777)
- }
- var dbfile = baseDir + "/spider.dat"
- qu.Debug("db file:", dbfile)
- db = bdb.NewSpiderDb(dbfile, a)
- bdb.Db = db
- vm = bvm.NewVM(attachesDir, a)
- ws = bws.NewWebService(db, a, currentSpiderConfig)
- //
- go ws.RunHttpServe()
- }
- // destory
- func (a *App) destory(ctx context.Context) {
- db.Close()
- }
- //var (
- // //db *SpiderDb
- // exitCh chan bool
- //)
- // App struct
- //type App struct {
- // ctx context.Context
- //}
- // NewApp creates a new App application struct
- //func NewApp() *App {
- // return &App{}
- //}
- // startup is called when the app starts. The context is saved
- // so we can call the runtime methods
- //func (a *App) startup(ctx context.Context) {
- // a.ctx = ctx
- // db = NewSpiderDb("./data.db")
- //}
- // destory
- //func (a *App) destory(ctx context.Context) {
- // db.Close()
- //}
- // Greet returns a greeting for the given name
- //func (a *App) Greet(name string) string {
- // return fmt.Sprintf("Hello %s, It's show time!", name)
- //}
- //// LoadSpiderConfigAll,带分页
- //func (a *App) LoadSpiderConfigAll(pageSize, pageNo int) []*SpiderConfig {
- // return db.LoadAll()
- //}
- //
- //// LoadSpiderConfigAll,带分页
- //func (a *App) SaveOrUpdateSpiderConfig(sc *SpiderConfig) string {
- // db.SaveOrUpdate(sc)
- // return "ok"
- //}
- // SwitchSpiderConfig
- //func (a *App) SwitchSpiderConfig(code string) string {
- // qu.Debug("切换当前默认爬虫配置:", code)
- // db.Switch(code)
- // return "ok"
- //}
- // SwitchSpiderConfig
- //func (a *App) ViewCurrentSpiderConfig() *SpiderConfig {
- // return currentSpiderConfig
- //}
- // SwitchSpiderConfig
- //func (a *App) DeleteSpiderConfig(code string) string {
- // db.Delete(code)
- // return "ok"
- //}
- // 推送消息
- //func (a *App) pushMessage(event string, data interface{}) {
- // runtime.EventsEmit(a.ctx, event, data)
- //}
- // 调试爬虫
- //func (a *App) DebugSpider(url string, listDealy int64, contentDelay int64, headless bool, showImage bool, proxyServe string) {
- // exitCh = make(chan bool, 1)
- // RunSpider(url, listDealy, contentDelay, headless, showImage, proxyServe, exitCh)
- //}
- // 停止调试
- //func (a *App) StopDebugSpider() string {
- // defer func() {
- // if err := recover(); err != nil {
- // qu.Debug(err)
- // }
- // }()
- // exitCh <- true
- // return "ok"
- //}
- // 查看所有结果
- //func (a *App) ViewResultItemAll() ResultItems {
- // return currentResult
- //}
- //
- //// ExportEpubFile
- //func (a *App) ExportEpubFile(filepath string) string {
- // ExportEpubFile(filepath)
- // return "ok"
- //}
- // SelectSaveFilePath
- //func (a *App) SelectSaveFilePath() string {
- // 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"},
- // }})
- // if err != nil {
- // qu.Debug(err.Error())
- // return ""
- // }
- // return path
- //}
- // SelectOpenFilePath
- //func (a *App) SelectOpenFilePath() string {
- // path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{Filters: []runtime.FileFilter{
- // {Pattern: "*.xlsx", DisplayName: "excel file *.xlsx"},
- // }})
- // if err != nil {
- // qu.Debug(err.Error())
- // return ""
- // }
- // return path
- //}
- // ImportSpiderConfigByExcelFile 通过excel文件导入爬虫配置
- //func (a *App) ImportSpiderConfigByExcelFile(filepath string) string {
- // db.BatchImport(filepath)
- // return "ok"
- //}
- // 获取login状态
- //func (a *App) GetLoginState() bool {
- // return loginState
- //}
- //
- //func (a *App) PutLoginState(state bool) string {
- // loginState = state
- // return "ok"
- //}
- // CountYestodayArts
- //func (a *App) CountYestodayArts(url string, listDealy int64,
- // trunPageDelay int64, headless bool, showImage bool) {
- // exitCh = make(chan bool, 1)
- // CountYestodayArts(url, listDealy, trunPageDelay, headless, showImage, exitCh)
- //}
|