Port Wordpress' wicked awesome "Tweet This" plugin to Drupal

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
bflora's picture

Nowadays its more important than ever to get your users easy ways to share content on other sites.

I want the users of my D5 site to be able to click a "Tweet This" link under my posts and be taken to their twitter home pages with the post form auto-filled in with the post title and a tinyurl to the original post.

Someone's created a Wordpress plugin that does just that: http://richardxthripp.thripp.com/tweet-this

I'd love to see this ported over to Drupal. I have no clue how to do it myself, but it'd be a win for the community. The code's there, it just needs to be "drupalized,"

Comments

.

Michelle's picture

http://drupal.org/project/addthis already includes Twitter.

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

Hey Michelle, Thanks for

bflora's picture

Hey Michelle, Thanks for pointing that out.

I looked into it a bit.

  1. You have to use it as part of the add-this bar. Does look like you can break it out.

  2. The implementation is pretty poor. It just takes you to your twitter account and autocompletes the posting form with the raw URL to your post. The Wordpress plugin creates the TInyURL on the fly and autopopulates with the post title. very cool!

So I'd argue there's still a need there.

.

Michelle's picture

Sounds cool but then you get one that will post to facebook and one that will post to myspace and one that will post to stumble and suddenly you have 100 modules installed and a link section that takes the whole page. That's why I suggested "add this". It may not be quite as fancy, but you get them all in one link.

This is all rather academic, though, since you said you don't know how to write it. Posting wishlists usually doesn't go anywhere anyway.

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

It's your lucky day

IceCreamYou's picture

I decided writing code to do this would be more interesting than whatever I was doing before. It's possible someone's already written this code for a module before.

You can put all of this code in a block if you'd like. I recommend putting it in a module (called tweet.module) instead, if you know how to do that, because then it will be included on every page, and you can call it in your template for example if you want.

This will probably work on both D5 and D6.

<?php
//If you put all of this in a module, don't include this line.  Instead, this is all you need to put wherever you want the button to appear.
echo tweet_to_twitter();

function
tweet_to_twitter($title = TRUE, $nid = '', $q = '', $file = 'install.php') {
 
$tweet = tweet_make_url($q, $file);
  if (
$title) {
   
$tweet .= ' '. tweet_get_title($nid);
  }
 
$tweet = drupal_urlencode($tweet);
 
$path = 'http://twitter.com/home';
 
$image = '<img src="http://assets1.twitter.com/images/favicon.ico" alt="Post to Twitter" />';
 
$attributes = array('target' => '_blank', 'title' => 'Post to Twitter');
  return
l($image, $path, $attributes, 'status='. $tweet, NULL, TRUE, TRUE);
}

function
tweet_get_title($nid = '') {
  if (!
$nid) {
   
$nid = arg(1);
  }
 
$node = node_load(array('nid' => $nid));
 
$title = $node->title;
  if (
drupal_strlen($title) > 120) {
   
$title = drupal_substr($title, 0, 119) .'…';
  }
  return
$title;
}

function
tweet_make_url($q = '', $file = 'install.php') {
  if (!
$q) {
   
$q = tweet_get_url($file);
  }
 
$url = file_get_contents('http://th8.us/api.php?url='. $q);
 
$url = drupal_substr($url, 7);
 
$url = 'www.'. $url;
  return
$url;
}

//Stolen from drupal_detect_baseurl because that function is only included when install.inc is included.
//If anyone knows a better way to do this, please comment.
function tweet_get_url($file = 'install.php') {
  global
$profile;
 
$proto = $_SERVER['HTTPS'] ? 'https://' : 'http://';
 
$host = $_SERVER['SERVER_NAME'];
 
$port = ($_SERVER['SERVER_PORT'] == 80 ? '' : ':'. $_SERVER['SERVER_PORT']);
 
$uri = preg_replace("/\?.*/", '', $_SERVER['REQUEST_URI']);
 
$dir = str_replace("/$file", '', $uri);
  return
"$proto$host$port$dir";
}
?>

Sure, prove me wrong...

Michelle's picture

Ok, usually posting a module wish doesn't make the module fairy appear to write it for you. :P

Does this mean I get a pony now? :)

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

Hahaha

IceCreamYou's picture

A pony is slightly more difficult to code.

            .---.
           /   .  --.
  ________/          \
/                    /
|                 --
|  ____________.-`
| |         | |
|_|         |_|

Eh. I tried. :P

Beat me to

bflora's picture

Beat me to it!

                             //|                              ,|
                           //,/                             -~ |
                         // / |                         _-~   /  ,
                       /'/ / /                       _-~   _/_-~ |
                      ( ( / /'                   _ -~     _-~ ,/'
                       \~\/'/|             __--~~__--\ _-~  _/,
               ,,)))))));, \/~-_     __--~~  --~~  __/~  _-~ /
            __))))))))))))));,>/\   /        __--~~  \-~~ _-~
           -\(((((''''(((((((( >~\/     --~~   __--~' _-~ ~|
  --==//////((''  .     `)))))), /     ___---~~  ~~\~~__--~
          ))| @    ;-.     (((((/           __--~~~'~~/
          ( `|    /  )      )))/      ~~~~~__\__---~~__--~~--_
             |   |   |       (/      ---~~~/__-----~~  ,;::'  \         ,
             o_);   ;        /      ----~~/           \,-~~~\  |       /|
                   ;        (      ---~~/         `:::|      |;|      < >
                  |   _      `----~~~~'      /      `:|       \;\_____//
            ______/\/~    |                 /        /         ~------~
          /~;;.____/;;'  /          ___----(   `;;;/
         / //  _;______;'------~~~~~    |;;/\    /
        //  | |                        /  |  \;;,\
       (<_  | ;                      /',/-----'  _>
        \_| ||_                     //~;~~~~~~~~~
            `\_|                   (,~~
                                    \~\

Wow!

Michelle's picture

Ok, that is awesome. Sorry, IceCreamYou but bflora upped my pony to a flying unicorn. Can't beat that. LOL

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

Holy moly

IceCreamYou's picture

I guess we know where our respective skill sets are... ;) That's hilariously awesome.

Open source for the

bflora's picture

Open source for the win!

Thank you so much.

Could I drop this into my theme template file and then just call tweet_to_twitter()?

I think I can follow what the functions are doing here, just not the first one. What's with the install.php stuff?

I think so, if you got rid

IceCreamYou's picture

I think so, if you got rid of the first line (with the echo).

The install.php is just the name of a file in Drupal's root directory. It's used to determine what your website's URL is. Without it, all Drupal can figure out (to the best of my knowledge) is what comes after the .com/.org/etc.; for example we could use base_path() on this page to get node/17581 but we have to call tweet_get_url() to get http://groups.drupal.org/...

Of course, since you're not redistributing this, you could just go ahead and change tweet_get_url() to return 'http://example.com/'. base_path(); (with your site instead of example.com) and cut out all the nasty stuff.

Ok

bflora's picture

That makes total sense. So it's just a way to figure out base URL, which I can hard-write into the function since I'm not looking to create a general solution. Sweet. Thanks.

Yes...

IceCreamYou's picture

But after playing around with this for much longer than I really had time to, I realized that global $base_url is a much better way to go. :D

.

IceCreamYou's picture

I went ahead and made a module for this: http://drupal.org/project/tweet

Unfortunately, I never really understood the whole CVS thing, and I somehow managed to put the files somewhere that appears not to exist in CVS. So I created my own tarball to allow people to download the module anyway, accessible from the project page, and if someone would like to have temporary CVS access to the module to get the files where they need to go, I would be much obliged. :P It's official-release ready.

If you're bored...

Michelle's picture

Even more than a pony, I'd love a script / module that will take all the tweets of people I am following, including my own, minus people who have their tweets set to private, and be able to display that on my site. I had that as a feature request on the Twitter module but never got a response. I tried working with the API myself but never figured it out. :(

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

hahaha

IceCreamYou's picture

That doesn't really have anything to do with tweet.module, nor do I really have any motivation to write it or ability to test it since I hardly use Twitter... but if I have nothing better to do I might take a shot at it. ;) Not likely that I won't have anything more pressing to do unfortunately. But it doesn't sound too hard: curl -u username:password http://twitter.com/statuses/friends_timeline.xml

I don't know anything about using curl in scripts though.

I figured out my CVS issues by the way. All should be good now--there's an official release of the Tweet module on the project page.

Heh

Michelle's picture

"That doesn't really have anything to do with tweet.module"

No, it really doesn't. It's just so rare that someone says "I'd like this module" and someone comes along and writes it for them. LOL! I thought maybe I might get lucky. If you don't have time or interest, that's fine. Just taking a shot. :) It's one of those things that I'll take the time to figure out how to write "someday".

Thanks anyway,

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

.

IceCreamYou's picture

When's your birthday? It could happen... :P

But boy, do I know what you're talking about when you say "I'll get to it someday." I actually did finish my to-do list once. But I actually have two to-do lists: one of things that might actually get finished, and one of things that probably won't, like "write a book." But by the time I started on the first thing in the second list, I had dozens of things to put in the first list again.

Making progress I suppose.

LOL

Michelle's picture

November 1st. I'm hoping I have time to write it myself before then. :)

I've never even come close to finishing my to do list. I just keep piling things on. This one is fairly low priority so it keeps getting pushed off. Basically, I have a Twitter account that only follows local coulee region folks. I'd like to be able to put that stream up on my site as a "local voices" kind of thing. I spent an hour or two a while back fiddling with the Twitter API and couldn't get it to work right. Maybe I'll pick it up again once I've gotten some of these other things knocked off the list. :)

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

This is a good

timmillwood's picture

This is a good read!

@michelle: I think it should be part of the Twitter module, ask Walkah!

Here is some code I have just whacked in a block on http://www.millwoodonline.co.uk to display the avatars of the people I am following. You may want to use it, or it may help you with your module.

<?php
# Requires cURL and SimpleXML!
#
# Fill in your twitter user name and password in these two rows
$username = "USERNAME";
$login = "USERNAME:PASSWORD";

$target = "http://twitter.com/statuses/friends/".$username.".xml";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$gettwit = curl_exec($ch);

$twitters = new SimpleXMLElement($gettwit);
## Printing/Dumping the data
foreach ($twitters->user as $twit) {
echo
"<a href=\"http://www.twitter.com/", $twit->screen_name, "\"><img class=\"twitter_followers\" src=\"", $twit->profile_image_url, "\" title=\"", $twit->name, " from ", $twit->location, "\" alt=\"", $twit->name, " from ", $twit->location, "\" /></a>\n";}

curl_close($ch);
?>