2019-10-04 01:25:57 +00:00
|
|
|
package linkinfo
|
|
|
|
|
2019-10-06 04:52:26 +00:00
|
|
|
import (
|
|
|
|
"github.com/dghubble/go-twitter/twitter"
|
|
|
|
"github.com/dghubble/oauth1"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2019-10-04 01:25:57 +00:00
|
|
|
var (
|
2019-10-06 04:52:26 +00:00
|
|
|
twitterHosts = []string{"twitter.com"}
|
|
|
|
twitterStatusRegexp = regexp.MustCompile("/status/(\\d+)")
|
2019-10-04 01:25:57 +00:00
|
|
|
)
|
|
|
|
|
2019-10-06 04:52:26 +00:00
|
|
|
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
|
2019-10-04 01:25:57 +00:00
|
|
|
}
|