123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package main
- import (
- "context"
- "fmt"
- "log"
- "github.com/wailsapp/wails/v2/pkg/runtime"
- )
- 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 {
- log.Println("切换当前默认爬虫配置:", 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 {
- log.Println(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 {
- log.Println(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 {
- log.Println(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)
- }
|