Skip to content

Instantly share code, notes, and snippets.

@ota42y
Created September 1, 2014 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ota42y/7e493449beec0fe73f62 to your computer and use it in GitHub Desktop.
Save ota42y/7e493449beec0fe73f62 to your computer and use it in GitHub Desktop.
mongodbにツイートを保存する
package main
import (
"fmt"
"github.com/ChimeraCoder/anaconda"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"net/url"
)
func saveUnRegisterTweetsToDatabase(tweets []anaconda.Tweet) {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("tweettest").C("twetter")
for _, tweet := range tweets {
count, _ := c.Find(bson.M{"id": tweet.Id}).Count()
if count == 0 {
fmt.Println("no exist")
err = c.Insert(&tweet)
if err != nil {
panic(err)
}
} else {
fmt.Println("exist")
}
}
}
func getTweets(screen_name string, api *anaconda.TwitterApi) []anaconda.Tweet {
v := url.Values{}
v.Set("screen_name", screen_name)
tweets, err := api.GetUserTimeline(v)
if err != nil {
panic(err)
}
return tweets
}
func saveUnRegisterTweets(screen_name string, api *anaconda.TwitterApi) {
tweets := getTweets(screen_name, api)
saveUnRegisterTweetsToDatabase(tweets)
}
func main() {
anaconda.SetConsumerKey(CONSUMER_KEY)
anaconda.SetConsumerSecret(CONSUMER_SECRET)
api := anaconda.NewTwitterApi(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
saveUnRegisterTweets("LoveLive_staff", api)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment