Created
April 27, 2012 02:09
-
-
Save suissa/2505085 to your computer and use it in GitHub Desktop.
Parser do json do youtube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
$('#slider').nivoSlider(); | |
$.ajax({ | |
type: "GET", | |
url: "http://gdata.youtube.com/feeds/users/XYZLIVEBRASIL/uploads?alt=json-in-script&format=5", | |
dataType:'jsonp', | |
success: function(data){ | |
var video = getListaVideos(data), | |
c=0, | |
html = ""; | |
html += "<ul class='youtube-listagem'>"; | |
for(var c = 0; c<3; c++){ | |
html += montaVideo(video[c]); | |
} | |
html += "</ul>"; | |
$("#youtube-list").html(html); | |
} | |
}); | |
}); | |
function montaVideo(video){ | |
var thumb = getThumbnailImagem(video), | |
titulo = getTitle(video), | |
href = getLink(video), | |
title = "<a href='"+href+"' class='youtube-title' target='_blank'>"+titulo+"</a><br />", | |
views = "<span class='youtube-views'>"+getViews(video)+" views</span>", | |
published = "<span class='youtube-published'>"+getPublished(video)+"</span>", | |
img = "<img src='"+thumb+"' alt='"+titulo+"' width='150' class='youtube-thumbnail' />", | |
a_open = "<a href='"+href+"' title='"+titulo+"' target='_blank'>", | |
a_close = "</a>", | |
li = ""; | |
li += "<li>"; | |
li += a_open; | |
li += img; | |
li += a_close; | |
li += "<p>", | |
li += title; | |
// li += views; | |
li += published; | |
li += "</p>"; | |
li += "</li>"; | |
return li; | |
} | |
function getListaVideos(data){ | |
return data.feed.entry; | |
} | |
function getThumbnailImagem(video){ | |
return video.media$group.media$thumbnail[0].url; | |
} | |
function getAuthor(video){ | |
} | |
function getLink(video){ | |
return video.link[0].href; | |
} | |
function getTitle(video){ | |
return video.media$group.media$title.$t; | |
} | |
function getPublished(video){ | |
var date = new Date(), | |
diff = date.diff(new Date(video.published.$t)); | |
if(diff.years > 0){ | |
return (diff.years === 1) ? diff.years+" dia atras" : diff.years+" dias atras"; | |
} | |
if(diff.months > 0){ | |
return (diff.months === 1) ? diff.months+" dia atras" : diff.months+" dias atras"; | |
} | |
if(diff.days > 0){ | |
return (diff.days === 1) ? diff.days+" dia atras" : diff.days+" dias atras"; | |
} | |
if(diff.hours > 0){ | |
return (diff.hours === 1) ? diff.hours+" dia atras" : diff.hours+" dias atras"; | |
} | |
if(diff.minutes > 0){ | |
return (diff.minutes === 1) ? diff.minutes+" dia atras" : diff.minutes+" dias atras"; | |
} | |
if(diff.seconds > 0){ | |
return (diff.seconds === 1) ? diff.seconds+" dia atras" : diff.seconds+" dias atras"; | |
} | |
} | |
function getContent(video){ | |
return video.content.$t; | |
} | |
function getDescription(video){ | |
return video.media$group.media$description.$t; | |
} | |
function getKeywords(video){ | |
return video.media$group.media$keywords.$t; | |
} | |
function getViews(video){ | |
return video.yt$statistics.viewCount; | |
} | |
Object.defineProperty(Date.prototype,"diff",{ | |
writable: false, configurable: false, enumerable: true, | |
/** | |
* Returns the difference between two Date objects. | |
* @param {Date} The date to compare to. | |
* @return {Object} | |
* @throws {TypeError} | |
*/ | |
value: function(date) { | |
if (date instanceof Date){ | |
var ms = this-date; | |
var diff = {}; | |
for ( diff.years = 0; ms>=31536000000; diff.years++, ms -= 31536000000); | |
for ( diff.months = 0; ms>=2628000000; diff.months++, ms -= 2628000000); | |
for ( diff.days = 0; ms>=86400000; diff.days++, ms -= 86400000); | |
for ( diff.hours = 0; ms>=3600000; diff.hours++, ms -= 3600000); | |
for ( diff.minutes = 0; ms>=60000; diff.minutes++, ms -= 60000); | |
for ( diff.seconds = 0; ms>=1000; diff.seconds++, ms -= 1000); | |
diff.milliseconds = ms; | |
return diff; | |
} | |
throw new TypeError("invalid date"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment