// fetchNewSources will check the Twitter API for new tweets by 'sources,' using the // authentication from 'token.' Note that we'd like this to be a member function of some // struct interface "ResourceHolder," but Goobuntu + GBus Wifi are so craptacularly out // of sync that I have to do this one offline, and can't look up whether we could even // attach an implementation to an interface in Go, or what that would look like. // // Feel my first 'rants' email coming along... func fetchNewSources(userlist []string, userToken *oauth1.Token, data *DataHandle, logger *logging.LogMaster, tf *TweetFetcher) []string { var sourcestrings []string for _, username := range userlist { // get tweets from persistent storage logger.StatusWrite("Reading from persistent storage for %s...\n", username) oldTweets := data.GetTweetsFromStorage(username) var newTweets Tweets if len(oldTweets) == 0 { logger.StatusWrite("Found no tweets for %s, doing a deep dive to retrieve their history.\n", username) newTweets = tf.DeepDive(username, userToken) } else { logger.StatusWrite("Found %d tweets for %s.\n", len(oldTweets), username) newest := oldTweets[len(oldTweets)-1] newTweets = tf.GetRecentTimeline(username, &newest, userToken) } // update the persistent storage logger.StatusWrite("Inserting %d new tweets into persistent storage.\n", len(newTweets)) data.InsertFreshTweets(username, newTweets) copyFrom(&sourcestrings, &oldTweets) copyFrom(&sourcestrings, &newTweets) } return sourcestrings }
// Ensures we've got a valid instance of the database, and if not, creates one // with the appropriate tables. func getDataHandle(filename string, logger *logging.LogMaster) DataHandle { db, err := sql.Open("sqlite3", filename) handle := DataHandle{db, logger} if err != nil { logger.StatusWrite("sql.Open returned non-nil error!\n") logger.DebugWrite("sql.Open returned error: %v\n", err) return handle } sqls := []string{"CREATE TABLE Tweets (Id TEXT NOT NULL, Screen_Name TEXT NOT NULL, Content TEXT NOT NULL)", "CREATE TABLE TwitterUsers (Screen_Name TEXT NOT NULL, Token TEXT NOT NULL, Token_Secret TEXT NOT NULL)"} for _, sql := range sqls { _, err = db.Exec(sql) if err != nil && err.Error() != "table Tweets already exists" && err.Error() != "table TwitterUsers already exists" { logger.StatusWrite("sql.Exec returned unexpected error on DataHandle Aquisition.\n") logger.DebugWrite("Error: %v\n", err) } } return handle }