Skip to content

Commit

Permalink
Merge pull request #2 from mikuta0407/feature/daemon-mode
Browse files Browse the repository at this point in the history
Feature/daemon mode
  • Loading branch information
mikuta0407 authored May 10, 2024
2 parents 1a7b7ad + ccea324 commit 8c96d66
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 55 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
build:
go build .
go build -o kuchihira-bot

debug: build
./kuchihira-bot post --debug
build-debug:
go build -o kuchihira-bot_debug

debug: build-debug
./kuchihira-bot_debug post --debug

daemondebug: build
./kuchihira-bot_debug daemon --debug
28 changes: 28 additions & 0 deletions cmd/daemon.go
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)")
}
2 changes: 1 addition & 1 deletion cmd/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var postCmd = &cobra.Command{
// Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("post called")
if err := core.Start(debug); err != nil {
if err := core.SingleStart(debug); err != nil {
fmt.Println(err)
}
},
Expand Down
33 changes: 0 additions & 33 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"fmt"
"time"

"github.com/mikuta0407/kuchihira-bot/internal/bsky"
"github.com/mikuta0407/kuchihira-bot/internal/config"
Expand Down Expand Up @@ -41,38 +40,6 @@ func init() {
}

}
func Start(isDebug bool) error {

if isDebug {
fmt.Println("==== DRY RUN MODE!!! ====")
}

// RSS取得
items, latestGUID, err := getNewPost(kuchihiraCfg.RSSURL)
if err != nil {
discord.DoPost(discordCfg, "Failed: Get RSS, "+err.Error(), isDebug)
return err
}

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)
}
}

return nil
}

func post(item Item, isDebug bool) error {
// Twitterへの投稿
Expand Down
50 changes: 50 additions & 0 deletions internal/core/daemon.go
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
}
7 changes: 7 additions & 0 deletions internal/core/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core

import "errors"

var (
ErrorNoUpdate = errors.New("更新はありません")
)
64 changes: 64 additions & 0 deletions internal/core/single.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package core

import (
"errors"
"fmt"
"time"

"github.com/mikuta0407/kuchihira-bot/internal/discord"
)

func SingleStart(isDebug bool) error {
if isDebug {
fmt.Println("==== DRY RUN MODE!!! ====")
}

// RSS取得
var i int
var items []Item
var latestGUID string
var err error
for i = 1; i <= 360; i++ {
fmt.Println(i, "回目...")
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 != "" {
fmt.Println("取得!")
break
}
time.Sleep(time.Second * 20)
if i == 361 {
fmt.Println(ErrorNoUpdate)
discord.DoPost(discordCfg, "Failed: Get RSS, "+ErrorNoUpdate.Error(), isDebug)
return ErrorNoUpdate
}
}

if err != nil {
discord.DoPost(discordCfg, "Failed: Get RSS, "+err.Error(), isDebug)
return err
}

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)
}
}

return nil
}
24 changes: 6 additions & 18 deletions internal/core/tryget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/mikuta0407/kuchihira-bot/internal/rss"
"github.com/mmcdole/gofeed"
Expand All @@ -24,24 +23,13 @@ func getNewPost(url string) (items []Item, latestGUID string, err error) {
}
var feedItems []*gofeed.Item

var i int
for i = 1; i <= 360; i++ {
fmt.Println(i, "回目...")
feedItems, err = rss.GetAllRssFeed(url)
if err != nil {
return
}

if isExistNewPost(feedItems[0], lastGUID) {
fmt.Println("取得!")
break
}

time.Sleep(time.Second * 20)
feedItems, err = rss.GetAllRssFeed(url)
if err != nil {
return
}
if i == 361 {
fmt.Println("失敗しました")
return items, "", fmt.Errorf("更新されませんでした")

if !isExistNewPost(feedItems[0], lastGUID) {
return items, "", ErrorNoUpdate
}

items, latestGUID = extractNewPosts(feedItems, lastGUID)
Expand Down

0 comments on commit 8c96d66

Please sign in to comment.