From 3b8c987bc77404329675387f5d4e4f579e63648d Mon Sep 17 00:00:00 2001 From: Alvin Reyes Date: Tue, 21 Jan 2025 11:55:49 -0500 Subject: [PATCH] feat: add scrape by tweet id (#16) * feat: add scrape by tweet id * use valid twitter id on the ut --- internal/jobs/twitter.go | 10 ++++++++++ internal/jobs/twitter/tweets.go | 17 +++++++++++++++++ internal/jobs/twitter_test.go | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/internal/jobs/twitter.go b/internal/jobs/twitter.go index bd22a6d..34938e7 100644 --- a/internal/jobs/twitter.go +++ b/internal/jobs/twitter.go @@ -185,6 +185,16 @@ func (ws *TwitterScraper) ExecuteJob(j types.Job) (types.JobResult, error) { return types.JobResult{ Data: dat, }, err + + case "getbyid": + tweet, err := twitter.ScrapeTweetByID(ws.configuration.DataDir, args.Query) + if err != nil { + return types.JobResult{Error: err.Error()}, err + } + dat, err := json.Marshal(tweet) + return types.JobResult{ + Data: dat, + }, err } // Do the web scraping here diff --git a/internal/jobs/twitter/tweets.go b/internal/jobs/twitter/tweets.go index ee43d36..d1a45f8 100644 --- a/internal/jobs/twitter/tweets.go +++ b/internal/jobs/twitter/tweets.go @@ -31,3 +31,20 @@ func ScrapeTweetsByQuery(baseDir string, query string, count int) ([]*TweetResul } return tweets, nil } + +func ScrapeTweetByID(baseDir string, tweetID string) (*twitterscraper.Tweet, error) { + scraper, account, err := getAuthenticatedScraper(baseDir) + if err != nil { + return nil, err + } + + tweet, err := scraper.GetTweet(tweetID) + if err != nil { + if handleRateLimit(err, account) { + return nil, err + } + return nil, err + } + + return tweet, nil +} diff --git a/internal/jobs/twitter_test.go b/internal/jobs/twitter_test.go index f3f21c8..43ba2bb 100644 --- a/internal/jobs/twitter_test.go +++ b/internal/jobs/twitter_test.go @@ -104,4 +104,23 @@ var _ = Describe("Twitter Scraper", func() { Expect(results[0].Username).ToNot(BeEmpty()) }) + + It("should get tweet by ID", func() { + res, err := twitterScraper.ExecuteJob(types.Job{ + Type: TwitterScraperType, + Arguments: map[string]interface{}{ + "type": "getbyid", + "query": "1881258110712492142", + }, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(res.Error).To(BeEmpty()) + + var tweet *twitterscraper.Tweet + res.Unmarshal(&tweet) + Expect(err).NotTo(HaveOccurred()) + Expect(tweet).NotTo(BeNil()) + Expect(tweet.ID).To(Equal("1234567890")) + Expect(tweet.Text).NotTo(BeEmpty()) + }) })