Esempio n. 1
0
func (bot *Bot) News() {
	var old map[string]bool
	ticker := time.NewTicker(5 * time.Second)

	defer ticker.Stop()

Retry:
	if briefs, err := bulletin.Briefs(); err != nil {
		select {
		case <-ticker.C:
			goto Retry
		case <-bot.closestream:
			return
		}
	} else {
		old = makeOld(briefs)
	}

	for {
		select {
		case <-ticker.C:
			old = bot.NewNews(old)
		case <-bot.closestream:
			return
		}
	}
}
Esempio n. 2
0
func (bot *Bot) NewNews(old map[string]bool) map[string]bool {
	briefs, err := bulletin.Briefs()
	if err != nil {
		return old
	}

	if !hasNew(old, briefs) {
		return old
	}

	if atomic.LoadUint32(&bot.onnews) == 0 {
		for _, brief := range briefs {
			if url := brief.URL(); !old[url] {
				now := NowKST()
				delta := now.Sub(brief.Time())

				min := uint(delta / time.Minute)
				sec := uint(delta % time.Minute / time.Second)

				if min == 0 {
					bot.Broadcast(fmt.Sprintf("%ds ago - %s", sec, brief.Title()))
				} else {
					bot.Broadcast(fmt.Sprintf("%dm %ds ago - %s", min, sec, brief.Title()))
				}

				bot.Broadcast(brief.URL())
			}
		}
	}

	return makeOld(briefs)
}