Skip to content

Commit

Permalink
Fix #14: Extended logging
Browse files Browse the repository at this point in the history
  • Loading branch information
andygrunwald committed Dec 23, 2016
1 parent c6a9625 commit 4f3bf73
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func main() {
if err != nil {
log.Fatalf("Twitter Configuration initialisation failed: %s", err)
}
log.Printf("Twitter Configuration initialisation success: ShortUrlLength %d\n", twitterClient.Configuration.ShortUrlLength)
}

twitterClient.SetupConfigurationRefresh(*configurationRefreshTime)
Expand All @@ -72,6 +73,8 @@ func main() {
// Request a storage backend
storageBackend := storage.NewBackend(*storageURL, *storageAuth, *debugMode)
defer storageBackend.Close()
log.Println("Storage backend initialisation success")


// Let the party begin
StartTweeting(twitterClient, storageBackend, *tweetTime)
Expand Down
33 changes: 21 additions & 12 deletions tweets.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func (ts *TweetSearch) TimeframeLoopToSearchAProject(timeFrames []string, langua

for _, timeFrame := range timeFrames {
if len(language) > 0 {
log.Printf("Getting projects for timeframe %s and language %s", timeFrame, language)
log.Printf("Getting trending projects for timeframe \"%s\" and language \"%s\"", timeFrame, language)
} else {
log.Printf("Getting projects for timeframe %s", timeFrame)
log.Printf("Getting trending projects for timeframe \"%s\"", timeFrame)
}

getProject := ts.Trending.GetRandomProjectGenerator(timeFrame, language)
Expand All @@ -98,7 +98,10 @@ func (ts *TweetSearch) SendProject(p trending.Project) {
// This is a really hack here ...
// We have to abstract this a little bit.
// Eieieieiei
repository, _ := github.GetRepositoryDetails(p.Owner, p.RepositoryName)
repository, err := github.GetRepositoryDetails(p.Owner, p.RepositoryName)
if err != nil {
log.Printf("Error by retrieving repository details: %s", err)
}

text = ts.BuildTweet(p, repository)
}
Expand Down Expand Up @@ -128,9 +131,6 @@ func (ts *TweetSearch) FindProjectWithRandomProjectGenerator(getProject func() (
storageConn := ts.Storage.Get()
defer storageConn.Close()

// TODO Lets throw an error, when we dont get a project at all
// This happened in the past and the bot tweeted nothing.

for project, projectErr = getProject(); projectErr == nil; project, projectErr = getProject() {
// Check if the project was already tweeted
alreadyTweeted, err := storageConn.IsRepositoryAlreadyTweeted(project.Name)
Expand All @@ -150,6 +150,13 @@ func (ts *TweetSearch) FindProjectWithRandomProjectGenerator(getProject func() (
break
}

// Lets throw an error, when we dont get a project at all
// This happened in the past and the bot tweeted nothing.
// See https://github.com/andygrunwald/TrendingGithub/issues/12
if projectErr != nil {
log.Printf("Error by searching for a new project with random project generator: %s", projectErr)
}

return projectToTweet
}

Expand Down Expand Up @@ -236,7 +243,7 @@ func (ts *TweetSearch) MarkTweetAsAlreadyTweeted(projectName string) (bool, erro

res, err := storageConn.MarkRepositoryAsTweeted(projectName, score)
if err != nil || res != true {
log.Printf("Error during adding project %s to tweeted list: %s (%v)", projectName, err, res)
log.Printf("Error during adding project %s to tweeted list: %s (%v)\n", projectName, err, res)
}

return res, err
Expand All @@ -256,6 +263,7 @@ func StartTweeting(twitter *twitter.Twitter, storageBackend storage.Pool, tweetT
URLLength: twitter.Configuration.ShortUrlLengthHttps,
}
SetupRegularTweetSearchProcess(ts, tweetTime)
log.Println("Everything setted up. Lets wait for the first trending project...")

// Waiting for tweets ...
for tweet := range ts.Channel {
Expand All @@ -268,7 +276,7 @@ func StartTweeting(twitter *twitter.Twitter, storageBackend storage.Pool, tweetT
// We do this check here and not in tweets.go, because otherwise
// a new tweet won`t be scheduled
if len(tweet.ProjectName) <= 0 {
log.Print("No project found. No tweet sent.")
log.Println("No project found. No tweet sent.")
continue
}

Expand All @@ -281,9 +289,9 @@ func StartTweeting(twitter *twitter.Twitter, storageBackend storage.Pool, tweetT
} else {
postedTweet, err := twitter.Tweet(tweet.Tweet)
if err != nil {
log.Println(err)
log.Printf("Error during tweet publishing process: %s\n", err)
} else {
log.Printf("Tweet %s posted", postedTweet.IdStr)
log.Printf("New tweet posted! ID: %s\n", postedTweet.IdStr)
}
}
ts.MarkTweetAsAlreadyTweeted(tweet.ProjectName)
Expand All @@ -292,12 +300,13 @@ func StartTweeting(twitter *twitter.Twitter, storageBackend storage.Pool, tweetT

// SetupRegularTweetSearchProcess is the time ticker to search a new project and
// tweet it in a specific time interval.
func SetupRegularTweetSearchProcess(tweetSearch *TweetSearch, tweetTime time.Duration) {
func SetupRegularTweetSearchProcess(tweetSearch *TweetSearch, d time.Duration) {
go func() {
for range time.Tick(tweetTime) {
for range time.Tick(d) {
go tweetSearch.GenerateNewTweet()
}
}()
log.Printf("Project search and tweet interval: Every %s\n", d.String())
}

// ShuffleStringSlice will randomize a string slice.
Expand Down
3 changes: 3 additions & 0 deletions twitter/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ChimeraCoder/anaconda"
"log"
)

const (
Expand Down Expand Up @@ -85,6 +86,7 @@ func (client *Twitter) SetupConfigurationRefresh(d time.Duration) {
client.LoadConfiguration()
}
}()
log.Printf("Twitter Configuration refreshrate: Every %s\n", d.String())
}

// SetupFollowNewPeopleScheduling sets up a scheduler and will search for a new person to follow every duration d.
Expand All @@ -95,6 +97,7 @@ func (client *Twitter) SetupFollowNewPeopleScheduling(d time.Duration) {
client.FollowNewPerson()
}
}()
log.Printf("Growth hack interval: Every %s\n", d.String())
}

// FollowNewPerson will follow a new person on twitter to raise the attraction for the bot.
Expand Down

0 comments on commit 4f3bf73

Please sign in to comment.