Skip to content

Instantly share code, notes, and snippets.

@yhirano55
Created March 5, 2017 14:16
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 yhirano55/6e1da71090e8709aae515390ffff0b6d to your computer and use it in GitHub Desktop.
Save yhirano55/6e1da71090e8709aae515390ffff0b6d to your computer and use it in GitHub Desktop.
GolangでTwitterの検索結果を出力する

GolangでTwitterの検索結果を出力する

library

$ go get github.com/ChimeraCoder/anaconda

Create Twitter Application

https://apps.twitter.com/

examples

特定キーワードの検索結果を出力

package main

import (
	"fmt"
	"github.com/ChimeraCoder/anaconda"
)

func main() {
	anaconda.SetConsumerKey("CONSUMER_KEY")
	anaconda.SetConsumerSecret("CONSUMER_SECRET")
	api := anaconda.NewTwitterApi("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")

	searchResult, _ := api.GetSearch("#tbs", nil)
	for _, tweet := range searchResult.Statuses {
		fmt.Println(tweet.Text)
	}

	fmt.Println("done")
}

flagパッケージで引数をとる

package main

import (
	"flag"
	"fmt"
	"github.com/ChimeraCoder/anaconda"
)

func main() {
	// --keyword="keyword" で取得可能
	var keyword = flag.String("keyword", "golang", "keyword for search")
	flag.Parse()

	fmt.Println("keyword:" + *keyword)

	anaconda.SetConsumerKey("CONSUMER_KEY")
	anaconda.SetConsumerSecret("CONSUMER_SECRET")
	api := anaconda.NewTwitterApi("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")

	searchResult, _ := api.GetSearch(*keyword, nil)
	for _, tweet := range searchResult.Statuses {
		fmt.Println(tweet.Text)
	}

	fmt.Println("done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment