Created
September 1, 2014 22:51
-
-
Save ota42y/7e493449beec0fe73f62 to your computer and use it in GitHub Desktop.
mongodbにツイートを保存する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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