jdelamater99 / Twitter-RSS-Parser

Convert Twitter API v1.1 JSON to RSS
Other
69 stars 18 forks source link

Format the tweet links according to the t.co recommendations #10

Open ianchanning opened 4 years ago

ianchanning commented 4 years ago

Question from @patrickbootscec in https://github.com/jdelamater99/Twitter-RSS-Parser/issues/8#issuecomment-673584226 that should be a separate issue:

I was looking at the JSON that I guess the Twitter API is providing us and I noticed that there's a condition for "expanded_url" and "url" if the Tweet has a link in it. The documentation explains these differences, https://developer.twitter.com/en/docs/tco and also explains the "t.co" formats.

For the Twitter to RSS Parser, the function "processString" appears to handle this sort of thing:

Twitter-RSS-Parser/functions.php

https://github.com/jdelamater99/Twitter-RSS-Parser/blob/c159d164a17b6da8e64617c11def953d4a474644/functions.php#L69

Is there a way to use the "expanded_url" condition for the text displayed?

ianchanning commented 4 years ago

It looks like you could do a search and replace in the text via the urls. This is the example JSON they provide in the earlier extended tweets documentation ( https://developer.twitter.com/en/docs/twitter-api/v1/tweet-updates#chars280 ):

  "extended_tweet": {
    "full_text": "Can’t fit your Tweet into 140 characters? 🤔\n\nWe’re trying something new with a small group, and increasing the character limit to 280! Excited about the possibilities? Read our blog to find out how it all adds up. 👇\nhttps://t.co/C6hjsB9nbL",
    "display_text_range": [
      0,
      239
    ],
    "entities": {
      "hashtags": [

      ],
      "urls": [
        {
          "url": "https://t.co/C6hjsB9nbL",
          "expanded_url": "https://cards.twitter.com/cards/gsby/4ubsj",
          "display_url": "cards.twitter.com/cards/gsby/4ub…",
          "unwound": {
            "url": "https://cards.twitter.com/cards/gsby/4ubsj",
            "status": 200,
            "title": "Giving you more characters to express yourself",
            "description": null
          },
          "indices": [
            216,
            239
          ]
        }
      ],

You should be able to loop through the extended_tweet and replace all the urls found in the urls array with a formatted link based on their suggestion:

  • Use the “url” value of a URL entity for the href attribute of an anchor tag.
  • Use the “expanded_url” value of a URL entity for the title attribute of an anchor tag, so that when users hover over the link they see the fully expanded URL.
  • Use the “display_url” value of a URL entity for the displayed text between opening & closing anchor tags. If you prefer not to display the truncated format, use the full “expanded_url” instead.

So something like <a href="$url['url']" title="$url['expanded_url']>$url['display_url']</a>

Unfortunately I haven't got the time to get more involved but I think that's the basic solution