Skip to content

Instantly share code, notes, and snippets.

@drillbits
Last active December 18, 2015 09:49
Show Gist options
  • Save drillbits/5764247 to your computer and use it in GitHub Desktop.
Save drillbits/5764247 to your computer and use it in GitHub Desktop.
https://github.com/ymotongpoo/go-twitter 使って手探りで Twitter のメンション取得するの書いてみた。それだけなのもアレなので json から設定取ってくるみたいにした。
{
"Twitter": {
"ConsumerKey": "yourConsumerKey",
"ConsumerSecret": "yourConsumerSecret",
"AccessToken": "yourAccessToken",
"AccessTokenSecret": "yourAccessTokenSecret"
}
}
package main
import (
"encoding/json"
"fmt"
"github.com/garyburd/go-oauth/oauth"
"github.com/ymotongpoo/go-twitter/twitter"
"io/ioutil"
"net/http"
"os"
)
type TwitterConfig struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
func LoadConfig() (conf *TwitterConfig, err error) {
b, err := ioutil.ReadFile("conf.json")
if err != nil {
return
}
var jsonMap map[string]*json.RawMessage
err = json.Unmarshal(b, &jsonMap)
if err != nil {
fmt.Println("Bad json", err)
return
}
err = json.Unmarshal(*jsonMap["Twitter"], &conf)
if err != nil {
fmt.Println("Bad json", err)
return
}
return
}
func main() {
var (
err error
twConf *TwitterConfig
client *http.Client
twClient *twitter.Client
tweets []*twitter.Tweets
)
if twConf, err = LoadConfig(); err != nil {
fmt.Printf("Could not parse config file: %v\n", err)
os.Exit(1)
}
client = &http.Client{}
twClient = twitter.NewClient(client)
consumer := oauth.Credentials{twConf.ConsumerKey, twConf.ConsumerSecret}
access := oauth.Credentials{twConf.AccessToken, twConf.AccessTokenSecret}
twClient.AddCredentials(&consumer, &access)
if tweets, err = twClient.MentionsTimeline(nil); err != nil {
fmt.Printf("Could not get mentions: %v\n", err)
os.Exit(1)
}
for _, tweet := range tweets {
fmt.Printf("%v: %v", tweet.User.ScreenName, tweet.Text)
fmt.Printf("\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment