Here is an example of a very long post title:

Braveheart is an Academy Award-Winning 1995 historical action-drama movie produced and directed by Mel Gibson who also starred in the title role

This title is 144 characters long, 4 more than the maximum allowed by Twitter. If you were to use the default format string New post: !title !url the Twitter message would look like this:

New post: Braveheart is an Academy Award-Winning 1995 historical action-drama movie produced and directed by Mel Gibson who also starred in ...

This is bad because the URL is omitted from the message. Ideally, in the case of long titles, we would want to truncate the title and keep the TinyURL:

New post: Braveheart is an Academy Award-Winning 1995 historical action-drama movie produced and directed by ... http://tinyurl.com/aj8u2v

Comments

todd nienkerk’s picture

Assigned: Unassigned » todd nienkerk
Status: Active » Needs review
StatusFileSize
new6.47 KB

The attached patch does many things, all of which are needed to provide good truncation logic.

  • Only the !title of the tweet is truncated to fit the 140 character limit (username and URLs are not truncated).
  • All URLs greater than 30 characters are converted to tinyurls inside the module -- not by Twitter. This allows us to predict the amount of space we can fill for the title. (I picked 30 characters because I read somewhere that Twitter uses that same number to determine which URLs it will "tiny.")
  • Only one URL replacement string is honored. The order of preference is: !tinyurl, !url-alias, !url. Remaining URL replacement strings are replaced with an empty string.

It's kind of confusing, so any help making it more concise or efficient is much appreciated.

todd nienkerk’s picture

Version: 6.x-2.0 » 6.x-2.x-dev

The patch applies to the 6.x-2.x-dev snapshot, not the 6.x-2.0 release.

todd nienkerk’s picture

StatusFileSize
new6.29 KB

I've attached a better, more streamlined patch. Logic is simpler, and there are fewer vars.

Patch applies to 6.x-2.x-dev 2009-Jan-17 (not against my previous patch).

eaton’s picture

Todd, good catch on the title length issue. I'm a little concerned, though, that all of this extra code is essentially what token module provides; it was designed explicitly for this sort of thing, and already handles extracting the data we're talking about pulling up.

I'd much prefer to just add optional support for node tokens to the outgoing post, and expose a 'tinyurl' token for all nodes. Would there be objections to that approach?

todd nienkerk’s picture

@Eaton: Yeah, it's a lot of code. There's a lot of logic to it because it involves predicting how many characters will be available after all !strings are replaced. I'm not a seasoned developer, though, so I'm sure you can come up with a more efficient way to do this!

I have no objects to a token approach so long as it can:

  1. calculate available space when !strings removed
  2. create tinyurls if *prior to* calculating available space
  3. create tinyurls for any URL longer than 30 chars regardless of method chosen by the user (!url, !url-alias, !tinyurl)
  4. truncate the title to make room for anything else
  5. add an ellipsis to the truncated title
  6. display the title only if something like 10 character spaces are available (this allows an ellipsis plus seven title chars)

One question regarding the token approach: Wouldn't it be better to build a tinyurl module that provides the token? Maybe a module that supports multiple URL-shortening services? tinyurl is losing mindshare to other, more concise services: bit.ly, is.gd, etc. Here's an annoyingly long list of such services.

PS. We're using the patched version of the module on the Four Kitchens site: twitter.com/fourkitchens. We're happy to test any improvements or alternative methods.

todd nienkerk’s picture

StatusFileSize
new5.67 KB

Re-rolled patch for the 2009-02-10 snapshot of 6.x-2.x-dev.

greggles’s picture

My initial reaction when I saw the title of this issue was "all we need is a [title-X-chars] token that has reasonable X values like 20, 30, 50, and then that token takes care of truncating/ellipses.

Looking at the discussion and proposed patch here...that would indeed be a lot less code. It does require the administrator to be somewhat smart when they setup the pattern to make sure that their tokens will all work, but...that's why they get paid the big bucks to be the admin, right?

I think that it would make sense for twitter to have a hook just after the message has been built (which would receive the pattern, the tokens, and the current replacement) so that another module could jump in and do really smart things like truncating specific tokens to fit better. Putting all that logic into token seems hard because the "right thing to do" depends on so many variables...

Leeteq’s picture

Subscribing.

todd nienkerk’s picture

@greggles:

I absolutely agree there's potential for Token to handle much of the grunt work. But whatever the solution, this module -- which I use all the time and love! -- must "protect" the URL. After all, it's the most important part of any "check out my new post"-style tweet.

The rules for creating a successful Drupal post tweet are:

(1) Create the shortest URL possible to maximize the number of other characters in the tweet. (In some rare cases, this may be the original URL. Usually, the TinyURL is more efficient.)
(2) Calculate the number of non-URL and non-title characters are in the tweet, including spaces that must separate the elements. Example: "New post!"
(3) Fit the post's title into the remaining gap. Bonus points for truncating between words instead of mid-wo...

todd nienkerk’s picture

Version: 6.x-2.x-dev » 6.x-2.3
StatusFileSize
new5.46 KB

Re-rolled patch for 6.x-2.3.

michaek’s picture

Status: Needs review » Closed (won't fix)
bkosborne’s picture

Why was this set to won't fix?

greggles’s picture

Status: Closed (won't fix) » Needs review

Agreed.

steinmb’s picture

Version: 6.x-2.3 » 6.x-3.x-dev

Agree, moving to dev. Anyone feel up to rerolling this?

davidneedham’s picture

Status: Needs review » Patch (to be ported)

It's been awhile, but this still seems valid.

Possible cross-reference: #1441596: Add support for tokens module on twitter post.

davidneedham’s picture

Version: 6.x-3.x-dev » 6.x-5.x-dev
Status: Patch (to be ported) » Needs work

Oops, wrong status.

Also, as I understand it the code was re-written significantly since 6.x-3.x, so it needs to be rolled up before it'll be ready for review.

13rac1’s picture

Patch does not apply to 6.x-5.x.

$ git apply twitter_truncation_4.patch 
error: patch failed: twitter.module:115
error: twitter.module: patch does not apply
$ patch -p0 < twitter_truncation_4.patch 
patching file twitter.module
Hunk #1 FAILED at 115.
1 out of 1 hunk FAILED -- saving rejects to file twitter.module.rej
xurizaemon’s picture

Version: 6.x-5.x-dev » 7.x-5.x-dev
Issue summary: View changes
Status: Needs work » Closed (works as designed)

Re-roll welcome, but Twitter does its own shortening stunts now so let's drop the tinyurl remnants if there are any.