Tag Archives: Twitter

Creating Your Own Social Home

9 Mar

With the advancements in Social Media in the past decade, many of us now have “Social homes” on the internet. Personally I use Twitter and Facebook. These web services offer ways to interact with them and extract data from them without visiting the site itself. This is called an API (or Application Programming Interface).

You can think of an API as something like an RSS feed. If you read webdesignerer through a feed reader like Google Reader, you know that you don’t even need to visit my site to read my content, because that data is being displayed in another way. In the case of RSS, it’s an XML file formatted in a very specific way. API’s are often served up as XML. XML is nice, but much like HTML, it needs to be parsed before you can really do anything with it.

JSON (JavaScript Object Notation) is what all the applications are using these days with their API’s as an alternative to XML. The nice part about JSON is that you don’t need to parse it in the same way you do XML. That data you get from a JSON comes back as an object that is ready to use.

Using APIs (jQuery and JSON)

jQuery provides a very simple way of retrieving these JSON objects.

$.getJSON(‘http://url-to-api.com’, function(data){

$.each(data, function(index, item){

// do stuff with each item

});

});

The code above hits the URL provided (you’ll need to replace that with a real URL to a real API that has real JSON of course) and then does a loop through each “item” and gives you a chance to do something with that item. I say “item” because while that is a common name that APIs use, you’ll need to be specific and accurate here as JSON provides very little in the way of error handling.

Putting it All Together on Your Page

Another reason to use jQuery is because of the simplicity. Let’s take a look at the code example for extracting recent “tweets” from Twitter and then using the built-in jQuery function append() to get them on the page.

$.getJSON(‘http://twitter.com/status/user_timeline/SamanthaRSelman.json?count=10&callback=?’, function(data){

$.each(data, function(index, item){

$(‘#twitter’).append(‘<div class=”tweet”><p>’ + item.text + ‘</p><p>’ + item.created_at + ‘</p></div>’);

});

});

This will put a new div of class “tweet” onto the page (inside the parent div of ID “twitter”) for each “item” in the object. Notice the “count” variable in the URL which Twitter provides. It is set at 10 which will return 10 items thus there will be 10 divs on the page. Inside of those div’s we have two paragraph elements. One with “item.text” and one with “item.created_at”. These will be the actual text of my last tweet and when I submitted it. Please note, replace my twitter name with yours, or else it won’t work.

Example of resulting HTML from one item:

<div>

<p>New post on the “webdesignerer” blog!</p>

<p>1 day ago</p>

</div>

Our project is going great! We can use our CSS skills to style this however we want. (Look forward to the next post, when I’ll give you some ideas on how to style up your website twitter feed!)

Furthering The Idea

Let’s make a div for our web page twitter feed. Then we’ll use our jQuery + JSON technique to fill it up.

Base HMTL:

<body>

<div id=”page-wrap”>

<div id=”twitter”>

<h1>Twitter Updates</h1>

</div>

</div>

</body>

Now here is the jQuery to extract and display all the data from Twitter:

<script type=”text/javascript” src=”js/jquery-1.2.6.min.js”></script>

<script type=”text/javascript”>

$(document).ready(function(){

$.getJSON(‘http://twitter.com/status/user_timeline/SamanthaRSelman.json?count=10&callback=?&#8217;, function(data){

$.each(data, function(index, item){

$(‘#twitter’).append(‘<div class=”tweet”><p>’ + item.text.linkify() + ‘</p><p>’ + relative_time(item.created_at) + ‘</p></div>’);

});

});

});

</script>

Cleaning Up Twitter

There are two problems with the raw data that make the Twitter API stand out… in a bad way.

The full URL is there, but it’s just text, not a real anchor link.

The date appears as an ugly timestamp, not really readable text such as “2 days ago”.

So let’s fix it!

Linkify:

String.prototype.linkify = function() {

return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {

return m.link(m);

});

};

relative_time:

function relative_time(time_value) {

var values = time_value.split(” “);

time_value = values[1] + ” ” + values[2] + “, ” + values[5] + ” ” + values[3];

var parsed_date = Date.parse(time_value);

var relative_to = (arguments.length > 1) ? arguments[1] : new Date();

var delta = parseInt((relative_to.getTime() – parsed_date) / 1000);

delta = delta + (relative_to.getTimezoneOffset() * 60);

var r = ”;

if (delta < 60) {

r = ‘a minute ago’;

} else if(delta < 120) {

r = ‘couple of minutes ago’;

} else if(delta < (45*60)) {

r = (parseInt(delta / 60)).toString() + ‘ minutes ago’;

} else if(delta < (90*60)) {

r = ‘an hour ago’;

} else if(delta < (24*60*60)) {

r = ” + (parseInt(delta / 3600)).toString() + ‘ hours ago’;

} else if(delta < (48*60*60)) {

r = ‘1 day ago’;

} else {

r = (parseInt(delta / 86400)).toString() + ‘ days ago’;

}

So now instead of just putting “item.text” in your append statement, you can put “item.text.linkify()”. Also instead of putting “item.created_at” you can put “relative_time(item.created_at)”. If you have a problem with the code, liked the idea I presented here or have an idea, please don’t hesitate to leave a comment.

Add a “Follow on Twitter” Button to Your Site

18 Feb

This month I am attempting to post a few tips and tricks about social networking sites such as Twitter. Since I am relatively new to Twitter, I am starting out simple. In this post I will show you how to create a “Follow on Twitter” button for your site. This is quite simple.

First, create a new division on your page (<div></div>) for your twitter buttons. (See my last post “Add a Tweet This button to your Site“)

Insert the following code:

<a href=”https://twitter.com/SamanthaRSelman” class=”twitter-follow-button” data-show-count=”false” data-size=”large”>

Follow @SamanthaRSelman</a>

<script>

!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=”//platform.twitter.com/widgets.js”;fjs.parentNode.insertBefore(js,fjs);}}(document,”script”,”twitter-wjs”);

</script>

Please note  this would create a link to my Twitter profile, so be sure to change SamanthaRSelman to your own Twitter username. You can see an example of the Twitter button below, which you can style with CSS. Congratulations! You have created your own “Follow on Twitter” button, which I hope will bring you more Twitter followers.

If you have liked this post or have a problem with the code, please leave a comment.

Follow @SamanthaRSelman
//

Add a “Follow on Twitter” Button to Your Site

18 Feb

This month I am attempting to post a few tips and tricks about social networking sites such as Twitter. Since I am relatively new to Twitter, I am starting out simple. In this post I will show you how to create a “Follow on Twitter” button for your site. This is quite simple.

First, create a new division on your page (<div></div>) for your twitter buttons. (See my last post “Add a Tweet This button to your Site“)

Insert the following code:

<a href=”https://twitter.com/SamanthaRSelman&#8221; class=”twitter-follow-button” data-show-count=”false” data-size=”large”>

Follow @SamanthaRSelman</a>

<script>

!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=”//platform.twitter.com/widgets.js”;fjs.parentNode.insertBefore(js,fjs);}}(document,”script”,”twitter-wjs”);

</script>

Please note  this would create a link to my Twitter profile, so be sure to change SamanthaRSelman to your own Twitter username. You can see an example of the Twitter button below, which you can style with CSS. Congratulations! You have created your own “Follow on Twitter” button, which I hope will bring you more Twitter followers.

If you have liked this post or have a problem with the code, please leave a comment.


//

Create a JavaScript Only “Tweet This” Button

14 Feb

Earlier I posted a “Tweet This!” button created with JavaScript and HTML. In this post I will show you how to create a button with nothing more than JavaScript. Just copy and paste this wherever you want the button.

<script type=”text/javascript”>

var twtTitle = document.title;

var twtUrl = location.href;

var maxLength = 140 – (twtUrl.length + 1);

if (twtTitle.length > maxLength) {

twtTitle = twtTitle.substr(0, (maxLength – 3))+’…’;

}

var twtLink = ‘http://twitter.com/home?status=’+encodeURIComponent(twtTitle + ‘ ‘ + twtUrl);

document.write(‘<a href=”‘+twtLink+'” target=”_blank”‘+’><img src=”tweetthis.jpg” border=”0″ alt=”Tweet This!” /’+’><‘+’/a>’);

</script>

The only thing you need to change is the image source. You may have your own image you would like to use or you can simply style the link with CSS.

Create a “Tweet This!” Button for your Site

14 Feb

In this post I will show you how to easily create links to the pages on your website whenever you have both a page URL and a page title available. This code automatically removes characters from the end of the title so that in addition to the URL itself and a space character between the maximum length of 140 characters will not be exceeded. If the title is too long it will be truncated and three dots will be added to show that it has been shortened.

When clicking on the link the user is taken to the Twitter homepage with the message already preset so he or she can tweet it immediately with just a single click.

function getTweetUrl($url, $title)

{

$maxTitleLength = 140 – (strlen($url)+1);

if (strlen($title) > $maxTitleLength) {

$title = substr($title, 0, ($maxTitleLength-3)).’…’;

}

$output = “$title $url”;

return ‘http://twitter.com/home?status=&#8217;.urlencode($output);

}

?>

Simply add this code to the <head> section of your document in between the <script> tags. Then, in the body of your document, create a link like the one shown below:

<a href=”<?php echo $url; ?>” target=”_blank”><img src=”tweetthis.jpg” border=”0″ alt=”Tweet This!”/></a>

You can replace the image source shown above with your own Twitter button. You now have a “Tweet This” button for your site!

Display the Number of Your Twitter Followers on Your Web Page

20 Dec

Wouldn’t you love to display the number of your Twitter followers on your website? It’s easy with this code snippet!
In your page, create an HTML element with the ID property set to “followers”:

< div id=”followers” > < /div >

This element will be used to display your number of followers.
Now, create a new JavaScript file (twitter.js) with this code:

$(document).ready(function(){
$(function() {
$.ajax({
url: ‘http://api.twitter.com/1/users/show.json&#8217;,
data: { screen_name: ‘YOUR USER NAME’ },
dataType: ‘jsonp’,
success: function(data) {
$(‘#followers’).html(data.followers_count.
toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, “,”));
}
});
});
});
“YOUR USER NAME”, of course, should be replaced with your Twitter user name. Finally, create a link to jQuery and twitter.js in the head section of the page like so:

< script type=”text/javascript” src=”YOUR_PATH/jquery.js” > < /script >
< script type=”text/javascript” src=”YOUR_PATH/twitter.js” > < /script >

Congratulations! The number of your Twitter followers will now be displayed on your web page!
Please feel free to leave a comment if you have any problems with the code.