Thứ Sáu, 30 tháng 3, 2012

Cách đem thật nhiều YouTube videos về blog của bạn

Bạn,

Hôm nay tôi muốn chia xẻ với bạn một "bí kiếp" mà tôi đã dùng để "lôi" về các videos từ một user account rồi post lên trang hoctroviet. Rất tiện lợi nếu bạn muốn xem video mà không phải xem quảng cáo từ những trang nhạc như AbbaVEVO chẳng hạn. Bạn cũng có thể đem về rồi góp những bài hay lại thành một tuyển chọn.







Bước 1:Tạo một blog mới, chỉ với mục đích là "lôi" đồ nhà người ta về, rồi chèn đoạn mã này vào đằng trước thẻ "</head>".



<script type='text/javascript'>

var yt = {
nComments : -1,
totCount : 0,
increment : 25,
queryDefault : 25,
web : &#39;&#39;,
ytDiv : &#39;&#39;,



ytQuery:function(query, ytD, start, increment, func) {
this.web = query;
this.ytDiv = ytD;

var script = document.createElement(&#39;script&#39;);

script.setAttribute(&#39;src&#39;, &#39;http://gdata.youtube.com/feeds/api/videos?q=&#39; + query + &#39;&amp;alt=json&#39;
+ &#39;&amp;callback=&#39; + func );
script.setAttribute(&#39;type&#39;, &#39;text/javascript&#39;);
document.documentElement.firstChild.appendChild(script);
},


ytsearch:function(query, ytD, start, increment, func) {
this.web = query;
this.ytDiv = ytD;
var script = document.createElement(&#39;script&#39;);

script.setAttribute(&#39;src&#39;, &#39;http://gdata.youtube.com/feeds/api/users/&#39; + query + &#39;/uploads?alt=json&amp;start-index=&#39;
+ start + &#39;&amp;max-results=&#39; + increment
+ &#39;&amp;callback=&#39; + func );
script.setAttribute(&#39;type&#39;, &#39;text/javascript&#39;);
document.documentElement.firstChild.appendChild(script);
},



// getLink return a href link if &quot;name&quot; matches the content inside &quot;a&quot; tags) of the link
getLink: function(entry, name) {
var alturl;

for (var k = 0; k &lt; entry.link.length; k++) {
if (entry.link[k].rel == name)
alturl = entry.link[k].href;
}
return alturl;
},



// getContent gets the content inside an entry of a json object.

getContent: function(entry) {
return entry.content.$t;
},


//
getTitle: function(entry) {
return entry.title.$t;
},



//
getAuthorName: function(entry) {
return entry.author.name.$t;
},

//

getYTLink: function(entry) {
return &quot;http://www.youtube.com/watch?v=&quot; + this.getID(entry);
},

//
getYTLinkPlaylist: function(entry) {
return &quot;http://www.youtube.com/watch?v=&quot; + this.getIDPlaylist(entry);
},

//

getIDPlaylist: function(entry) {
  var id = entry.link[0].href.substr(entry.link[0].href.length - 33,entry.link[0].href.length - 22);
  return id.substr(0,11);
},

//
getID: function(entry) {
  var id = entry.id.$t;
  return id.substr(id.length - 11, id.length - 1);
},



// getTotalResults needs the json object, and it&#39;ll return the total number of comments (or posts) of the blog.

getTotalResults: function(json) {
return json.feed.openSearch$totalResults.$t;
},



// getStartIndex gets the start index of a search inside an json object.
getStartIndex: function(json) {
return json.feed.openSearch$startIndex.$t;
},



listBatch : function (json, tag) {

  var text=&quot;&quot;;
  var p = document.createElement(&#39;div&#39;);
  var ul = document.createElement(&#39;ul&#39;);

for (var i = 0; i &lt; json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];

text = &#39;&lt;li class= &quot;playlist-video-item &quot;&gt;&#39; +

&#39;&lt;a href=&quot;http://www.youtube.com/watch?v=&#39; + this.getID(entry) + &#39;&quot; class= &quot;tile-link-block video-tile &quot;&gt;&#39; +

&#39;&lt;span class=&quot;playlist-video-item-base-content&quot;&gt;&#39; +

&#39;&lt;span class=&quot;thumb-container&quot;&gt;&#39; +

&#39;&lt;span class=&quot;ux-thumb-wrap&quot;&gt;&#39; +

&#39; &lt;span class=&quot;video-thumb ux-thumb ux-thumb-114 &quot;&gt;&#39; +

&#39;&lt;span class=&quot;clip&quot;&gt;&#39; +

&#39;&lt;span class=&quot;clip-inner&quot;&gt;&#39; +

&#39;&lt;img src=&quot;http://i2.ytimg.com/vi/&#39; +

 this.getID(entry) +

&#39;/default.jpg&quot; &gt;&lt;span class=&quot;vertical-align&quot;&gt;&#39; +

&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&#39; +

&#39;&lt;span class=&quot;video-info&quot;&gt;&lt;span class=&quot;video-overview&quot;&gt;&lt;span class=&quot;title video-title &quot;  dir=&quot;ltr&quot; &gt;&#39; +

this.getTitle(entry) +

&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&#39; +

&#39;&lt;/a&gt;&lt;/li&gt;&#39;;


  var li = document.createElement(&#39;li&#39;);
  var a = document.createElement(&#39;a&#39;);
  a.href = this.getYTLink(entry);
  a.target =  &quot;_blank&quot;;

  var txt = document.createTextNode(text);
  a.appendChild(txt);
  li.appendChild(a);
  ul.appendChild(li);
  }

  p.appendChild(ul);
  document.getElementById(tag).appendChild(p);

},



callJson : function(json) {
  this.listBatch(json, this.ytDiv);
},



getCo : function(json) {
  this.totCount = this.getTotalResults(json);
  var count =  (this.nComments &lt; 0) ? this.totCount : this.nComments;
  var i=1;
  while( i &lt;= count ) {
    var incr = (count-i &lt; this.increment) ? count-i+1 : this.increment;
    this.ytsearch( this.web, this.ytDiv, i, incr, &#39;yt.callJson&#39;);
    if (incr==0) incr=incr+1;
    i = i+incr;
  }
},


Channel:function(query, ytD) {
this.web = query;
this.ytDiv = ytD;
this.ytsearch(query, ytD, 1,20,&#39;yt.getCo&#39;);
}

};

</script>



Bước 2: Tạo ra một bài viết mới bằng cách thêm vào đoạn sau. Thế chữ in đậm(AbbaVEVO) với tên (YouTube username) của người mà bạn muốn đem videos của họ về. Nhớ đem vào bằng cách "HTML" chứ đừng bằng cách  "chữ đẹp" (compose).



<div id="yt2012">

</div>

<script type='text/javascript'>

yt.Channel( 'AbbaVEVO', 'yt2012');

</script>



Bước 3: đăng bài lên.  Đoạn mã tôi viết sẽ đến username account đó, rồi lôi hết các videos về, 10, 100, 1000 videos nó cũng lôi về hết!





Lấy nội dung của post bằng cách cắt rồi dán vào notepad (Ctrl+A, mở notepad, rồi Ctrl+V)

Bây giờ ta trở qua blog chính.

Trước hết, nếu bạn muốn xem videos ngay trên trang nhà, giống như cách hoctro làm trên trang hoctroviet, thì thêm đoạn mã này trước thẻ </head> :


<!-- Yahoo Media Player -->

<script src='http://webplayer.yahooapis.com/player.js' type='text/javascript'/>





Sau đó, tạo ra một bài viết mới, rồi chuyển qua dạng viết HTML. Thêm vào <ul></ul> rồi nhét đoạn mã mới thu hoạch vào giữa hai thẻ đó.


Đăng bài lên! Bạn sẽ thấy hết các bài nhạc từ trang muốn lấy về. Thí dụ như trang AbbaVEVO dưới đây.




Đây là cách tôi đem videos về trang hoctroviet từ hơn năm nay. Hy vọng bạn sẽ có dịp thử làm theo.

Chúc vui!

Hoctro
3/30/2012

Không có nhận xét nào:

Đăng nhận xét

Girls Generation - Korean