Exemple #1
0
// Every tweet is its own feed item
func CreateIndividualFeed(conf config.Config, tweets []twitter.Tweet) *feeds.Feed {
	feed := createFeedHeader()

	for _, tweet := range tweets {
		parsedTweetText := parser.ParseTweetText(tweet)

		if filter.IsTweetFiltered(tweet, conf, parsedTweetText) {
			continue
		}

		titleLimit := 60
		if len(tweet.Text) < 60 {
			titleLimit = len(tweet.Text)
		}
		item := &feeds.Item{
			// TODO: check if slicing a string with non ascii chars will fail/scramble the text
			Title:       fmt.Sprintf("%s: %s...", tweet.User.Name, tweet.Text[:titleLimit]),
			Link:        &feeds.Link{Href: parser.GetTweetUrl(tweet)},
			Description: parsedTweetText,
			Author:      &feeds.Author{Name: tweet.User.Name, Email: tweet.User.ScreenName},
			Created:     parser.ConvertTwitterTime(tweet.CreatedAt),
			Id:          tweet.IDStr,
		}
		feed.Add(item)
	}

	return feed
}
Exemple #2
0
// combine multiple tweets together into one feed item
func CreateCombinedUserFeed(conf config.Config, tweets []twitter.Tweet) *feeds.Feed {
	feed := createFeedHeader()

	sortedTweets := sortTweetsIntoHourSegments(tweets, conf.CombinedFeedHours)

	for tweetTimeSegment, timeSortedTweets := range sortedTweets {
		// get YYYY-MM-DD + Hour / <segmentsize>
		hourBlock := strconv.Itoa(time.Now().Hour() / conf.CombinedFeedHours)
		currentTimeSegment := time.Now().Format("2006-01-02") + "-" + hourBlock

		if tweetTimeSegment == currentTimeSegment {
			fmt.Println("INFO: Skipping tweets from the most recent time segment.")
			fmt.Println("      This will avoid duplicates or incomplete rss entries.")
			continue
		}

		for twitterUser, authorTweets := range timeSortedTweets {
			var feedText string

			for _, tweet := range timeSort(authorTweets) {
				parsedTweetText := parser.ParseTweetText(tweet)

				if filter.IsTweetFiltered(tweet, conf, parsedTweetText) {
					continue
				}
				feedText += parsedTweetText + "\n<hr>\n"
			}

			// 0 if all tweets are filtered
			if len(feedText) == 0 {
				log.Printf("DEBUG: skipping %v-%v since all tweets are filtered\n", tweetTimeSegment, twitterUser)
				continue
			}

			// Calculate the time so we have a nicer formating
			segment, err := strconv.Atoi(tweetTimeSegment[11:])
			if err != nil {
				fmt.Printf("WARNING: couldn't converte %q to integer\n", tweetTimeSegment[11:])
			}
			segmentTime := strconv.Itoa(conf.CombinedFeedHours * segment)

			rssDate, err := time.Parse("2006-01-02-15", tweetTimeSegment[0:10]+"-"+segmentTime)
			if err != nil {
				fmt.Printf("WARNING: couldn't parse time %q \n", tweetTimeSegment[0:10]+"-"+segmentTime)
			}
			humanTweetSummaryTime := rssDate.Format("03pm")

			if conf.Debug {
				feedText = rssDate.Format("1 2 3 4 -- 03pm") + "<br>\nId: " + tweetTimeSegment + "-" + twitterUser + "<br>\nLen feedtext: " + strconv.Itoa(len(feedText)) + "<p>\n\n" + feedText
			}

			item := &feeds.Item{
				// TODO: check if slicing a string with non ascii chars will fail/scramble the text
				Title:       fmt.Sprintf("%s %s %s...", twitterUser, humanTweetSummaryTime, "combined tweets update"),
				Link:        &feeds.Link{Href: "https://twitter.com/" + twitterUser + "/"},
				Description: feedText,
				Author:      &feeds.Author{Name: twitterUser, Email: twitterUser},
				Created:     rssDate,
				Id:          fmt.Sprintf("%s-%s", tweetTimeSegment, twitterUser),
			}
			feed.Add(item)
		}
	}

	return feed
}