Skip to content

Commit

Permalink
Merge pull request #51 from dwarvesf/feat/news-cmd
Browse files Browse the repository at this point in the history
feat: news cmd
  • Loading branch information
baenv authored Jul 9, 2024
2 parents fca1179 + f86e8ac commit 0bd7ef2
Show file tree
Hide file tree
Showing 31 changed files with 467 additions and 9 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/shopspring/decimal v1.3.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.14.0
github.com/vartanbeno/go-reddit/v2 v2.0.1
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/text v0.14.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -34,6 +35,7 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -57,6 +59,7 @@ require (
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sys v0.16.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -258,6 +261,8 @@ github.com/temoto/robotstxt v1.1.2/go.mod h1:+1AmkuG3IYkh1kv0d2qEB9Le88ehNO0zwOr
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/vartanbeno/go-reddit/v2 v2.0.1 h1:P6ITpf5YHjdy7DHZIbUIDn/iNAoGcEoDQnMa+L4vutw=
github.com/vartanbeno/go-reddit/v2 v2.0.1/go.mod h1:758/S10hwZSLm43NPtwoNQdZFSg3sjB5745Mwjb0ANI=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -363,6 +368,8 @@ golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 h1:nt+Q6cXKz4MosCSpnbMtqiQ8Oz0pxTef2B4Vca2lvfk=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
12 changes: 12 additions & 0 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/dwarvesf/fortress-discord/pkg/adapter/fortress"
"github.com/dwarvesf/fortress-discord/pkg/adapter/mochi"
"github.com/dwarvesf/fortress-discord/pkg/adapter/openai"
"github.com/dwarvesf/fortress-discord/pkg/adapter/reddit"
"github.com/dwarvesf/fortress-discord/pkg/config"
"github.com/dwarvesf/fortress-discord/pkg/logger"
)
Expand All @@ -16,14 +17,21 @@ type subAdapter struct {
Fortress fortress.FortressAdapter
Mochi mochi.MochiAdapter
OpenAI openai.OpenAIAdapter
Reddit reddit.Adapter
}

func New(cfg *config.Config, l logger.Logger) IAdapter {
reddit, err := reddit.New(cfg, l)
if err != nil {
l.Error(err, "can't create reddit adapter")
}

return &Adapter{
subAdapter: subAdapter{
Fortress: fortress.New(cfg.Endpoint.Fortress, cfg.ApiServer.APIKey),
Mochi: mochi.New(cfg.Endpoint.Mochi),
OpenAI: openai.New(cfg.OpenAI.APIKey),
Reddit: reddit,
},
}
}
Expand All @@ -40,3 +48,7 @@ func (a *Adapter) Mochi() mochi.MochiAdapter {
func (a *Adapter) OpenAI() openai.OpenAIAdapter {
return a.subAdapter.OpenAI
}

func (a *Adapter) Reddit() reddit.Adapter {
return a.subAdapter.Reddit
}
2 changes: 2 additions & 0 deletions pkg/adapter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"github.com/dwarvesf/fortress-discord/pkg/adapter/fortress"
"github.com/dwarvesf/fortress-discord/pkg/adapter/mochi"
"github.com/dwarvesf/fortress-discord/pkg/adapter/openai"
"github.com/dwarvesf/fortress-discord/pkg/adapter/reddit"
)

type IAdapter interface {
Fortress() fortress.FortressAdapter
Mochi() mochi.MochiAdapter
OpenAI() openai.OpenAIAdapter
Reddit() reddit.Adapter
}
11 changes: 11 additions & 0 deletions pkg/adapter/reddit/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package reddit

import (
"context"

"github.com/vartanbeno/go-reddit/v2/reddit"
)

type Adapter interface {
FetchNewsBySubreddit(ctx context.Context, sub string) ([]reddit.Post, []reddit.Post, error)
}
75 changes: 75 additions & 0 deletions pkg/adapter/reddit/news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package reddit

import (
"context"
"sort"
"time"

"github.com/vartanbeno/go-reddit/v2/reddit"
)

func (a adapter) FetchNewsBySubreddit(ctx context.Context, sub string) ([]reddit.Post, []reddit.Post, error) {
dayAgo := time.Now().Add(-24 * time.Hour)

newPosts, _, err := a.client.Subreddit.NewPosts(ctx, sub, &reddit.ListOptions{
Limit: 50,
})
if err != nil {
return nil, nil, err
}

newPostsMap := make(map[string]reddit.Post)
for _, post := range newPosts {
if post.Created.Before(dayAgo) {
continue
}

newPostsMap[post.ID] = *post
}

risingPosts, _, err := a.client.Subreddit.RisingPosts(ctx, sub, &reddit.ListOptions{
Limit: 50,
})
if err != nil {
return nil, nil, err
}

risingPostsMap := make(map[string]reddit.Post)
for _, post := range risingPosts {
if post.Created.Before(dayAgo) {
continue
}

risingPostsMap[post.ID] = *post
}

popularPosts := make([]reddit.Post, 0)
emergingPosts := make([]reddit.Post, 0)
for _, post := range newPostsMap {
if _, ok := risingPostsMap[post.ID]; !ok {
emergingPosts = append(emergingPosts, post)
continue
}

popularPosts = append(popularPosts, post)
}

sort.Slice(popularPosts, func(i, j int) bool {
return popularPosts[i].NumberOfComments > (popularPosts[j].NumberOfComments)
})

if len(popularPosts) > 10 {
emergingPosts = append(emergingPosts, popularPosts[10:]...)
popularPosts = popularPosts[:10]
}

sort.Slice(emergingPosts, func(i, j int) bool {
return emergingPosts[i].Created.Time.After(emergingPosts[j].Created.Time)
})

if len(emergingPosts) > 10 {
emergingPosts = emergingPosts[:10]
}

return popularPosts, emergingPosts, nil
}
51 changes: 51 additions & 0 deletions pkg/adapter/reddit/reddit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package reddit

import (
"fmt"

"github.com/dwarvesf/fortress-discord/pkg/config"
"github.com/dwarvesf/fortress-discord/pkg/logger"
"github.com/vartanbeno/go-reddit/v2/reddit"
)

type adapter struct {
client *reddit.Client
}

func New(cfg *config.Config, l logger.Logger) (Adapter, error) {
clientID := cfg.Reddit.ClientID
if clientID == "" {
l.Warn("reddit client id is empty")
}

clientSecret := cfg.Reddit.ClientSecret
if clientSecret == "" {
l.Warn("reddit client secret is empty")
}

username := cfg.Reddit.Username
if username == "" {
l.Warn("reddit username is empty")
}

password := cfg.Reddit.Password
if password == "" {
l.Warn("reddit password is empty")
}

auth := reddit.Credentials{
ID: clientID,
Secret: clientSecret,
Username: username,
Password: password,
}

client, err := reddit.NewClient(auth, reddit.WithUserAgent("fortress-bot"))
if err != nil {
return nil, fmt.Errorf("create reddit client failed: %w", err)
}

return &adapter{
client: client,
}, nil
}
14 changes: 14 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ type Config struct {
ApiServer ApiServer
OpenAI OpenAI
Discord Discord
Reddit Reddit

Endpoint Endpoint
}

type Reddit struct {
ClientID string
ClientSecret string
Username string
Password string
}

type OpenAI struct {
APIKey string
}
Expand Down Expand Up @@ -78,6 +86,12 @@ func Generate(v ENV) *Config {
},
WhiteListedChannels: v.GetString("DISCORD_WHITELISTED_CHANNELS"),
},
Reddit: Reddit{
ClientID: v.GetString("REDDIT_CLIENT_ID"),
ClientSecret: v.GetString("REDDIT_CLIENT_SECRET"),
Username: v.GetString("REDDIT_USERNAME"),
Password: v.GetString("REDDIT_PASSWORD"),
},
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/discord/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/dwarvesf/fortress-discord/pkg/discord/command/milestone"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/mma"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/new"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/news"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/profile"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/radar"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/staff"
Expand Down Expand Up @@ -83,6 +84,7 @@ func New(cfg *config.Config, l logger.Logger, svc service.Servicer, view view.Vi
trend.New(l, svc, view),
salary.New(l, svc, view),
withdraw.New(l, svc, view),
news.New(l, svc, view, cfg),
})

return cmd
Expand Down
52 changes: 52 additions & 0 deletions pkg/discord/command/news/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package news

import (
"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (c command) Prefix() []string {
return []string{"news"}
}

// Execute is where we handle logic for each command
func (c command) Execute(message *model.DiscordMessage) error {
// default command for only 1 args input from user, c.g `?earn`
if len(message.ContentArgs) == 1 {
return c.DefaultCommand(message)
}

// handle command for 2 args input from user, c.g `?earn list`
switch message.ContentArgs[1] {
case "help":
return c.Help(message)
case "reddit":
switch len(message.ContentArgs) {
case 2:
return c.DefaultCommand(message)
default:
return c.Reddit(message, message.ContentArgs[2])
}
}

return nil
}

func (c command) Name() string {
return "News Command"
}

func (c command) Help(message *model.DiscordMessage) error {
return c.view.News().Help(message)
}

func (c command) DefaultCommand(message *model.DiscordMessage) error {
return c.Help(message)
}

func (c command) PermissionCheck(message *model.DiscordMessage) (bool, []string) {
return true, []string{}
}

func (c command) ChannelPermissionCheck(message *model.DiscordMessage) bool {
return true
}
9 changes: 9 additions & 0 deletions pkg/discord/command/news/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package news

import (
"github.com/dwarvesf/fortress-discord/pkg/discord/base"
)

type Commander interface {
base.TextCommander
}
24 changes: 24 additions & 0 deletions pkg/discord/command/news/news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package news

import (
"github.com/dwarvesf/fortress-discord/pkg/config"
"github.com/dwarvesf/fortress-discord/pkg/discord/service"
"github.com/dwarvesf/fortress-discord/pkg/discord/view"
"github.com/dwarvesf/fortress-discord/pkg/logger"
)

type command struct {
L logger.Logger
svc service.Servicer
view view.Viewer
cfg *config.Config
}

func New(l logger.Logger, svc service.Servicer, view view.Viewer, cfg *config.Config) Commander {
return &command{
L: l,
svc: svc,
view: view,
cfg: cfg,
}
}
Loading

0 comments on commit 0bd7ef2

Please sign in to comment.