package linkinfo import ( "github.com/dghubble/go-twitter/twitter" "github.com/dghubble/oauth1" "regexp" "strconv" ) var ( twitterHosts = []string{"twitter.com"} twitterStatusRegexp = regexp.MustCompile("/status/(\\d+)") ) type TwitterOptions struct { Option ConsumerKey string ConsumerSecret string Token string TokenSecret string } type TwitterInfoApi struct { opts *TwitterOptions client *twitter.Client } func (i *TwitterInfoApi) Init() { config := oauth1.NewConfig(i.opts.ConsumerKey, i.opts.ConsumerSecret) token := oauth1.NewToken(i.opts.Token, i.opts.TokenSecret) i.client = twitter.NewClient(config.Client(oauth1.NoContext, token)) } func (i *TwitterInfoApi) Handler(link string) (*LinkInfo, error) { m := twitterStatusRegexp.FindStringSubmatch(link) if m == nil { return nil, nil } id, err := strconv.ParseInt(m[1], 10, 64) if err != nil { return nil, err } tweet, _, err := i.client.Statuses.Show(id, nil) if err != nil { return nil, err } ret := &LinkInfo{ Title: tweet.User.Name + " on Twitter: " + tweet.Text, Description: tweet.Text, } return ret, nil }