Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > มาเขียนการค้นหา video กับ youtube Api กันเถอะ



 
Clound SSD Virtual Server

มาเขียนการค้นหา video กับ youtube Api กันเถอะ

มาเขียนการค้นหา video กับ youtube Api กันเถอะ เห็นถามในบอร์ดกันเยอะ วันนี้มาลองทดสอบกันครับ ไม่ขออธิบายโค้ดนะครับ ลองนำไปประยุกต์กันเอาเอง

Code
<?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Youtube Api By Deawx</title>
<script src="js/jquery.js" type="text/javascript"></script>
<script   type="text/javascript">
jQTubeUtil = function (h) {
    function i(a, b, c) {
        var d = {
            "max-results": b.max || l,
            "start-index": b.start || u
        };
        if (b.time) d.time = b.time;
        a = m(a, d);
        return n(a, b.callback || c)
    }
    function j(a, b) {
        switch (typeof a) {
        case "function":
            return {
                callback: a,
                time: undefined
            };
        case "object":
            var c = {
                max: a.max,
                start: a["start-index"]
            };
            if (b) c.time = a.time;
            return c;
        default:
            return {}
        } }
    function v(a, b) {
        a = m(w, a);
        return n(a, b)
    }
    function n(a, b) {
        var c = {};
        h.ajax({
            type: "GET",
            dataType: "json",
            url: a,
            success: function (d) {
                if (typeof d != "undefined") {
                    var g = [];
                    if (d.feed) {
                        var e = d.feed,
                            p = d.feed.entry;
                        for (entry in p) g.push(new q(p[entry]));
                        c.startIndex = e.openSearch$startIndex.$t;
                        c.itemsPerPage = e.openSearch$itemsPerPage.$t;
                        c.totalResults = e.openSearch$totalResults.$t
                    } else g.push(new q(d.entry));
                    c.version = d.version;
                    c.searchURL = a;
                    c.videos = g;
                    typeof b == "function" && b(c)
                }
            },
            error: function (d) {
                throw Exception("couldn't fetch YouTube request : " + a + " : " + d);
            }
        });
        return c
    }
    function m(a, b) {
        var c = "?",
            d, g = true,
            e = h.extend({}, b, r);
        for (o in e) {
            b = o;
            d = e[o];
            c += (g ? "" : "&") + b + "=" + d;
            g = false
        }
        return a + c
    }
    var s = function () {},
        f = s.prototype,
        l = 10,
        u = 1,
        w = "http://gdata.youtube.com/feeds/api/videos";
    MostPopular = "http://gdata.youtube.com/feeds/api/standardfeeds/most_popular";
    MostRecent = "http://gdata.youtube.com/feeds/api/standardfeeds/most_recent";
    TopRated = "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated";
    TopFavs = "http://gdata.youtube.com/feeds/api/standardfeeds/top_favorites";
    RecentlyFeatured = "http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured";
    SuggestURL = "http://suggestqueries.google.com/complete/search";
    Times = ["today", "this_week", "this_month", "all_time"];
    OrderBy = ["relevance", "published", "viewCount", "rating"];
    var k = {
        q: "",
        orderby: OrderBy[2],
        time: Times[3],
        "max-results": l
    },
        r = {
            key: "",
            format: 5,
            alt: "json",
            callback: "?"
        },
        t = {
            hl: "en",
            ds: "yt",
            client: "youtube",
            hjson: "t",
            cp: 1
        };
    f.init = function (a) {
        if (!a.key) throw "jQTube requires a key!";
        r.key = a.key;
        if (a.orderby) k.orderby = a.orderby;
        if (a.time) k.time = a.time;
        if (a.maxResults) k["max-results"] = l = a.maxResults;
        if (a.lang) t.hl = a.lang
    };
    f.getTimes = function () {
        return Times
    };
    f.getOrders = function () {
        return OrderBy
    };
    f.suggest = function (a, b) {
        a = {
            q: encodeURIComponent(a)
        };
        var c = m(SuggestURL, h.extend({}, t, a));
        h.ajax({
            type: "GET",
            dataType: "json",
            url: c,
            success: function (d) {
                var g = [],
                    e = {};
                for (entry in d[1]) g.push(d[1][entry][0]);
                e.suggestions = g;
                e.searchURL = c;
                typeof b == "function" && b(e)
            }
        })
    };
    f.search = function (a, b) {
        if (typeof a == "string") a = {
            q: encodeURIComponent(a)
        };
        return v(h.extend({}, k, a), b)
    };
    f.video = function (a, b) {
        return n("http://gdata.youtube.com/feeds/api/videos/" + a + "?alt=json", b)
    };
    f.mostViewed = function (a, b) {
        return i("http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed", j(a, true), b)
    };
    f.mostRecent = function (a, b) {
        return i(MostRecent, j(a, false), b)
    };
    f.mostPopular = function (a, b) {
        return i(MostPopular, j(a, true), b)
    };
    f.topRated = function (a, b) {
        return i(TopRated, j(a, true), b)
    };
    f.topFavs = function (a, b) {
        return i(TopFavs, j(a, true), b)
    };
    var q = function (a) {
            var b = [],
                c = a.id.$t,
                d = c.lastIndexOf("/") + 1;
            this.videoId = c.substring(d, c.length);
            this.title = a.title.$t;
            try {
                this.updated = a.updated.$t
            } catch (g) {
                b.push("updated")
            }
            try {
                this.thumbs = a.media$group.media$thumbnail
            } catch (e) {
                b.push("thumbs")
            }
            try {
                this.duration = a.media$group.yt$duration.seconds
            } catch (p) {
                b.push("duration")
            }
            try {
                this.favCount = a.yt$statistics.favoriteCount
            } catch (x) {
                b.push("favCount")
            }
            try {
                this.viewCount = a.yt$statistics.viewCount
            } catch (y) {
                b.push("viewCount")
            }
            try {
                this.category = a.media$group.media$category.$t
            } catch (z) {
                b.push("category")
            }
            try {
                this.description = a.media$group.media$description.$t
            } catch (A) {
                b.push("description")
            }
            try {
                this.keywords = a.media$group.media$keywords.$t
            } catch (B) {
                b.push("keywords")
            }
            this.unavailAttributes = b
        };
    return new s
}(jQuery);

$(function(){
	
	jQTubeUtil.init({
		key: 'AI39si73J88KqOpnS0qufASk1veyM38Bx1c2n4FNr--LoSh3hQrNI6BT9PsWblRvP_gbX2sQ651gR0ThY1lCABwSL5aFedgPqQ',
		orderby: 'viewCount',
		time: 'all_time',  
		maxResults: 50
	});	
	$('.search').keyup(function(){
		var val = $(this).val();
		jQTubeUtil.suggest(val, function(response){
			var html = '';
			for(s in response.suggestions){
				var sug = response.suggestions[s];
				html += '<li><a href="#">'+sug+'</a></li>';
			}
			if (response.suggestions.length)
				$('.autocomplete').html(html).fadeIn(500);
			else 
				$('.autocomplete').fadeOut(500);
		});
	});
	$('.btn').click(function(){
		show_videos();
		$('.autocomplete').fadeOut(500);
		return false;
	});
	$('.autocomplete').find('a').live('click', function(){
		var text = $(this).text();
		$('.blocks').find('.search').val(text);
		$('.autocomplete').fadeOut(500);
		show_videos();
		return false;
	});
	function show_videos(){
		var val = $('.blocks').find('.search').val();
		$('.videos').addClass('preloader').html('');
		jQTubeUtil.search(val, function(response){
			var html = '';
			for (v in response.videos) {
				var	video = response.videos[v],
					minutes = parseInt(video.duration / 60),
					seconds = video.duration % 60;
				html += '<li>';
				html += '<p class="image"><a href="http://www.youtube.com/watch?v='+video.videoId+'">';
				html += '<img src="' + video.thumbs[1].url + '" alt="' + video.title + '" title="' + video.title + '" />';
				html += '</a></p>'
				html += '<p class="entry"><a href="http://www.youtube.com/watch?v='+video.videoId+'">' + video.title + '</a>';
				html += '<small>' + minutes + ':' + (seconds < 10 ? '0'+seconds : seconds) + '</small>';
				html += 'link' +  video.videoId + '</small>';
				html += '</p>';
				html += '</li>';
			}
			$('.videos').removeClass('preloader').html(html);
		});
	}			
});
</script>
<style>
a { text-decoration:none; color:#555; } a img { border:none; } body { background:url(gfx/bg.jpg) repeat; font:13px/18px Tahoma,Arial,Helvetica,sans-serif;color:#666; } .blocks { float:left; width:100%; margin-bottom:50px; position:relative; } .blocks .btn { float:left; width:71px; height:42px; border:none; text-indent:-9999px; cursor:pointer; background:url(gfx/btn.png) no-repeat; } .blocks .search { text-align:center; width:258px; height:42px; padding:0 10px; font-size:13px; color:#8c8c8c; background:url(gfx/input.png) no-repeat; border:none; font:bold 13px/42px Tahoma,Arial,Helvetica,sans-serif;} .blocks p { float:left; margin:0; } .videos { float:left; clear:both; width:100%; min-height:200px; } .videos .entry { float:right; width:163px;     font-weight:bold; } .videos .entry small { display:block; font-weight:bold; color:#ACACAC; } .videos .image { float:left; width:120px; height:90px; font-size:0; line-height:0; padding:5px; background:#fff; border:1px solid #E6E6E6; overflow:hidden; } .videos.preloader { background:url(gfx/loader.gif) no-repeat center;  } .wrapper { width:1000px; margin:100px auto 0; } ul.autocomplete { display:none; font:bold 13px Tahoma,Arial,Helvetica,sans-serif;width:300px; padding:10px; position:absolute; top:20px; left:0; background:#fff; border:1px solid #e4e4e4; -moz-border-radius:8px; -webkit-border-radius:8px; border-radius:8px; } ul.autocomplete li a { display:block; padding:5px 10px; font-size:11px; outline:none; } ul.reset { } ul.reset li { display:block; list-style:none; padding:0; margin:0; background:#fff; border:1px solid #e4e4e4; -moz-border-radius:8px; -webkit-border-radius:8px; border-radius:8px; }  ul.videos li { float:left; width:300px; padding:10px; margin:0 5px 5px 0; } ul.videos li p { margin:0; padding:0; } ul.autocomplete li a:hover { background:#f5f5f5; color:#991b22; -moz-border-radius:4px; -webkit-border-radius:4px; border-radius:4px; }  
</style>
</head>
<body>
<div class="wrapper">
  <form class="blocks" action="#" method="get">
    <p>
      <input type="text" class="search" />
    </p>
    <p>
      <input type="Submit" class="btn" value="Search" />
    </p>
    <ul class="reset autocomplete">
    </ul>
  </form>
  <ul class="reset videos">
  </ul>
</div>
</body>
</html>

?>


Power By deawx Cyberthai.net


Reference : http://www.cyberthai.net





   
Share
Bookmark.   

  By : deawx
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2011-10-05
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่