Пример #1
0
func (m *JNetModule) prHandler(conn *irc.Conn, msg *Privmsg, matches [][]string) {
	for _, match := range matches {
		prNumber := match[1]

		tinyUrlChan := make(chan string)
		go func(tinyUrlChan chan string) {
			longUrl := jnet.PRUrl(prNumber)
			shortUrl, err := tinyurl.Tinyify(longUrl)
			if err != nil {
				log.WithFields(logrus.Fields{
					"longUrl":  longUrl,
					"shortUrl": shortUrl,
					"pr":       prNumber,
					"error":    err,
				}).Error("Jnet: Failed to resolve tinyURL")
				tinyUrlChan <- longUrl
			}
			tinyUrlChan <- shortUrl
		}(tinyUrlChan)

		prChan := make(chan *jnet.JNetPR)
		go func(prChan chan *jnet.JNetPR) {
			j := jnet.NewJNet(m.config.Username, m.config.Password)
			pr, err := j.GetPR(prNumber)
			if err != nil {
				log.WithFields(logrus.Fields{
					"pr":    prNumber,
					"error": err,
				}).Error("Jnet: Failed to get PR information")
				prChan <- nil
			}
			prChan <- pr
		}(prChan)

		// Wait for reponses
		shortUrl := <-tinyUrlChan
		pr := <-prChan

		if pr == nil {
			msg.RespondToNick(conn, "Failed to find PR information for %s - %s", prNumber, shortUrl)
			return
		}

		var buf bytes.Buffer

		buf.WriteString(fmt.Sprintf("%s (%s) [%s] %s", pr.Number, pr.Status, pr.Severity, pr.Title))

		if pr.ResolvedIn != "" {
			buf.WriteString(fmt.Sprintf(" Fixed in: %s", pr.ResolvedIn))
		}
		buf.WriteString(" - ")
		buf.WriteString(shortUrl)

		msg.Respond(conn, buf.String())
	}
}
Пример #2
0
func pollFeed(conn *irc.Conn, feedConfig *RSSFeedConfig, timeout int) {
	itemHandler := func(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
		tag := feedConfig.Name
		if feedConfig.Name == "" {
			tag = ch.Title
		}

		// Dirty reverse
		reverseItems := make([]*rss.Item, len(newitems))
		for i, item := range newitems {
			reverseItems[len(newitems)-i-1] = item
		}

		rssLastSeen := RSSLastSeen{}
		db.Where(RSSLastSeen{Feed: feedConfig.URL}).FirstOrInit(&rssLastSeen)

		// This is hack.  Testing for gorm.RecordNotFound doesn't seem to be working with
		// FirstOrInit
		if rssLastSeen.Key == "" {
			log.WithFields(logrus.Fields{
				"feed": feedConfig.URL,
			}).Info("No last seen for feed.  Assuming new feed and not spamming the channel")
		} else {
			for _, item := range reverseItems {
				if rssLastSeen.Key == item.Key() {
					log.WithFields(logrus.Fields{
						"item":        item.Title,
						"lastseenkey": rssLastSeen.Key,
						"item_key":    item.Key(),
					}).Debug("RSS: Already seen this RSS item")
					break
				}

				// Tinyify item's url
				shortURL, err := tinyurl.Tinyify(item.Links[0].Href)
				if err != nil {
					log.WithFields(logrus.Fields{
						"longURL":  item.Links[0].Href,
						"shortURL": shortURL,
						"error":    err,
					}).Error("RSS: Failed to resolve tinyURL")
					shortURL = item.Links[0].Href
				}

				for _, channel := range feedConfig.Channels {
					conn.Privmsg(channel, fmt.Sprintf("[RSS %s] %s - %s", tag, item.Title, shortURL))
				}
			}
		}

		// Write the first item to the last seen database
		rssLastSeen.Key = reverseItems[0].Key()
		db.Save(&rssLastSeen)
	}

	feed := rss.New(timeout, true, nil, itemHandler)

	for {
		log.WithFields(logrus.Fields{
			"feed": feedConfig.URL,
		}).Info("RSS: Fetching rss feed")

		if err := feed.Fetch(feedConfig.URL, nil); err != nil {
			log.WithFields(logrus.Fields{
				"feed":  feedConfig.URL,
				"error": err,
			}).Error("RSS: Error retriving feed.")
		}

		log.WithFields(logrus.Fields{
			"feed":              feedConfig.URL,
			"secondsTillUpdate": feed.SecondsTillUpdate(),
		}).Info("RSS: Waiting")

		<-time.After(time.Duration(feed.SecondsTillUpdate()) * time.Second)
	}
}