mi-v1/cmd/mi/twitter.go

51 lines
1.3 KiB
Go

package main
import (
"context"
"flag"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
"within.website/ln"
)
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")
)
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
}
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
}