-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
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
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
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"` | ||
} |
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
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 | ||
} |
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
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 | ||
} |