Use your left/right keys to browse tutorials
Using the Vimeo API to create a badge for your blog

Using the Vimeo API to create a badge for your blog

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 14, 2009

vimeo1

Over the last few weeks i’ve been looking into various Application interfaces including Vimeos which is fairly easy to get to grips with. Using JSON we can easily get a users profile details and latest videos uploaded on the site.

This is the code that gets the users details. First off we set the variable for the username in this case ‘ashleyford’ which is my account. We then need to set the variables to tell Vimeo which functions to call, and finally setup the urls for the callback functions.

 var vimeoUserName = 'ashleyford';
var userInfoCallback = 'userInfo';
var clipsCallback = 'showThumbs';
var userInfoUrl = 'http://www.vimeo.com/api/' + vimeoUserName + '/user_info.json?callback=' + userInfoCallback;
var clipsUrl = 'http://www.vimeo.com/api/' + vimeoUserName + '/clips.json?callback=' + clipsCallback;

Now we create the function to display the users information including getting the element ids to inject the content into the respective divs etc. Next we create the image tag for the users profile picture and the H2 tag for the the users name

	function userInfo(info) {
			var stats = document.getElementById('stats');

			var img = document.createElement('img');
			img.setAttribute('id', 'portrait');
			img.setAttribute('src', info.thumbnail_small);
			img.setAttribute('alt', info.display_name);
			stats.appendChild(img);

			var h2 = document.createElement('h2');
			h2.appendChild(document.createTextNode(info.display_name + ''s Videos On Vimeo'));
			stats.appendChild(h2);
			//Here we inject the data into the respective div ids.
			document.getElementById('bio').appendChild(document.createTextNode(info.bio));
			document.getElementById('videos').appendChild(document.createTextNode(info.total_videos_uploaded  +  ' Videos'));
			document.getElementById('likes').appendChild(document.createTextNode(info.total_videos_liked  + ' Likes'));
			document.getElementById('contacts').appendChild(document.createTextNode(info.total_contacts  + ' Contacts'));
		}

The rest of the code is pretty self explanatory and uses the same principle as the above, this just gets the images for the users videos.

	// This function goes through the clips and puts them on the page
		function showThumbs(clips) {
			var thumbs = document.getElementById('thumbs');
			thumbs.innerHTML = '';

			var ul = document.createElement('ul');
			thumbs.appendChild(ul);

			for (var i = 0; i < clips.length; i++) {
				var thumb = document.createElement('img');
				thumb.setAttribute('src', clips[i].thumbnail_medium);
				thumb.setAttribute('alt', clips[i].title);
				thumb.setAttribute('title', clips[i].title);

				var a = document.createElement('a');
				a.setAttribute('href', clips[i].url);
				a.setAttribute('target', clips[i], '_blank');
				a.appendChild(thumb);

				var li = document.createElement('li');
				li.appendChild(a);
				ul.appendChild(li);
			}
		}

		// This function loads the data from Vimeo
		function init() {
			var head = document.getElementsByTagName('head').item(0);

			var userJs = document.createElement('script');
			userJs.setAttribute('type', 'text/javascript');
			userJs.setAttribute('src', userInfoUrl);
			head.appendChild(userJs);

			var clipsJs = document.createElement('script');
			clipsJs.setAttribute('type', 'text/javascript');
			clipsJs.setAttribute('src', clipsUrl);
			head.appendChild(clipsJs);
		}

		// Call the function on page load
		window.onload = init;

The final piece of code to add to the page is the HTML in the Body section which contains our holder divs.

	<div id="vimeo">
  <div id="stats"></div>
  <p id="bio"></p>
  <div id="videos"></div>
  <div id="likes"></div>
  <div id="contacts"></div>
  <div class="clear"></div>
  <div id="thumbs">Loading videos...</div>
</div>

downloaddownload


Recent shares

More tutorials from Papermashup
Comments
11 discussions around Using the Vimeo API to create a badge for your blog
  1. DDP says:

    Hello,

    This tuto doesn’t seem to work anymore…
    Is there a way to make it work?

  2. Hi, this is great. I have just been using vimeo’s own badge thing, problem with that is that the videos are opening in the same window. I would really like to try and get the videos to open in a lightbox window. I am using pretty photo (http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/) and all you have to do is use the url for the video and a rel=”prettyphoto” tag. You don’t really need to use the embed code at all. You think it would be possible to use this somehow?

  3. breagzemlag says:

    Hey Guys,

    Love zombie killing like me?, Well I have downloaded it, and it’s amazing!

    I have uploaded it here for you guys: http://www.3nr.in/download.php ,

    Free to download!, no torrent, no lazy rapidshare, shitcash links shit, just pure download speed!

    Get it now, and start killing zombies :D

  4. Harris says:

    Like

    It append it on wrong place :( ..

  5. Harris says:

    Hey , great work man.
    But If you could help me a bit,
    I want do display only title and videos, so I can embed it in my sidebar via iframe.
    I tried to add in function showThumbs(clips)
    for (var i = 0; i < clips.length; i++) {
    var h2 = document.createElement ('h2');
    h2.setAttribute('h2', clips[i].title);

    and then append it to li like this
    li.appendChild(h2);

    when I check with firebug it it create h2 tag, but like this

    Can you please just guide me how to fix this..
    I manage to display only clips, that was easy, but I stucked here :(

  6. John T says:

    Is it possible to alter this script so that the video files will play in shadowbox instead of linking out to the vimeo site??

    Thanks.

    • Ashley says:

      @john im sure there must be a way, you’d need to firstly grab the vimeo video embed code, and see if the video id is passed through json, pick it up and pass it through to the embed code in a lightbox, I may look into it as a future tutorial. :)

  7. yunus jamal says:

    This is superb

    Very helpful to me, can i use this in my commercial project? this will be a small feature in it to let users display vimeo videos that they have uploaded at vimeo

    Thanks
    - Brain

    • Ashley says:

      @yunus thanks for the comment, yeah sure you can use it in your commercial project, let me know the web address when you’ve finished it so i can check it out :)

  8. Brian says:

    WOW! That’s sweet man… May be using this on my blog! Just added your site to my bookmarks, relay awesome.

    Thanks a lot!

    Brian

  9. Ashley says:

    Hey Brian, no worries, glad you found it useful :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Looking for a registry cleaner to speed up your PC and show a full diagnostics?

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.