Skip to content

Commit

Permalink
Add initial client
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Aug 1, 2022
1 parent c96fa1e commit 2a1c949
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
"os"

"github.com/akiomik/get-old-tweets/config"
"github.com/akiomik/get-old-tweets/twitter"
"github.com/spf13/cobra"
)

var (
text string
text string
userAgent string
)

Expand All @@ -32,7 +33,16 @@ var rootCmd = &cobra.Command{
Short: "get-old-tweets " + config.Version,
Version: config.Version,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello, " + text)
client := twitter.NewClient()
json, err := client.Search(text)
if err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
}

for _, tweet := range json.GlobalObjects.Tweets {
fmt.Printf("%+v\n", tweet)
}
},
}

Expand Down
57 changes: 57 additions & 0 deletions twitter/adaptive_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 Akiomi Kamakura
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package twitter

type Tweet struct {
Id int64 `json:"id"`
UserId int64 `json:"user_id"`
FullText string `json:"full_text"`
RetweetCount int64 `json:"retweet_count"`
FavoriteCount int64 `json:"favorite_count"`
ReplyCount int64 `json:"reply_count"`
QuoteCount int64 `json:"quote_count"`
Geo string `json:"geo"`
Coodinates string `json:"coordinates"`
Place string `json:"place"`
Lang string `json:"lang"`
Source string `json:"source"`
CreatedAt RubyDate `json:"created_at"`
}

type User struct {
Id int64 `json:"id"`
Name string `json:"name"`
ScreenName string `json:"screen_name"`
Location string `json:"location"`
Description string `json:"description"`
Url string `json:"url"`
FollowersCount int64 `json:"followers_count"`
FriendsCount int64 `json:"friends_count"`
ListedCount int64 `json:"listed_count"`
FavouritesCount int64 `json:"favourites_count"`
StatusesCount int64 `json:"statuses_count"`
MediaCount int64 `json:"media_count"`
Verified bool `json:"verified"`
CreatedAt RubyDate `json:"created_at"`
}

type GlobalObjects struct {
Tweets map[string]Tweet `json:"tweets"`
Users map[string]User `json:"users"`
}

type AdaptiveJson struct {
GlobalObjects GlobalObjects `json:"globalObjects"`
}
85 changes: 85 additions & 0 deletions twitter/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2022 Akiomi Kamakura
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package twitter

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"

"github.com/akiomik/get-old-tweets/config"
)

type Client struct {
Client *http.Client
UserAgent string
}

func NewClient() *Client {
client := Client{}
client.Client = http.DefaultClient
client.UserAgent = "get-old-tweets/v" + config.Version

return &client
}

func (c *Client) get(url *url.URL) (*http.Response, error) {
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Set("Authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA")
req.Header.Set("x-guest-token", "1554100359735685121")

res, err := c.Client.Do(req)
if err != nil {
return nil, err
}

return res, nil
}

func (c *Client) Search(text string) (*AdaptiveJson, error) {
url, _ := url.Parse(
"https://twitter.com/i/api/2/search/adaptive.json" +
"?q=" + url.QueryEscape(text) +
"&include_quote_count=true" +
"&include_reply_count=1" +
"&tweet_mode=extended" +
"&count=100" +
"&query_source=typed_query" +
"&cursor=-1",
)
res, err := c.get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()

blob, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}

adaptiveJson := new(AdaptiveJson)
err = json.Unmarshal(blob, &adaptiveJson)
if err != nil {
return nil, err
}

return adaptiveJson, nil
}
33 changes: 33 additions & 0 deletions twitter/ruby_date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 Akiomi Kamakura
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package twitter

import (
"bytes"
"time"
)

type RubyDate time.Time

func (t *RubyDate) UnmarshalJSON(buf []byte) error {
s := bytes.Trim(buf, `"`)
parsed, err := time.Parse(time.RubyDate, string(s))
if err != nil {
return err
}

*t = RubyDate(parsed)
return nil
}

0 comments on commit 2a1c949

Please sign in to comment.