Exemplo n.º 1
0
// Test that creating a TwitterApi client creates a client with non-empty OAuth credentials
func Test_TwitterApi_NewTwitterApi(t *testing.T) {
	anaconda.SetConsumerKey(CONSUMER_KEY)
	anaconda.SetConsumerSecret(CONSUMER_SECRET)
	api = anaconda.NewTwitterApi(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

	if api.Credentials == nil {
		t.Errorf("Twitter Api client has empty (nil) credentials")
	}
}
Exemplo n.º 2
0
func ExampleTwitterApi_GetSearch() {

	anaconda.SetConsumerKey("your-consumer-key")
	anaconda.SetConsumerSecret("your-consumer-secret")
	api := anaconda.NewTwitterApi("your-access-token", "your-access-token-secret")
	search_result, err := api.GetSearch("golang", nil)
	if err != nil {
		panic(err)
	}
	for _, tweet := range search_result {
		fmt.Print(tweet.Text)
	}
}
Exemplo n.º 3
0
// Throttling queries can easily be handled in the background, automatically
func ExampleTwitterApi_Throttling() {
	api := anaconda.NewTwitterApi("your-access-token", "your-access-token-secret")
	api.EnableThrottling(10*time.Second, 5)

	// These queries will execute in order
	// with appropriate delays inserted only if necessary
	golangTweets, err := api.GetSearch("golang", nil)
	anacondaTweets, err2 := api.GetSearch("anaconda", nil)

	if err != nil {
		panic(err)
	}
	if err2 != nil {
		panic(err)
	}

	fmt.Println(golangTweets)
	fmt.Println(anacondaTweets)
}
Exemplo n.º 4
0
// Initialize an client library for a given user.
// This only needs to be done *once* per user
func ExampleTwitterApi_InitializeClient() {
	anaconda.SetConsumerKey("your-consumer-key")
	anaconda.SetConsumerSecret("your-consumer-secret")
	api := anaconda.NewTwitterApi(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
	fmt.Println(*api.Credentials)
}
Exemplo n.º 5
0
func (Client) Materialize() be.Reflex {
	var c1, a1 sync.Once
	api, consumer, access := make(chan *anaconda.TwitterApi), make(chan Image, 1), make(chan Image, 1)
	go func() { // start connecting monad
		var c, a Image
		for i := 0; i < 2; i++ {
			select {
			case c = <-consumer:
				consumer = nil
			case a = <-access:
				access = nil
			}
		}
		anaconda.SetConsumerKey(c.String("Key")) // dial API server
		anaconda.SetConsumerSecret(c.String("Secret"))
		y := anaconda.NewTwitterApi(a.String("Token"), a.String("Secret"))
		for {
			api <- y // give out api server to all endpoint goroutines
		}
	}()
	// API
	query := make(chan Image, 5)
	reflex, eye := plumb.NewEyeCognizer(
		func(eye *plumb.Eye, valve string, value interface{}) {
			switch valve {
			case "Consumer":
				c1.Do(func() { consumer <- value.(Image) })
			case "Access":
				a1.Do(func() { access <- value.(Image) })
			case "UserTimelineQuery", "HomeTimelineQuery", "RetweetsQuery", "RetweetsOfMeQuery":
				valve = valve[:len(valve)-len("Query")]
				query <- Make().Grow(valve, value)
			default:
				log.Printf("Unknown Twitter query: %s", valve)
			}
		},
		"Consumer", "Access", // set to start connection
		"UserTimelineQuery", "UserTimelineResult", // UserTimeline
		"HomeTimelineQuery", "HomeTimelineResult", // HomeTimeline
		"RetweetsQuery", "RetweetsResult", // Retweets
		"RetweetsOfMeQuery", "RetweetsOfMeResult", // RetweetsOfMe
	)
	for i := 0; i < 3; i++ {
		go func() { // API response loop
			y := <-api
			for {
				g := <-query
				q := g.Letters()[0]
				x := g[q].(Image)
				uv := urlize(x)
				log.Printf("Twitter %s query %v", q, ImagineWithMaps(uv).(Image).PrintLine())
				var tweets []anaconda.Tweet
				var err error
				switch q {
				case "UserTimeline":
					tweets, err = y.GetUserTimeline(uv)
				case "HomeTimeline":
					tweets, err = y.GetHomeTimeline(uv)
				case "Retweets":
					tweets, err = y.GetRetweets(int64(x.Int("Id")), uv)
				case "RetweetsOfMe":
					tweets, err = y.GetRetweetsOfMe(uv)
				}
				if err != nil {
					log.Fatalf("Problem %s query on Twitter (%v)", q, err)
				}
				eye.Show(
					q,
					Pretty(
						Image{
							"Name":     x.Interface("Name"),
							"Sentence": Imagine(tweets),
						},
					),
				)
			}
		}()
	}
	return reflex
}
Exemplo n.º 6
0
func init() {
	// Initialize api so it can be used even when invidual tests are run in isolation
	anaconda.SetConsumerKey(CONSUMER_KEY)
	anaconda.SetConsumerSecret(CONSUMER_SECRET)
	api = anaconda.NewTwitterApi(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
}