示例#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
}
示例#2
0
// getRedditInfo fetches information about a link from Reddit.
func (ext *ExtensionReddit) getRedditInfo(bot *papaBot.Bot, url, urlTitle, channel string) string {
	// Get the listing.
	url = fmt.Sprintf("https://www.reddit.com/api/info.json?url=%s", url)
	var listing redditListing
	if err := ext.getRedditListing(bot, url, &listing); err != nil {
		bot.Log.Debugf("Error getting reddit's response %d.", listing.Error)
		return ""
	}

	ext.announced[channel+url] = true

	// Find highest rated post and return it.
	message := ""
	bestScore := 0
	for i := range listing.Data.Children {
		postData := listing.Data.Children[i].Data
		if postData.Score > bestScore {
			// Was the title already included in the URL title?
			if strings.Contains(urlTitle, postData.Title) {
				postData.Title = ""
			}
			// Trim the title.
			if len(postData.Title) > 200 {
				postData.Title = postData.Title[:200] + "(…)"
			}
			message = utils.Format(ext.Texts.TempRedditAnnounce, postData.toStrings())
			bestScore = postData.Score
		}
	}
	bot.Log.Debugf("Reddit: %s", message)
	return message
}
示例#3
0
// Tick will clear the announces table and give post of the day.
func (ext *ExtensionReddit) Tick(bot *papaBot.Bot, daily bool) {
	if daily {
		// Clear the announced list.
		ext.announced = map[string]bool{}
		if bot.GetVar("redditDaily") != "" {
			post := ext.getRedditHot(bot)
			if post != nil {
				bot.SendMassNotice(utils.Format(ext.Texts.TempRedditDaily, post.toStrings()))
			}
		}
	} else { // 5 minute ticker.
		url, title := ext.getRedditLiveNow(bot)
		if url == "" || title == "" {
			return
		}
		if ext.announcedLive[url] {
			return
		}
		ext.announcedLive[url] = true
		bot.SendMassNotice(utils.Format(ext.Texts.TempRedditBreaking, map[string]string{"url": url, "title": title}))
	}
}
示例#4
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)
}