示例#1
0
func (this *FeedBuilder) Generate(messages []*usecases.UcMessage) []byte {
	channel := moverss.ChannelFactory("Hamburg Privateers", "http://www.kickern-hamburg.de/phpBB2/viewforum.php?f=15", "Hamburg Privateers feed")
	channel.SetTTL(strconv.Itoa(this.updateInterval / 120))
	for _, message := range messages {
		item := createItem(message)
		channel.AddItem(item)
	}
	return channel.Publish()
}
示例#2
0
// CleanNodeRSS recreates the node RSS feed from scratch using the
// database and logs any errors.
func CleanNodeRSS() {
	NodeRSS = moverss.ChannelFactory(
		Conf.Name+" NodeAtlas",
		Conf.Web.Hostname,
		"New local node feed")

	NodeXMLName = xml.Name{
		Space: Conf.Web.Hostname,
		Local: "nodes",
	}

	// We use a separate query here so that we can retrieve only the
	// fields we need, and only nodes newer than RSS.MaxAge ago.
	rows, err := Db.Query(`
SELECT updated,address,owner
FROM nodes
WHERE updated >= ?;`, time.Now().Add(time.Duration(-Conf.Web.RSS.MaxAge)))
	if err != nil {
		l.Errf("Error getting nodes from database: %s", err)
		return
	}
	defer rows.Close()

	for rows.Next() {
		// Initialize the variables.
		var updated int64
		node := new(Node)

		// Scan all of the values into them.
		err = rows.Scan(&updated, &node.Addr, &node.OwnerName)
		if err != nil {
			l.Errf("Error getting nodes from database: %s", err)
			return
		}

		// Add the Node to the RSS feed.
		in := node.Item()
		in.SetPubDate(time.Unix(updated, 0))
		NodeRSS.AddItem(in)
	}

	// Write the feed to the file, and log any errors.
	WriteNodeRSS()

	return
}
示例#3
0
func main() {
	setup()

	latestVotes, err := votes.FetchLatestVotes()
	if err != nil {
		log.Printf("%+v", err)
	}

	wordsToTranslate := []string{}
	for _, vote := range latestVotes {
		firstWord := strings.Split(vote.DescriptionEnglish, " ")[0]
		wordsToTranslate = append(wordsToTranslate, firstWord)
	}

	translateConfig := translate.Config{
		GrantType:    "client_credentials",
		ScopeUrl:     "http://api.microsofttranslator.com",
		ClientId:     env.String("BING_CLIENT_ID"),
		ClientSecret: env.String("BING_CLIENT_SECRET"),
		AuthUrl:      "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/",
	}
	token, err := translate.GetToken(&translateConfig)
	if err != nil {
		log.Printf("%v", err)
	}
	translatedWords, err := token.TranslateArray(wordsToTranslate, "", "fr")
	if err != nil {
		log.Printf("%v", err)
	}
	for index, word := range translatedWords {
		switch word {
		case "Assentiment":
			translatedWords[index] = "Adoption"
		case "2ème":
			translatedWords[index] = "2e"
		case "Privé":
			translatedWords[index] = "Affaires"
		case "Temps":
			translatedWords[index] = "Attribution"
		}
	}
	if len(latestVotes) > 0 {
		log.Printf("%+v", wordsToTranslate)
		log.Printf("%+v", translatedWords)
	}

	for index, vote := range latestVotes {
		splitIndex := strings.LastIndex(vote.DescriptionEnglish, translatedWords[index])
		if splitIndex < 0 {
			splitIndex = len(vote.DescriptionEnglish) - 1
		}
		log.Printf("whole: %v", vote.DescriptionEnglish)
		english := strings.Join(strings.Split(vote.DescriptionEnglish, "")[0:splitIndex], "")
		log.Printf("en: %v", english)
		french := strings.Join(strings.Split(vote.DescriptionEnglish, "")[splitIndex:], "")
		log.Printf("fr: %v", french)
		latestVotes[index].DescriptionEnglish = english
		latestVotes[index].DescriptionFrench = french
	}

	// Insert oldest votes first
	latestVotes.Reverse()
	votes.InsertVotes(latestVotes)
	log.Printf("[worker] Inserted %d rows", len(latestVotes))

	// Push new votes to Buffer, oldest first
	buffer := initBuffer()
	profiles, err := buffer.Profiles()
	if err != nil {
		log.Printf("%v", err)
	}

	bufferCount := 0
	for _, vote := range latestVotes {
		u := bufferapi.NewUpdate{Text: vote.ShortDescription() + " " + vote.Link(),
			Media:      map[string]string{"link": vote.Link()},
			ProfileIds: []string{(*profiles)[0].Id},
			Shorten:    true,
			Now:        false}
		_, err := buffer.Update(&u)
		if err != nil {
			log.Printf("%v", err)
		} else {
			bufferCount++
		}
	}
	log.Printf("[worker] Pushed %d tweets to Buffer", bufferCount)

	latestTenVotes, err := votes.LatestTenVotes()
	if err != nil {
		log.Printf("%+v", err)
	}

	// Render JSON file
	j := JSONFeed{Title: "Today's Vote", Link: "http://www.todaysvote.ca", Description: "Stay up to date with what Canada's House of Commons is voting on each day.", Items: latestTenVotes}
	err = renderJSON("feed", j)
	if err != nil {
		log.Printf("%v", err)
	}
	log.Println("[worker] Rendered JSON feed")

	// Write feed to RSS file
	c := moverss.ChannelFactory("Today's Vote", "http://www.todaysvote.ca", "Stay up to date with what Canada's House of Commons is voting on each day.")
	err = renderRSS("feed", c, latestTenVotes)
	if err != nil {
		log.Printf("%v", err)
	}
	log.Println("[worker] Rendered RSS feed")

	// Render index.html
	err = renderHTML("index", latestTenVotes)
	if err != nil {
		log.Printf("%v", err)
	}
	log.Println("[worker] Rendered HTML page")
}