Skip to content

Commit

Permalink
tweet recent search initial commit (#38)
Browse files Browse the repository at this point in the history
* tweet recent search initial commit

* added unit tests

* added a example

* typo fix

* added comments
  • Loading branch information
g8rswimmer authored Jan 5, 2021
1 parent ff9c93b commit 561ec6b
Show file tree
Hide file tree
Showing 7 changed files with 644 additions and 47 deletions.
62 changes: 62 additions & 0 deletions v2/_examples/tweet-recent-search/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

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

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

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 tweet ids.
**/
func main() {
token := flag.String("token", "", "twitter API token")
query := flag.String("query", "", "twitter query")
flag.Parse()

client := &twitter.Client{
Authorizer: authorize{
Token: *token,
},
Client: http.DefaultClient,
Host: "https://api.twitter.com",
}
opts := twitter.TweetRecentSearchOpts{
Expansions: []twitter.Expansion{twitter.ExpansionEntitiesMentionsUserName, twitter.ExpansionAuthorID},
TweetFields: []twitter.TweetField{twitter.TweetFieldCreatedAt, twitter.TweetFieldConversationID, twitter.TweetFieldAttachments},
}

fmt.Println("Callout to tweet recent search callout")

tweetResponse, err := client.TweetRecentSearch(context.Background(), *query, opts)
if err != nil {
log.Panicf("tweet lookup error: %v", err)
}

dictionaries := tweetResponse.Raw.TweetDictionaries()

enc, err := json.MarshalIndent(dictionaries, "", " ")
if err != nil {
log.Panic(err)
}
fmt.Println(string(enc))

metaBytes, err := json.MarshalIndent(tweetResponse.Meta, "", " ")
if err != nil {
log.Panic(err)
}
fmt.Println(string(metaBytes))
}
64 changes: 61 additions & 3 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)

const (
tweetMaxIDs = 100
userMaxIDs = 100
userMaxNames = 100
tweetMaxIDs = 100
userMaxIDs = 100
userMaxNames = 100
tweetRecentSearchQueryLength = 512
)

// Client is used to make twitter v2 API callouts.
Expand Down Expand Up @@ -212,3 +214,59 @@ func (c *Client) UserNameLookup(ctx context.Context, usernames []string, opts Us
Raw: raw,
}, nil
}

// TweetRecentSearch will return a recent search based of a query
func (c *Client) TweetRecentSearch(ctx context.Context, query string, opts TweetRecentSearchOpts) (*TweetRecentSearchResponse, error) {
switch {
case len(query) == 0:
return nil, fmt.Errorf("tweet recent search: a query is required: %w", ErrParameter)
case len(query) > tweetRecentSearchQueryLength:
return nil, fmt.Errorf("tweet recent search: the query over the length (%d): %w", tweetRecentSearchQueryLength, ErrParameter)
default:
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, tweetRecentSearchEndpoint.url(c.Host), nil)
if err != nil {
return nil, fmt.Errorf("tweet recent search request: %w", err)
}
req.Header.Add("Accept", "application/json")
c.Authorizer.Add(req)
opts.addQuery(req)
q := req.URL.Query()
q.Add("query", query)
req.URL.RawQuery = q.Encode()

resp, err := c.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("tweet recent search response: %w", err)
}
defer resp.Body.Close()

respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("tweet recent search response read: %w", err)
}
if resp.StatusCode != http.StatusOK {
e := &ErrorResponse{}
if err := json.Unmarshal(respBytes, e); err != nil {
return nil, fmt.Errorf("tweet recent search response error decode: %w", err)
}
e.StatusCode = resp.StatusCode
return nil, e
}

recentSearch := &TweetRecentSearchResponse{
Raw: &TweetRaw{},
Meta: &TweetRecentSearchMeta{},
}

if err := json.Unmarshal(respBytes, recentSearch.Raw); err != nil {
return nil, fmt.Errorf("tweet recent search raw response error decode: %w", err)
}

if err := json.Unmarshal(respBytes, recentSearch); err != nil {
return nil, fmt.Errorf("tweet recent search meta response error decode: %w", err)
}

return recentSearch, nil
}
Loading

0 comments on commit 561ec6b

Please sign in to comment.