// 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 - 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] // Link to the primary url for this type of story url := story.PrimaryURL() if strings.HasPrefix(url, "/") { url = "https://golangnews.com" + url } tweet := fmt.Sprintf("%s #golang %s", story.Name, url) context.Logf("#info sending tweet:%s", tweet) _, 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") } }
// FacebookPostTopStory facebook posts the top story func FacebookPostTopStory(context schedule.Context) { context.Log("#info posting top story facebook") // Get the top story q := stories.Popular().Limit(1).Order("rank desc, points desc, id desc") // Don't fetch old stories q.Where("created_at > current_timestamp - interval '6 hours'") // Fetch the story results, err := stories.FindAll(q) if err != nil { context.Logf("#error getting top story for fb %s", err) return } if len(results) > 0 { story := results[0] context.Logf("#info facebook posting %s", story.Name) err := facebook.Post(story.Name, story.Url) if err != nil { context.Logf("#error facebook post top story %s", err) return } // Do not record fb posts - this could lead to duplicates... // we should perhaps have a join table for social media posts, rather than dates on stories? } else { context.Logf("#warn no top story found for fb") } }
// 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") } }
// TweetStory tweets the given story func TweetStory(context schedule.Context, story *stories.Story) { // Base url from config baseURL := context.Config("root_url") // Link to the primary url for this type of story url := story.PrimaryURL() // Check for relative urls if strings.HasPrefix(url, "/") { url = baseURL + url } tweet := fmt.Sprintf("%s #golang %s", story.Name, url) // If the tweet will be too long for twitter, use GN url if len(tweet) > 140 { tweet = fmt.Sprintf("%s #golang %s", story.Name, baseURL+story.URLShow()) } context.Logf("#info sending tweet:%s", tweet) _, 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 } }