<commit_before>package extensions

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/pawelszydlo/papa-bot"
	"github.com/pawelszydlo/papa-bot/events"
	"github.com/pawelszydlo/papa-bot/utils"
	"math/rand"
	"strings"
	"text/template"
	"time"
)

/*
ExtensionReddit - extension for getting link information from reddit.com.

This extension will automatically try to fetch Reddit information for each URL posted. It will also post a random
link from one of the hot reddits once per day.

Used custom variables:
- interestingReddits - space separated list of subreddits to use for hot links.
- redditDaily - if set, show daily interesting link.
*/
type ExtensionReddit struct {
	announced     map[string]bool
	announcedLive map[string]bool
	Texts         *extensionRedditTexts
	bot           *papaBot.Bot
}

type extensionRedditTexts struct {
	TempRedditAnnounce *template.Template
	TempRedditDaily    *template.Template
	TempRedditBreaking *template.Template
}

// Reddit structs.
type redditPostData struct {
	Id        string
	Subreddit string
	Author    string
	Domain    string
	Score     int
	Comments  int `json:"num_comments"`
	Title     string
	Url       string
	Created   float64
	Stickied  bool
}
type redditListing struct {
	Error int
	Kind  string
	Data  struct {
		Title    string
		Id       string
		Children []struct{ Data redditPostData }
	}
}

func (postData *redditPostData) toStrings(ext *ExtensionReddit) map[string]string {
	// Try to shorten the url if it is a self post.
	if strings.HasPrefix(postData.Domain, "self.") {
		postData.Url = "http://redd.it/" + postData.Id
	}
	return map[string]string{
		"id":           postData.Id,
		"created":      ext.bot.Humanizer.TimeDiffNow(time.Unix(int64(postData.Created), 0), false),
		"author":       postData.Author,
		"subreddit":    postData.Subreddit,
		"score":        ext.bot.Humanizer.PrefixFast(float64(postData.Score)),
		"comments_url": "http://redd.it/" + postData.Id,
		"comments":     fmt.Sprintf("%d", postData.Comments),
		"title":        postData.Title,
		"url":          postData.Url,
	}
}

// getRedditListing fetches a reddit listing data.
func (ext *ExtensionReddit) getRedditListing(url string, listing *redditListing) error {
	// Reddit API doesn't like it when you pretend to be someone else.
	headers := map[string]string{"User-Agent": "PapaBot version " + papaBot.Version}
	err, _, body := ext.bot.GetPageBody(url, headers)
	if err != nil {
		return err
	}

	// Decode JSON.
	if err := json.Unmarshal(body, &listing); err != nil {<commit_msg>Use UTC creation time in reddit.<commit_after>package extensions

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/pawelszydlo/papa-bot"
	"github.com/pawelszydlo/papa-bot/events"
	"github.com/pawelszydlo/papa-bot/utils"
	"math/rand"
	"strings"
	"text/template"
	"time"
)

/*
ExtensionReddit - extension for getting link information from reddit.com.

This extension will automatically try to fetch Reddit information for each URL posted. It will also post a random
link from one of the hot reddits once per day.

Used custom variables:
- interestingReddits - space separated list of subreddits to use for hot links.
- redditDaily - if set, show daily interesting link.
*/
type ExtensionReddit struct {
	announced     map[string]bool
	announcedLive map[string]bool
	Texts         *extensionRedditTexts
	bot           *papaBot.Bot
}

type extensionRedditTexts struct {
	TempRedditAnnounce *template.Template
	TempRedditDaily    *template.Template
	TempRedditBreaking *template.Template
}

// Reddit structs.
type redditPostData struct {
	Id        string
	Subreddit string
	Author    string
	Domain    string
	Score     int
	Comments  int `json:"num_comments"`
	Title     string
	Url       string
	Created_utc   float64
	Stickied  bool
}
type redditListing struct {
	Error int
	Kind  string
	Data  struct {
		Title    string
		Id       string
		Children []struct{ Data redditPostData }
	}
}

func (postData *redditPostData) toStrings(ext *ExtensionReddit) map[string]string {
	// Try to shorten the url if it is a self post.
	if strings.HasPrefix(postData.Domain, "self.") {
		postData.Url = "http://redd.it/" + postData.Id
	}
	return map[string]string{
		"id":           postData.Id,
		"created":      ext.bot.Humanizer.TimeDiffNow(time.Unix(int64(postData.Created_utc), 0), false),
		"author":       postData.Author,
		"subreddit":    postData.Subreddit,
		"score":        ext.bot.Humanizer.PrefixFast(float64(postData.Score)),
		"comments_url": "http://redd.it/" + postData.Id,
		"comments":     fmt.Sprintf("%d", postData.Comments),
		"title":        postData.Title,
		"url":          postData.Url,
	}
}

// getRedditListing fetches a reddit listing data.
func (ext *ExtensionReddit) getRedditListing(url string, listing *redditListing) error {
	// Reddit API doesn't like it when you pretend to be someone else.
	headers := map[string]string{"User-Agent": "PapaBot version " + papaBot.Version}
	err, _, body := ext.bot.GetPageBody(url, headers)
	if err != nil {
		return err
	}

	// Decode JSON.
	if err := json.Unmarshal(body, &listing); err != nil {
{'fraction_non_alphanumeric': 0.1050461417632044, 'fraction_numerical': 0.0027488709994109564, 'mean_word_length': 3.560429722470904, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1960123', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1680, 'n_words': 463}