// TweetTopStory tweets the top story func TweetTopStory(context schedule.Context) { context.Log("Sending top story tweet") // Get the top story which has not been tweeted yet, newer than 1 day (we don't look at older stories) q := stories.Popular().Limit(1).Order("rank desc, points desc, id desc") // Don't fetch old stories - at some point soon this can come down to 1 day // as all older stories will have been tweeted // For now omit this as we have a backlog of old unposted stories // q.Where("created_at > current_timestamp - interval '60 days'") // Don't fetch stories that have already been tweeted q.Where("tweeted_at IS NULL") // Fetch the stories results, err := stories.FindAll(q) if err != nil { context.Logf("#error getting top story tweet %s", err) return } if len(results) > 0 { story := results[0] TweetStory(context, story) } else { context.Logf("#warn no top story found for tweet") } }
// DailyEmail sends a daily email to subscribed users with top stories - change this to WeeklyEmail // before putting it into production // We should probably only do this for kenny at present func DailyEmail(context schedule.Context) { context.Log("Sending daily email") // First fetch our stories over 5 points q := stories.Popular() // Must be within 7 days q.Where("created_at > current_timestamp - interval '7 day'") // Order by rank q.Order("rank desc, points desc, id desc") // Don't fetch stories that have already been mailed q.Where("newsletter_at IS NULL") // Fetch the stories topStories, err := stories.FindAll(q) if err != nil { context.Logf("#error getting top story tweet %s", err) return } if len(topStories) == 0 { context.Logf("#warn no stories found for newsletter") return } // Now fetch our recipient (initially just Kenny as this is in testing) recipient, err := users.Find(1) if err != nil { context.Logf("#error getting email reciipents %s", err) return } var jobStories []*stories.Story // Email recipients the stories in question - we should perhaps save in db so that we can // have an issue number and always reproduce the digests? mailContext := map[string]interface{}{ "stories": topStories, "jobs": jobStories, } err = mail.SendOne(recipient.Email, "Go News Digest", "users/views/mail/digest.html.got", mailContext) if err != nil { context.Logf("#error sending email %s", err) return } // Record that these stories have been mailed in db params := map[string]string{"newsletter_at": query.TimeString(time.Now().UTC())} err = q.Order("").UpdateAll(params) if err != nil { context.Logf("#error updating top story newsletter_at %s", err) return } }
// TweetTopStory tweets the top story func TweetTopStory(context schedule.Context) { context.Log("Sending top story tweet") // Get the top story which has not been tweeted yet, newer than 1 day (we don't look at older stories) q := stories.Popular().Limit(1).Order("rank desc, points desc, id desc") // Don't fetch old stories q.Where("created_at > current_timestamp - interval '1 day'") // Don't fetch stories that have already been tweeted q.Where("tweeted_at IS NULL") // Fetch the stories results, err := stories.FindAll(q) if err != nil { context.Logf("#error getting top story tweet %s", err) return } if len(results) > 0 { story := results[0] // TWEET tweet := fmt.Sprintf("%s #golang %s", story.Name, story.Url) _, err := twitter.Tweet(tweet) if err != nil { context.Logf("#error tweeting top story %s", err) return } // Record that this story has been tweeted in db params := map[string]string{"tweeted_at": query.TimeString(time.Now().UTC())} err = story.Update(params) if err != nil { context.Logf("#error updating top story tweet %s", err) return } } else { context.Logf("#warn no top story found for tweet") } }