Last active
July 18, 2020 10:23
-
-
Save zaydek-old/0a4b6e28e803b594bab404c3bcc6b960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO | |
// | |
// add direct message e.g. | |
// | |
// - DirectMessage(userID, msg) | |
// - a.DirectMessageService.New | |
// - a.DirectMessageService.Destroy | |
// | |
// | |
// add undo fns e.g. | |
// | |
// - Stream ().Stop() ? | |
// - Tweet ().Undo() | |
// - Like ().Undo() | |
// - Retweet().Undo() | |
// - Follow ().Undo() | |
// - DM ().Undo() | |
// | |
// | |
// api e.g. | |
// | |
// - api/tweet/id | |
// - api/tweet/id/undo | |
// - api/like/id | |
// - api/like/id/undo | |
// - api/retweet/id | |
// - api/retweet/id/undo | |
// - api/follow/id | |
// - api/follow/id/undo | |
// - api/directmessage/id | |
// - api/directmessage/id/undo | |
// | |
// write tests | |
// write documentation | |
package main | |
import ( | |
"errors" | |
"log" | |
"os" | |
"github.com/dghubble/go-twitter/twitter" | |
"github.com/dghubble/oauth1" | |
) | |
const ( | |
CONS_KEY = "..." | |
CONS_SEC = "..." | |
ACCS_KEY = "..." | |
ACCS_SEC = "..." | |
) | |
var ( | |
stdout = log.New(os.Stdout, "", log.LstdFlags).Println | |
stderr = log.New(os.Stderr, "", log.LstdFlags).Println | |
) | |
// decorate decorates a non-nil error | |
func decorate(err error, str string) error { | |
if err == nil { | |
return nil | |
} | |
return errors.New(str + " (" + err.Error() + ")") | |
} | |
// info logs to stdout | |
func info(v ...interface{}) { | |
stdout(append([]interface{}{"(info)"}, v...)...) // prepend | |
} | |
// warn attempts to log to stderr | |
func warn(err error) { | |
if err != nil { | |
stderr("(warn)", err) | |
} | |
} | |
// fata attempts to log to stderr and exit | |
func fata(err error) { | |
if err != nil { | |
stderr("(fata)", err) | |
os.Exit(1) | |
} | |
} | |
func main() { | |
_, err := Auth() | |
warn(decorate(err, "_, err := Auth()")) | |
// tweet | |
// untweet | |
// like | |
// undolike | |
// retweet | |
// undoretweet | |
// follow | |
// undofollow | |
} | |
// godoc.org/github.com/dghubble/go-twitter/twitter#Client | |
// godoc.org/github.com/dghubble/go-twitter/twitter#User | |
type Account struct { | |
*twitter.Client | |
*twitter.User | |
} | |
// Client subroutine for Auth | |
// godoc.org/github.com/dghubble/go-twitter/twitter#hdr-Authentication | |
func authClient(a *Account) { | |
config := oauth1.NewConfig(CONS_KEY, CONS_SEC) | |
httpClient := config.Client(oauth1.NoContext, oauth1.NewToken(ACCS_KEY, ACCS_SEC)) | |
a.Client = twitter.NewClient(httpClient) | |
} | |
// User subroutine for Auth | |
// godoc.org/github.com/dghubble/go-twitter/twitter#AccountService.VerifyCredentials | |
func authUser(a *Account) error { | |
var err error | |
a.User, _, err = a.Accounts.VerifyCredentials(nil) | |
return err | |
} | |
// Auth authenticates an Account | |
func Auth() (*Account, error) { | |
acc := &Account{} | |
authClient(acc) | |
err := authUser(acc) | |
return acc, err | |
} | |
// func Auth() (*Account, error) { | |
// config := oauth1.NewConfig(CONS_KEY, CONS_SEC) | |
// token := oauth1.NewToken(ACCS_KEY, ACCS_SEC) | |
// httpClient := config.Client(oauth1.NoContext, token) | |
// client := twitter.NewClient(httpClient) | |
// user, _, err := client.Accounts.VerifyCredentials(nil) | |
// acc := &Account{client, user} | |
// return acc, err | |
// } | |
// has guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#StreamService.Filter | |
func (a *Account) Stream(terms []string) <-chan *twitter.Tweet { | |
params := &twitter.StreamFilterParams{Track: terms} | |
in, err := a.Streams.Filter(params) | |
fata(err) | |
out := make(chan *twitter.Tweet) | |
go func() { | |
defer func() { in.Stop(); close(out) }() | |
for msg := range in.Messages { | |
tweet, ok := msg.(*twitter.Tweet) | |
if !ok { | |
warn(err) | |
continue | |
} | |
out <- tweet | |
} | |
}() | |
return out | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#StatusService.Update | |
func (a *Account) Tweet(msg string) (*twitter.Tweet, error) { | |
tweet, _, err := a.Statuses.Update(msg, nil) | |
return tweet, err | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#StatusService.Destroy | |
func (a *Account) UndoTweet(tweet *twitter.Tweet) error { | |
_, _, err := a.Statuses.Destroy(tweet.ID, nil) | |
return err | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#FavoriteService.Create | |
func (a *Account) Like(tweet *twitter.Tweet) error { | |
params := &twitter.FavoriteCreateParams{ID: tweet.ID} | |
_, _, err := a.Favorites.Create(params) | |
return err | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#FavoriteService.Destroy | |
func (a *Account) UndoLike(tweet *twitter.Tweet) error { | |
_, _, err := a.Statuses.Destroy(tweet.ID, nil) | |
return err | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#StatusService.Retweet | |
func (a *Account) Retweet(tweet *twitter.Tweet) error { | |
_, _, err := a.Statuses.Retweet(tweet.ID, nil) | |
return err | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#StatusService.Unretweet | |
func (a *Account) UndoRetweet(tweet *twitter.Tweet) error { | |
_, _, err := a.Statuses.Unretweet(tweet.ID, nil) | |
return err | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#FriendshipService.Create | |
func (a *Account) Follow(tweet *twitter.Tweet) error { | |
params := &twitter.FriendshipCreateParams{UserID: tweet.User.ID} | |
_, _, err := a.Friendships.Create(params) | |
return err | |
} | |
// add guard | |
// godoc.org/github.com/dghubble/go-twitter/twitter#FriendshipService.Destroy | |
func (a *Account) UndoFollow(tweet *twitter.Tweet) error { | |
params := &twitter.FriendshipDestroyParams{UserID: tweet.User.ID} | |
_, _, err := a.Friendships.Destroy(params) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment