-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from dwarvesf/feat/news-cmd
feat: news cmd
- Loading branch information
Showing
31 changed files
with
467 additions
and
9 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
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
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
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
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,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) | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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
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
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,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 | ||
} |
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,9 @@ | ||
package news | ||
|
||
import ( | ||
"github.com/dwarvesf/fortress-discord/pkg/discord/base" | ||
) | ||
|
||
type Commander interface { | ||
base.TextCommander | ||
} |
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,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, | ||
} | ||
} |
Oops, something went wrong.