From 73b1f71fa6832e692a6637f72553a99bc5400bca Mon Sep 17 00:00:00 2001 From: mikuta0407 Date: Wed, 1 May 2024 17:49:49 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=87=E3=83=BC=E3=83=A2=E3=83=B3=E3=83=A2?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/daemon.go | 28 +++++++++++++++++++++++ internal/core/daemon.go | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 cmd/daemon.go create mode 100644 internal/core/daemon.go diff --git a/cmd/daemon.go b/cmd/daemon.go new file mode 100644 index 0000000..e311030 --- /dev/null +++ b/cmd/daemon.go @@ -0,0 +1,28 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "fmt" + + "github.com/mikuta0407/kuchihira-bot/internal/core" + "github.com/spf13/cobra" +) + +// daemonCmd represents the daemon command +var daemonCmd = &cobra.Command{ + Use: "daemon", + Short: "post kuchiwohiraku new item in daemon mode", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("daemon called") + if err := core.DaemonStart(debug); err != nil { + fmt.Println(err) + } + }, +} + +func init() { + rootCmd.AddCommand(daemonCmd) + daemonCmd.Flags().BoolVar(&debug, "debug", false, "debug(default: false)") +} diff --git a/internal/core/daemon.go b/internal/core/daemon.go new file mode 100644 index 0000000..d3cad33 --- /dev/null +++ b/internal/core/daemon.go @@ -0,0 +1,50 @@ +package core + +import ( + "errors" + "fmt" + "time" + + "github.com/mikuta0407/kuchihira-bot/internal/discord" +) + +func DaemonStart(isDebug bool) error { + if isDebug { + fmt.Println("==== DRY RUN MODE!!! ====") + } + + // RSS取得 + var items []Item + var latestGUID string + var err error + for { + items, latestGUID, err = getNewPost(kuchihiraCfg.RSSURL) + if err != nil && !errors.Is(err, ErrorNoUpdate) { + // discord.DoPost(discordCfg, "Failed: Get RSS, "+err.Error(), isDebug) + fmt.Println(err) + } + + if latestGUID != "" { + if len(items) != 1 { + discord.DoPost(discordCfg, "Warning: Multi publish detected", isDebug) + } + if err := saveLastGUID(latestGUID); err != nil { + discord.DoPost(discordCfg, "Failed: saveLastGUID: "+latestGUID, isDebug) + } + + for _, v := range items { + if err := post(v, isDebug); err != nil { + return err + } + if len(items) != 1 { + time.Sleep(time.Second * 2) + } + } + + } + time.Sleep(time.Second * 20) + + } + + return nil +}