Skip to content

Commit

Permalink
Feature/user timeline (g8rswimmer#21)
Browse files Browse the repository at this point in the history
* added the user timeline tweets

* changed the response to timeline

* added the user timeline mentions API callout
  • Loading branch information
g8rswimmer authored Dec 24, 2020
1 parent a0d7337 commit 0188b33
Show file tree
Hide file tree
Showing 8 changed files with 795 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ The user lookup API example is located [here](./_examples/user/lookup-name)
The user following API example is located [here](./_examples/user/following)

#### Followers by User Id
The user followers API example is located [here](./_examples/user/followers)
The user followers API example is located [here](./_examples/user/followers)

#### Tweet Timeline by User Id
The user tweet timeline API example is located [here](./_examples/user/tweets)

#### Mention Timeline by User Id
The user tweet timeline API example is located [here](./_examples/user/mentions)
124 changes: 124 additions & 0 deletions _examples/user/mentions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"

"github.com/g8rswimmer/go-twitter"
)

type authorize struct {
Token string
}

func (a authorize) Add(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
}

/**
In order to run, the user will need to provide the bearer token and the list of ids.
**/
func main() {
token := flag.String("token", "", "twitter API token")
id := flag.String("id", "", "user id")
flag.Parse()

user := &twitter.User{
Authorizer: authorize{
Token: *token,
},
Client: http.DefaultClient,
Host: "https://api.twitter.com",
}
tweetOpts := twitter.UserTimelineOpts{
TweetFields: []twitter.TweetField{
twitter.TweetFieldAttachments,
twitter.TweetFieldAuthorID,
twitter.TweetFieldContextAnnotations,
twitter.TweetFieldConversationID,
twitter.TweetFieldCreatedAt,
twitter.TweetFieldEntities,
twitter.TweetFieldGeo,
twitter.TweetFieldID,
twitter.TweetFieldInReplyToUserID,
twitter.TweetFieldLanguage,
twitter.TweetFieldPossiblySensitve,
twitter.TweetFieldPublicMetrics,
twitter.TweetFieldReferencedTweets,
twitter.TweetFieldSource,
twitter.TweetFieldText,
},
UserFields: []twitter.UserField{
twitter.UserFieldCreatedAt,
twitter.UserFieldDescription,
twitter.UserFieldEntities,
twitter.UserFieldLocation,
twitter.UserFieldName,
twitter.UserFieldPinnedTweetID,
twitter.UserFieldProfileImageURL,
twitter.UserFieldProtected,
twitter.UserFieldURL,
twitter.UserFieldUserName,
twitter.UserFieldVerified,
twitter.UserFieldWithHeld,
},
Expansions: []twitter.Expansion{
twitter.ExpansionAuthorID,
twitter.ExpansionReferencedTweetsID,
twitter.ExpansionReferencedTweetsIDAuthorID,
twitter.ExpansionEntitiesMentionsUserName,
twitter.ExpansionAttachmentsMediaKeys,
twitter.ExpansionInReplyToUserID,
twitter.ExpansionGeoPlaceID,
},
PlaceFields: []twitter.PlaceField{
twitter.PlaceFieldContainedWithin,
twitter.PlaceFieldCountry,
twitter.PlaceFieldCountryCode,
twitter.PlaceFieldFullName,
twitter.PlaceFieldGeo,
twitter.PlaceFieldID,
twitter.PlaceFieldName,
twitter.PlaceFieldPlaceType,
},
PollFields: []twitter.PollField{
twitter.PollFieldDurationMinutes,
twitter.PollFieldEndDateTime,
twitter.PollFieldID,
twitter.PollFieldOptions,
twitter.PollFieldVotingStatus,
},
MaxResults: 10,
}

userTweets, err := user.Mentions(context.Background(), *id, tweetOpts)
var tweetErr *twitter.TweetErrorResponse
switch {
case errors.As(err, &tweetErr):
printTweetError(tweetErr)
case err != nil:
fmt.Println(err)
default:
printUserTweets(userTweets)
}

}
func printUserTweets(userTweets *twitter.UserTimeline) {
enc, err := json.MarshalIndent(userTweets, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(enc))
}

func printTweetError(tweetErr *twitter.TweetErrorResponse) {
enc, err := json.MarshalIndent(tweetErr, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(enc))
}
124 changes: 124 additions & 0 deletions _examples/user/tweets/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"

"github.com/g8rswimmer/go-twitter"
)

type authorize struct {
Token string
}

func (a authorize) Add(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
}

/**
In order to run, the user will need to provide the bearer token and the list of ids.
**/
func main() {
token := flag.String("token", "", "twitter API token")
id := flag.String("id", "", "user id")
flag.Parse()

user := &twitter.User{
Authorizer: authorize{
Token: *token,
},
Client: http.DefaultClient,
Host: "https://api.twitter.com",
}
tweetOpts := twitter.UserTimelineOpts{
TweetFields: []twitter.TweetField{
twitter.TweetFieldAttachments,
twitter.TweetFieldAuthorID,
twitter.TweetFieldContextAnnotations,
twitter.TweetFieldConversationID,
twitter.TweetFieldCreatedAt,
twitter.TweetFieldEntities,
twitter.TweetFieldGeo,
twitter.TweetFieldID,
twitter.TweetFieldInReplyToUserID,
twitter.TweetFieldLanguage,
twitter.TweetFieldPossiblySensitve,
twitter.TweetFieldPublicMetrics,
twitter.TweetFieldReferencedTweets,
twitter.TweetFieldSource,
twitter.TweetFieldText,
},
UserFields: []twitter.UserField{
twitter.UserFieldCreatedAt,
twitter.UserFieldDescription,
twitter.UserFieldEntities,
twitter.UserFieldLocation,
twitter.UserFieldName,
twitter.UserFieldPinnedTweetID,
twitter.UserFieldProfileImageURL,
twitter.UserFieldProtected,
twitter.UserFieldURL,
twitter.UserFieldUserName,
twitter.UserFieldVerified,
twitter.UserFieldWithHeld,
},
Expansions: []twitter.Expansion{
twitter.ExpansionAuthorID,
twitter.ExpansionReferencedTweetsID,
twitter.ExpansionReferencedTweetsIDAuthorID,
twitter.ExpansionEntitiesMentionsUserName,
twitter.ExpansionAttachmentsMediaKeys,
twitter.ExpansionInReplyToUserID,
twitter.ExpansionGeoPlaceID,
},
PlaceFields: []twitter.PlaceField{
twitter.PlaceFieldContainedWithin,
twitter.PlaceFieldCountry,
twitter.PlaceFieldCountryCode,
twitter.PlaceFieldFullName,
twitter.PlaceFieldGeo,
twitter.PlaceFieldID,
twitter.PlaceFieldName,
twitter.PlaceFieldPlaceType,
},
PollFields: []twitter.PollField{
twitter.PollFieldDurationMinutes,
twitter.PollFieldEndDateTime,
twitter.PollFieldID,
twitter.PollFieldOptions,
twitter.PollFieldVotingStatus,
},
MaxResults: 10,
}

userTweets, err := user.Tweets(context.Background(), *id, tweetOpts)
var tweetErr *twitter.TweetErrorResponse
switch {
case errors.As(err, &tweetErr):
printTweetError(tweetErr)
case err != nil:
fmt.Println(err)
default:
printUserTweets(userTweets)
}

}
func printUserTweets(userTweets *twitter.UserTimeline) {
enc, err := json.MarshalIndent(userTweets, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(enc))
}

func printTweetError(tweetErr *twitter.TweetErrorResponse) {
enc, err := json.MarshalIndent(tweetErr, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(enc))
}
2 changes: 1 addition & 1 deletion common_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type EntityURLObj struct {
URL string `json:"url"`
ExpandedURL string `json:"expanded_url"`
DisplayURL string `json:"display_url"`
Status string `json:"status"`
Status int `json:"status"`
Title string `json:"title"`
Desription string `json:"description"`
UnwoundURL string `json:"unwound_url"`
Expand Down
19 changes: 19 additions & 0 deletions exclude.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package twitter

// Exclude used in the timeline parameters
type Exclude string

const (
// ExcludeRetweets will exclude the tweet retweets
ExcludeRetweets Exclude = "retweets"
// ExcludeReplies will exclude the tweet replies
ExcludeReplies Exclude = "replies"
)

func excludetringArray(arr []Exclude) []string {
strs := make([]string, len(arr))
for i, field := range arr {
strs[i] = string(field)
}
return strs
}
Loading

0 comments on commit 0188b33

Please sign in to comment.