mi-v1/cmd/mi/twitter.go

51 lines
1.3 KiB
Go
Raw Normal View History

2020-01-11 18:38:50 +00:00
package main
import (
2020-01-11 20:18:59 +00:00
"context"
2020-01-11 19:11:30 +00:00
"flag"
2020-01-11 18:38:50 +00:00
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
2020-01-11 20:18:59 +00:00
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
"within.website/ln"
2020-01-11 18:38:50 +00:00
)
2020-01-11 19:11:30 +00:00
var (
// Twitter
twitterAPIKey = flag.String("twitter-api-key", "", "Twitter API key")
twitterAPISecret = flag.String("twitter-api-secret", "", "Twitter API secret")
twitterConsumerToken = flag.String("twitter-consumer-token", "", "Twitter consumer Token")
twitterConsumerSecret = flag.String("twitter-consumer-secret", "", "Twitter consumer Secret")
)
2020-01-11 18:38:50 +00:00
func makeTwitter() (*twitter.Client, error) {
config := oauth1.NewConfig(*twitterConsumerToken, *twitterConsumerSecret)
token := oauth1.NewToken(*twitterAPIKey, *twitterAPISecret)
// http.Client will automatically authorize Requests
httpClient := config.Client(oauth1.NoContext, token)
// twitter client
client := twitter.NewClient(httpClient)
_, _, err := client.Accounts.VerifyCredentials(nil)
if err != nil {
return nil, err
}
return client, nil
}
2020-01-11 20:18:59 +00:00
func (mi *Mi) PushTwitter(ctx context.Context, p Post) error {
tweet, _, err := mi.twitterClient.Statuses.Update(p.Format(), nil)
if err != nil {
return err
}
err = r.Table("tweets").Insert(tweet).Exec(mi.session)
if err != nil {
ln.Error(ctx, err)
}
return nil
}