Add Retweet type
This commit is contained in:
parent
02a2a0be33
commit
5b7d3dcdc4
20
scraper/retweet.go
Normal file
20
scraper/retweet.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package scraper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Retweet struct {
|
||||||
|
RetweetID TweetID
|
||||||
|
TweetID TweetID
|
||||||
|
RetweetedBy UserID
|
||||||
|
RetweetedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseSingleRetweet(apiTweet APITweet) (ret Retweet, err error) {
|
||||||
|
ret.RetweetID = TweetID(apiTweet.ID)
|
||||||
|
ret.TweetID = TweetID(apiTweet.RetweetedStatusIDStr)
|
||||||
|
ret.RetweetedBy = UserID(apiTweet.UserIDStr)
|
||||||
|
ret.RetweetedAt, err = time.Parse(time.RubyDate, apiTweet.CreatedAt)
|
||||||
|
return
|
||||||
|
}
|
36
scraper/retweet_test.go
Normal file
36
scraper/retweet_test.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package scraper_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"offline_twitter/scraper"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseSingleRetweet(t *testing.T) {
|
||||||
|
data, err := ioutil.ReadFile("test_responses/tweet_that_is_a_retweet.json")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
var api_tweet scraper.APITweet
|
||||||
|
err = json.Unmarshal(data, &api_tweet)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
retweet, err := scraper.ParseSingleRetweet(api_tweet)
|
||||||
|
|
||||||
|
if retweet.RetweetID != scraper.TweetID("1404270043018448896") {
|
||||||
|
t.Errorf("Expected %q, got %q", scraper.TweetID("1404270043018448896"), retweet.RetweetID)
|
||||||
|
}
|
||||||
|
if retweet.TweetID != scraper.TweetID("1404269989646028804") {
|
||||||
|
t.Errorf("Expected %q, got %q", scraper.TweetID("1404269989646028804"), retweet.TweetID)
|
||||||
|
}
|
||||||
|
if retweet.RetweetedBy != "44067298" {
|
||||||
|
t.Errorf("Expected %q, got %q", "44067298", retweet.RetweetedBy)
|
||||||
|
}
|
||||||
|
if retweet.RetweetedAt.Unix() != 1623639042 {
|
||||||
|
t.Errorf("Expected %d, got %d", 1623639042, retweet.RetweetedAt.Unix())
|
||||||
|
}
|
||||||
|
}
|
34
scraper/test_responses/tweet_that_is_a_retweet.json
Normal file
34
scraper/test_responses/tweet_that_is_a_retweet.json
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"created_at": "Mon Jun 14 02:50:42 +0000 2021",
|
||||||
|
"id_str": "1404270043018448896",
|
||||||
|
"text": "RT @nofunin10ded: @michaelmalice We're dealing with people who will napalm your children and then laugh about it",
|
||||||
|
"entities": {
|
||||||
|
"user_mentions": [
|
||||||
|
{
|
||||||
|
"screen_name": "nofunin10ded",
|
||||||
|
"name": "Adam",
|
||||||
|
"id_str": "1265866918835236865",
|
||||||
|
"indices": [
|
||||||
|
3,
|
||||||
|
16
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"screen_name": "michaelmalice",
|
||||||
|
"name": "Michael Malice",
|
||||||
|
"id_str": "44067298",
|
||||||
|
"indices": [
|
||||||
|
18,
|
||||||
|
32
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>",
|
||||||
|
"user_id_str": "44067298",
|
||||||
|
"retweeted_status_id_str": "1404269989646028804",
|
||||||
|
"retweet_count": 17,
|
||||||
|
"favorite_count": 0,
|
||||||
|
"conversation_id_str": "1404270043018448896",
|
||||||
|
"lang": "en"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user