KyleMit / eleventy-plugin-embed-tweet

A plugin for embedding tweets on the server side during build time
https://www.npmjs.com/package/eleventy-plugin-embed-tweet
25 stars 4 forks source link

Update to use Twitter API v2 #15

Open KyleMit opened 2 years ago

KyleMit commented 2 years ago

https://developer.twitter.com/en/docs/twitter-api

brycewray commented 2 years ago

In my own Hugo shortcode for using API v2, I've found that the JSON data (for both API v2 and v1.1) omits the right data for handling "attached" animated GIFs, so I simply don't show them. In an Eleventy repo using your current plugin with v1.1, only the GIF's static preview appears rather than animation.

For reference, the tweet in which I discovered this is: https://twitter.com/podcasterJay/status/1493660193653207040

— embedded on my site (currently using Hugo and the GIF-excluding shortcode) in: https://www.brycewray.com/posts/2022/02/gems-in-rough-14/#learning-from-a-friendly-hat-tip

BTW, if you can find the right JSON data to make this work, I'd surely appreciate your sharing it. I went through everything (at least relevant to media) that the Twitter Developer API Tool offers, and could see data for only the static preview, not the actual animated GIF:

{
  "data": [
    {
      "text": "Thanks to some of the work by @BryceWrayTX, I just made the @wafflingTaylors website even faster and safer for its users\n(embedded Tweets which are generated at build time).\nI'm really grateful for their sharing of how to do this. https://t.co/GTtPejFT8N",
      "created_at": "2022-02-15T18:55:15.000Z",
      "entities": {
        "urls": [
          {
            "start": 231,
            "end": 254,
            "url": "https://t.co/GTtPejFT8N",
            "expanded_url": "https://twitter.com/podcasterJay/status/1493660193653207040/photo/1",
            "display_url": "pic.twitter.com/GTtPejFT8N"
          }
        ],
        "mentions": [
          {
            "start": 30,
            "end": 42,
            "username": "BryceWrayTX",
            "id": "2391572880"
          },
          {
            "start": 60,
            "end": 76,
            "username": "wafflingTaylors",
            "id": "817863617181839360"
          }
        ]
      },
      "author_id": "1244885475372007424",
      "id": "1493660193653207040",
      "attachments": {
        "media_keys": [
          "16_1493660186841698315"
        ]
      }
    }
  ],
  "includes": {
    "media": [
      {
        "height": 480,
        "type": "animated_gif",
        "preview_image_url": "https://pbs.twimg.com/tweet_video_thumb/FLqMBrPX0AsT1BK.jpg",
        "media_key": "16_1493660186841698315"
      }
    ],
    "users": [
      {
        "url": "https://t.co/XhJtOFOGCm",
        "id": "1244885475372007424",
        "name": "podcasterJay",
        "entities": {
          "url": {
            "urls": [
              {
                "start": 0,
                "end": 23,
                "url": "https://t.co/XhJtOFOGCm",
                "expanded_url": "https://about.me/thejamietaylor",
                "display_url": "about.me/thejamietaylor"
              }
            ]
          },
          "description": {
            "mentions": [
              {
                "start": 28,
                "end": 43,
                "username": "dotnetcoreshow"
              },
              {
                "start": 56,
                "end": 72,
                "username": "wafflingTaylors"
              },
              {
                "start": 77,
                "end": 91,
                "username": "tabsnspacesHQ"
              }
            ]
          }
        },
        "created_at": "2020-03-31T07:13:29.000Z",
        "username": "podcasterJay",
        "profile_image_url": "https://pbs.twimg.com/profile_images/1366867879656841218/6h-u1pKT_normal.jpg"
      },
      {
        "url": "https://t.co/aQxMw3Dx7S",
        "id": "2391572880",
        "name": "Bryce Wray",
        "entities": {
          "url": {
            "urls": [
              {
                "start": 0,
                "end": 23,
                "url": "https://t.co/aQxMw3Dx7S",
                "expanded_url": "https://brycewray.com",
                "display_url": "brycewray.com"
              }
            ]
          }
        },
        "created_at": "2014-03-15T20:24:32.000Z",
        "username": "BryceWrayTX",
        "profile_image_url": "https://pbs.twimg.com/profile_images/1213524547057655810/kjMG8wu3_normal.jpg"
      },
      {
        "url": "https://t.co/8X5Nf097LT",
        "id": "817863617181839360",
        "name": "The Waffling Taylors",
        "entities": {
          "url": {
            "urls": [
              {
                "start": 0,
                "end": 23,
                "url": "https://t.co/8X5Nf097LT",
                "expanded_url": "https://wafflingtaylors.rocks",
                "display_url": "wafflingtaylors.rocks"
              }
            ]
          },
          "description": {
            "urls": [
              {
                "start": 85,
                "end": 108,
                "url": "https://t.co/8X5Nf097LT",
                "expanded_url": "https://wafflingtaylors.rocks",
                "display_url": "wafflingtaylors.rocks"
              }
            ]
          }
        },
        "created_at": "2017-01-07T22:40:56.000Z",
        "username": "wafflingTaylors",
        "profile_image_url": "https://pbs.twimg.com/profile_images/1460863675821142021/pap49yi1_normal.jpg"
      }
    ]
  }
}
samdking commented 2 years ago

Hi Kyle,

As I understand it, I need to sign up and apply for "elevated access" to get access to the v1.1 endpoints used in this package. Is there a plan to upgrade to use v2 endpoints, or would you recommend I go down the route of requesting elevated access?

Cheers.

KyleMit commented 2 years ago

@samdking - thanks for checking. I've been sleeping on this for a while - I'll try to poke at it this weekend and see what the migration takes and report back if there's any big blockers

brycewray commented 2 years ago

@KyleMit Here’s something I found today in the Twitter documentation about the media object in the v2 API that may make you not want to go ahead with this just yet: V2 doesn't yet support animation in tweets.

Note that video URLs are not currently available, only static images.

Not sure this would be a blocker to you, but just FYI.