<commit_before>package plugins

import (
	"errors"
	"strings"

	"fmt"

	"time"

	"strconv"

	"github.com/Seklfreak/Robyul2/cache"
	"github.com/Seklfreak/Robyul2/helpers"
	"github.com/Seklfreak/Robyul2/models"
	"github.com/Seklfreak/Robyul2/version"
	"github.com/Sirupsen/logrus"
	"github.com/bwmarrin/discordgo"
	"github.com/dustin/go-humanize"
	rethink "github.com/gorethink/gorethink"
	"github.com/jzelinskie/geddit"
)

type redditAction func(args []string, in *discordgo.Message, out **discordgo.MessageSend) (next redditAction)

type Reddit struct{}

var (
	redditSession   *geddit.OAuthSession
	RedditUserAgent string = "geddit:Robyul:" + version.BOT_VERSION + " by /u/Seklfreak"
)

const (
	RedditBaseUrl = "https://www.reddit.com"
	RedditColor   = "ff4500"
)

func (r *Reddit) Commands() []string {
	return []string{
		"reddit",
	}
}

func (r *Reddit) Init(session *discordgo.Session) {
	var err error
	redditSession, err = geddit.NewOAuthSession(
		helpers.GetConfig().Path("reddit.id").Data().(string),
		helpers.GetConfig().Path("reddit.secret").Data().(string),
		RedditUserAgent,
		"https://robyul.chat",
	)
	helpers.Relax(err)
	err = redditSession.LoginAuth(
		helpers.GetConfig().Path("reddit.username").Data().(string),
		helpers.GetConfig().Path("reddit.password").Data().(string),
	)
	helpers.Relax(err)
	go r.checkSubredditLoop()
	r.logger().Info("Started checkSubredditLoop loop (0s)")
}

func (r *Reddit) checkSubredditLoop() {
	defer helpers.Recover()
	defer func() {
		go func() {
			r.logger().Error("The checkSubredditLoop died. Please investigate! Will be restarted in 60 seconds")
			time.Sleep(60 * time.Second)
			r.checkSubredditLoop()
		}()
	}()

	var entries []models.RedditSubredditEntry
	var bundledEntries map[string][]models.RedditSubredditEntry

	for {
		cursor, err := rethink.Table(models.RedditSubredditsTable).Run(helpers.GetDB())
		helpers.Relax(err)

		err = cursor.All(&entries)
		helpers.Relax(err)

		bundledEntries = make(map[string][]models.RedditSubredditEntry, 0)

		for _, entry := range entries {
			channel, err := helpers.GetChannel(entry.ChannelID)
			if err != nil || channel == nil || channel.ID == "" {
				r.logger().Warn(fmt.Sprintf("skipped subreddit r/%s for Channel #%s on Guild #%s: channel not found!",
					entry.SubredditName, entry.ChannelID, entry.GuildID))
				continue
			}

			if _, ok := bundledEntries[entry.SubredditName]; ok {
				bundledEntries[entry.SubredditName] = append(bundledEntries[entry.SubredditName], entry)
			} else {
				bundledEntries[entry.SubredditName] = []models.RedditSubredditEntry{entry}
			}
		}

		for subredditName, entries := range bundledEntries {
		BundleStart:
			r.logger().Info(fmt.Sprintf("checking subreddit r/%s for %d channels", subredditName, len(entries)))
			newSubmissions, err := redditSession.SubredditSubmissions(subredditName, geddit.NewSubmissions, geddit.ListingOptions{
				Limit: 10,
			})
			if err != nil {
				if strings.Contains(err.Error(), "oauth2: token expired and refresh token is not set") {
					// login when token expired
					err = redditSession.LoginAuth(
						helpers.GetConfig().Path("reddit.username").Data().(string),
						helpers.GetConfig().Path("reddit.password").Data().(string),
					)
					helpers.Relax(err)
					r.logger().Error("logged in again after token expired")
					goto BundleStart
				}
				r.logger().Error(fmt.Sprintf("updating subreddit r/%s failed: %s", subredditName, err.Error()))
				time.Sleep(2 * time.Second)
				continue
			}
			for _, entry := range entries {
				hasToBeBefore := time.Now().Add(-(time.Duration(entry.PostDelay) * time.Minute))
				hasToBeAfter := entry.LastChecked

				for _, submission := range newSubmissions {
					submissionTime := time.Unix(int64(submission.DateCreated), 0)
					if !submissionTime.Before(hasToBeBefore) || !submissionTime.After(hasToBeAfter) {
						continue
					}

					postSubmission := submission
					postChannelID := entry.ChannelID
					go func() {
						defer helpers.Recover()<commit_msg>[reddit] increases reddit posts requesting to 30 posts<commit_after>package plugins

import (
	"errors"
	"strings"

	"fmt"

	"time"

	"strconv"

	"github.com/Seklfreak/Robyul2/cache"
	"github.com/Seklfreak/Robyul2/helpers"
	"github.com/Seklfreak/Robyul2/models"
	"github.com/Seklfreak/Robyul2/version"
	"github.com/Sirupsen/logrus"
	"github.com/bwmarrin/discordgo"
	"github.com/dustin/go-humanize"
	rethink "github.com/gorethink/gorethink"
	"github.com/jzelinskie/geddit"
)

type redditAction func(args []string, in *discordgo.Message, out **discordgo.MessageSend) (next redditAction)

type Reddit struct{}

var (
	redditSession   *geddit.OAuthSession
	RedditUserAgent string = "geddit:Robyul:" + version.BOT_VERSION + " by /u/Seklfreak"
)

const (
	RedditBaseUrl = "https://www.reddit.com"
	RedditColor   = "ff4500"
)

func (r *Reddit) Commands() []string {
	return []string{
		"reddit",
	}
}

func (r *Reddit) Init(session *discordgo.Session) {
	var err error
	redditSession, err = geddit.NewOAuthSession(
		helpers.GetConfig().Path("reddit.id").Data().(string),
		helpers.GetConfig().Path("reddit.secret").Data().(string),
		RedditUserAgent,
		"https://robyul.chat",
	)
	helpers.Relax(err)
	err = redditSession.LoginAuth(
		helpers.GetConfig().Path("reddit.username").Data().(string),
		helpers.GetConfig().Path("reddit.password").Data().(string),
	)
	helpers.Relax(err)
	go r.checkSubredditLoop()
	r.logger().Info("Started checkSubredditLoop loop (0s)")
}

func (r *Reddit) checkSubredditLoop() {
	defer helpers.Recover()
	defer func() {
		go func() {
			r.logger().Error("The checkSubredditLoop died. Please investigate! Will be restarted in 60 seconds")
			time.Sleep(60 * time.Second)
			r.checkSubredditLoop()
		}()
	}()

	var entries []models.RedditSubredditEntry
	var bundledEntries map[string][]models.RedditSubredditEntry

	for {
		cursor, err := rethink.Table(models.RedditSubredditsTable).Run(helpers.GetDB())
		helpers.Relax(err)

		err = cursor.All(&entries)
		helpers.Relax(err)

		bundledEntries = make(map[string][]models.RedditSubredditEntry, 0)

		for _, entry := range entries {
			channel, err := helpers.GetChannel(entry.ChannelID)
			if err != nil || channel == nil || channel.ID == "" {
				r.logger().Warn(fmt.Sprintf("skipped subreddit r/%s for Channel #%s on Guild #%s: channel not found!",
					entry.SubredditName, entry.ChannelID, entry.GuildID))
				continue
			}

			if _, ok := bundledEntries[entry.SubredditName]; ok {
				bundledEntries[entry.SubredditName] = append(bundledEntries[entry.SubredditName], entry)
			} else {
				bundledEntries[entry.SubredditName] = []models.RedditSubredditEntry{entry}
			}
		}

		for subredditName, entries := range bundledEntries {
		BundleStart:
			r.logger().Info(fmt.Sprintf("checking subreddit r/%s for %d channels", subredditName, len(entries)))
			newSubmissions, err := redditSession.SubredditSubmissions(subredditName, geddit.NewSubmissions, geddit.ListingOptions{
				Limit: 30,
			})
			if err != nil {
				if strings.Contains(err.Error(), "oauth2: token expired and refresh token is not set") {
					// login when token expired
					err = redditSession.LoginAuth(
						helpers.GetConfig().Path("reddit.username").Data().(string),
						helpers.GetConfig().Path("reddit.password").Data().(string),
					)
					helpers.Relax(err)
					r.logger().Error("logged in again after token expired")
					goto BundleStart
				}
				r.logger().Error(fmt.Sprintf("updating subreddit r/%s failed: %s", subredditName, err.Error()))
				time.Sleep(2 * time.Second)
				continue
			}
			for _, entry := range entries {
				hasToBeBefore := time.Now().Add(-(time.Duration(entry.PostDelay) * time.Minute))
				hasToBeAfter := entry.LastChecked

				for _, submission := range newSubmissions {
					submissionTime := time.Unix(int64(submission.DateCreated), 0)
					if !submissionTime.Before(hasToBeBefore) || !submissionTime.After(hasToBeAfter) {
						continue
					}

					postSubmission := submission
					postChannelID := entry.ChannelID
					go func() {
						defer helpers.Recover()
{'fraction_non_alphanumeric': 0.14507129572225666, 'fraction_numerical': 0.005455672659640421, 'mean_word_length': 4.265013054830288, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7188708', 'n_tokens_mistral': 3109, 'n_tokens_neox': 2808, 'n_words': 506}