Exemplo n.º 1
0
// checkForDuplicates checks for duplicates of the url in the database.
func (ext *ExtensionDuplicates) ProcessURL(bot *papaBot.Bot, channel, sender, msg string, urlinfo *papaBot.UrlInfo) {
	result, err := bot.Db.Query(`
		SELECT IFNULL(nick, ""), IFNULL(timestamp, datetime('now')), count(*)
		FROM urls WHERE link=? AND channel=?
		ORDER BY timestamp DESC LIMIT 1`, urlinfo.URL, channel)
	if err != nil {
		bot.Log.Warningf("Can't query the database for duplicates: %s", err)
		return
	}
	defer result.Close()

	// Announce a duplicate
	if result.Next() {
		var nick string
		var timestr string
		var count uint
		if err = result.Scan(&nick, &timestr, &count); err != nil {
			bot.Log.Warningf("Error getting duplicates: %s", err)
			return
		}
		timestamp, _ := time.Parse("2006-01-02 15:04:05", timestr)
		duplicate := ""
		// Only one duplicate
		if count == 1 {
			if bot.AreSamePeople(nick, sender) {
				nick = ext.Texts.DuplicateYou
			}
			elapsed := utils.HumanizedSince(utils.MustForceLocalTimezone(timestamp))
			duplicate = utils.Format(ext.Texts.TempDuplicateFirst, map[string]string{"nick": nick, "elapsed": elapsed})
		} else if count > 1 { // More duplicates exist
			if bot.AreSamePeople(nick, sender) {
				nick = ext.Texts.DuplicateYou
			}
			elapsed := utils.HumanizedSince(utils.MustForceLocalTimezone(timestamp))
			duplicate = utils.Format(ext.Texts.TempDuplicateMulti,
				map[string]string{"nick": nick, "elapsed": elapsed, "count": fmt.Sprintf("%d", count)})
		}
		// Only announce once per 5 minutes per link.
		if duplicate != "" && time.Since(ext.announced[channel+urlinfo.URL]) > 5*time.Minute {
			// Can we fit into the ShortInfo?
			if urlinfo.ShortInfo == "" {
				urlinfo.ShortInfo = duplicate
			} else if len(urlinfo.ShortInfo) < 50 {
				urlinfo.ShortInfo += " | " + duplicate
			} else { // Better send as separate noitce.
				bot.SendNotice(channel, duplicate)
			}
			ext.announced[channel+urlinfo.URL] = time.Now()
		}
	}
	return
}
Exemplo n.º 2
0
// message will produce an announcement message for the counter.
func (cs *extensionCountersCounter) message() string {
	diff := time.Since(cs.date)
	days := int(math.Abs(diff.Hours())) / 24
	hours := int(math.Abs(diff.Hours())) - days*24
	minutes := int(math.Abs(diff.Minutes())) - hours*60 - days*1440
	vars := map[string]string{
		"days":    fmt.Sprintf("%d", days),
		"hours":   fmt.Sprintf("%d", hours),
		"minutes": fmt.Sprintf("%d", minutes),
		"since":   utils.HumanizedSince(cs.date),
	}
	return utils.Format(cs.textTmp, vars)
}
Exemplo n.º 3
0
func (postData *redditPostData) toStrings() 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":      utils.HumanizedSince(time.Unix(int64(postData.Created), 0)),
		"author":       postData.Author,
		"subreddit":    postData.Subreddit,
		"score":        fmt.Sprintf("%d", postData.Score),
		"comments_url": "http://redd.it/" + postData.Id,
		"comments":     fmt.Sprintf("%d", postData.Comments),
		"title":        postData.Title,
		"url":          postData.Url,
	}
}