Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Handling Long Tweets with note_tweet Field #313

Merged
merged 1 commit into from
Jun 25, 2024

Conversation

Nhypocrite
Copy link
Contributor

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 michimani self-assigned this Jun 25, 2024
@michimani michimani added the enhancement New feature or request label Jun 25, 2024
@michimani
Copy link
Owner

@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.

@michimani michimani merged commit 370f3eb into michimani:main Jun 25, 2024
1 check passed
@Nhypocrite Nhypocrite deleted the add-note_tweet-support branch June 25, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants