diff --git a/scraper/retweet.go b/scraper/retweet.go new file mode 100644 index 0000000..f90db4f --- /dev/null +++ b/scraper/retweet.go @@ -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 +} diff --git a/scraper/retweet_test.go b/scraper/retweet_test.go new file mode 100644 index 0000000..81d4327 --- /dev/null +++ b/scraper/retweet_test.go @@ -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()) + } +} diff --git a/scraper/test_responses/tweet_that_is_a_retweet.json b/scraper/test_responses/tweet_that_is_a_retweet.json new file mode 100644 index 0000000..2d4e187 --- /dev/null +++ b/scraper/test_responses/tweet_that_is_a_retweet.json @@ -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": "Twitter Web App", + "user_id_str": "44067298", + "retweeted_status_id_str": "1404269989646028804", + "retweet_count": 17, + "favorite_count": 0, + "conversation_id_str": "1404270043018448896", + "lang": "en" +}