Exemple #1
0
func getTwitterData(DB *bolt.DB) {
	anaconda.SetConsumerKey("KmxA5PMS1WaVdSnJrYtq5XANb")
	anaconda.SetConsumerSecret("yt7ydv2qFt7BpyHrMK3UzIj7HXGGv7ezcVTnELxhgh2WMGj9IA")
	api := anaconda.NewTwitterApi(
		"268263175-deYL6a9YyDMy8YRDQI0p9NDBoKuZScRKG24Dpqkj",
		"PrFnSYOzsZjPYc5zhN9qeviyyHH0x1sKkiOYSSyPdWrnS")

	tweets, err := api.GetHomeTimeline(url.Values{
		"count": {"10"},
	})
	if err != nil {
		// TODO -- Handle timeouts here
		panic(err)
	}

	Tx, err := DB.Begin(true)
	if err != nil {
		// TODO -- Handle this gracely
		panic(err)
	}
	Bucket := Tx.Bucket([]byte("tweets"))
	for _, t := range tweets {

		tweetText := t.Text
		if t.RetweetedStatus != nil {
			tweetText = t.RetweetedStatus.Text
		}
		tweetText = replaceURLS(tweetText, func(s string) string {
			fmt.Println("Replacing ", s)
			for retries := 0; retries < 3; retries++ {
				newS, err := getRedirectedURL(s)
				if err != nil {
					time.Sleep(time.Duration(1+retries) * time.Second)
					continue
				}
				return newS
			}
			return s
		})
		if t.RetweetedStatus != nil {
			t.RetweetedStatus.Text = tweetText
		} else {
			t.Text = tweetText
		}
		data, err := json.Marshal(t)
		if err != nil {
			Tx.Rollback()
			DB.Sync()
			panic(err)
		}
		key := []byte(strconv.FormatInt(t.Id, 16))
		if err = Bucket.Put(key, data); err != nil {
			Tx.Rollback()
			DB.Sync()
			panic(err)
		}
	}
	Tx.Commit()
}