-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e00dff7
commit 73b1f71
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |