michimani / gotwi

A library for using the Twitter API v2 in the Go language. (It is still under development).
MIT License
127 stars 26 forks source link

Add Support for Handling Long Tweets with note_tweet Field #313

Closed Nhypocrite closed 5 months ago

Nhypocrite commented 5 months ago

Description:

This pull request added support for Twitter API's new feature, the note_tweet field, which allows our library to conveniently obtain the full text of long tweets.

Issue related:

Issue #312

Changes Made

To support the handling of long tweets, we have modified the TweetField structure and the Tweet structure in our library:

  1. Add TweetFieldNoteTweet as an available TweetField when submitting a request:

    TweetFieldNoteTweet TweetField = "note_tweet"
  2. Update the Tweet structure to include the NoteTweet field:

    type Tweet struct {
        // ...
        NoteTweet *TweetNoteTweet `json:"note_tweet,omitempty"`
    }
    
    type TweetNoteTweet struct {
        Entities struct {
            CashTags []TweetEntityTag `json:"cashtags"`
            HashTags []TweetEntityTag `json:"hashtags"`
            Mentions []TweetEntityTag `json:"mentions"`
            URLs     []URL            `json:"urls"`
        } `json:"entities"`
        Text *string `json:"text"`
    }

Test

To ensure the changes work as expected, I have written and executed the following test code:

package main_test

import (
    "testing"
    "encoding/json"
    "fmt"
    "context"

    "github.com/michimani/gotwi/fields"
    "github.com/michimani/gotwi"
    "github.com/michimani/gotwi/tweet/tweetlookup"
    "github.com/michimani/gotwi/tweet/tweetlookup/types"
    "github.com/stretchr/testify/require"
)

var MY_Access_Token = ""

func TestLongTweetLookup(t *testing.T) {
    c, err := gotwi.NewClientWithAccessToken(&gotwi.NewClientWithAccessTokenInput{
        AccessToken: MY_Access_Token, // my own Bearer Token
    })
    require.NoError(t, err)

    input := &types.GetInput{
        ID: "1729154466539266176",
        TweetFields: fields.TweetFieldList{
            fields.TweetFieldNoteTweet,
        },
    }
    tweets, err := tweetlookup.Get(context.Background(), c, input)
    require.NoError(t, err)
    fmt.Println("result:")
    PrintStructAsJSON(tweets)
}

func PrintStructAsJSON(v interface{}) {
    jsonData, err := json.MarshalIndent(v, "", "  ")
    if err != nil {
        fmt.Println("Error marshalling to JSON:", err)
        return
    }
    fmt.Println(string(jsonData))
}

Test Results

=== RUN   TestLongTweetLookup
result:
{
  "data": {
    "id": "1729154466539266176",
    "text": "A Big Uranium SHORTAGE is coming in the next decade\n\nUranium mining has significant long-term potential\n\nMiners have run up by almost 50% over the last few months\n\nWe JUST downgraded our Uranium Miners rating from Buy to Neutral\n\nIf you have recently accumulated URA, it may be… https://t.co/pFmyrGoyqv https://t.co/dw2PGshiQr",
    "edit_history_tweet_ids": [
      "1729154466539266176"
    ],
    "note_tweet": {
      "entities": {
        "cashtags": null,
        "hashtags": null,
        "mentions": null,
        "urls": null
      },
      "text": "A Big Uranium SHORTAGE is coming in the next decade\n\nUranium mining has significant long-term potential\n\nMiners have run up by almost 50% over the last few months\n\nWe JUST downgraded our Uranium Miners rating from Buy to Neutral\n\nIf you have recently accumulated URA, it may be best to take some profit or hold on to your position instead of adding more\n\nWe're optimistic in the long term but not currently favorable to enter the trade\n\nGet a weekly update on Uranium Miners as part of our Asset Ratings\n\nEach week, we provide our members with an overview of the most attractive assets to long or short\n\nCyber Week is the best deal of the year to join Game of Trades\n\nGet 40% off our membership + a FREE 7-day trial\n\nDon't miss out on this opportunity!"
    }
  },
  "includes": {},
  "errors": null
}
--- PASS: TestLongTweetLookup (0.53s)
PASS
ok      github.com/michimani/gotwi/test 0.963s

As a newcomer to the open-source community, I am open to feedback and suggestions to improve this contribution. Thank you!

michimani commented 5 months ago

@NHypocrite

Thank you for your contribution! This PR is straightforward and well-implemented, so I'll be merging it directly. Your work is greatly appreciated.

We plan to release the updated version with your changes in the near future.