web api youtube api

Post on 02-Jan-2016

106 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Web API Youtube API. Thành viên : Châu Hoàn Sơn – 0912392 Nguyễn Quốc Nhã – 0912317 Huỳnh Văn Lộc – 0912268. Nội dung tìm hiểu. Khái niệm Web API Lợi ích của Web API Các dạng Web API được cung cấp Cách sử dụng chung cho Web API Giới thiệu Youtube API - PowerPoint PPT Presentation

TRANSCRIPT

Web APIYoutube API

Thành viên:Châu Hoàn Sơn – 0912392Nguyễn Quốc Nhã – 0912317Huỳnh Văn Lộc – 0912268

I. Khái niệm Web APIII. Lợi ích của Web APIIII. Các dạng Web API được cung cấpIV. Cách sử dụng chung cho Web APIV. Giới thiệu Youtube APIVI. Một số API mà Youtube hỗ trợVII. Demo

Nội dung tìm hiểu

Khái niệm

Là 1 dạng thể hiện, giúp phát triển ứng dụng 1 cách dễ dàng qua việc hỗ trợ rất nhiều những hoạt động / dịch vụ giúp cho người dùng có thể tương tác với hệ thống.

Việc tương tác đó có thể được định nghĩa qua các hàm, các tham số, các biến có liên quan đến hệ thống chính.

API: Application Programming Interface

(Giao diện lập trình ứng dụng)

VD: Window có các API để tương tác với các service của hệ thống, các phần mềm mặc định…

API: Application Programming Interface

(Giao diện lập trình ứng dụng)

Web API: là API dưới dạng Web Services Là hệ thống thiết lập các thông điệp dạng

HTTP, NET/TCP,.. để gửi lên web, sau đó nhận các thông điệp phản hồi về (thường là dạng JSON, XML có cấu trúc rõ ràng).

Web API

Cung cấp cho người dùng 1 dạng các thể hiện, giúp cho người dùng có thể tương tác được với dịch vụ được cung cấp trên web

Người dùng thông qua API để sử dụng các dịnh vụ đó

VD: các hàm, các giao thức, gói tin gửi đi, các URL,..

Web API

Lợi ích của Web API

Giải quyết vấn đề về dung lượng lưu trữ, tốc độ xử lý….

Cập nhật realtime (thời gian thực) Tăng tính tiện dụng Giảm thời gian, chi phí khi phát triển ứng

dụng …

Lợi ích của Web API

Các dạng Web API được cung cấp

Địa chỉ Web của API:Mỗi API sẽ có địa chỉ web tương ứng, truy

vấn các địa chỉ này và các tham số truyền vào để sử dụng.

VD: Google map: http://maps.googleapis.com/maps/api/

Google Translator: http://translate.googleapis.com/translate_static

Yahoo! Map Service: http://local.yahooapis.com/MapsService/

Các dạng Web API được cung cấp

Các thư viện động: Web API được đóng gói trong 1 bộ thư

viện. Khi sử dụng, gọi các hàm trong đó để sử dung, tương tác với dịch vụ ở trên Net.

VD: Facebook SDK, Youtube Data API..

Các dạng Web API được cung cấp

Các thư viện Javascript:Dùng cho việc sử dụng Web API nhúng

vào trong website của người dùng (có dạng gần giống với cách sử dụng địa chỉ web API)

VD: Google, Youtube, Youtube Player API,..

Các dạng Web API được cung cấp

Cách sử dụng chung cho Web

API

Đối với Web API dạng Link: Dùng các Http Request để thực hiện

việc truy vấn đến web service và nhận dữ liệu.

Các Http Request là các link tương ứng từng dịch vụ mà hệ thống đó hỗ trợ; truyền vào các tham số, sau đó dùng nó để truy vấn lên hệ thống trên Web, hệ thống sẽ xử lý và trả về kết quả.

Cách sử dụng chung cho Web API

VD: đoạn code sau sẽ truy vấn lên Yahoo API, tìm và lấy “Tên” của tỉnh ở tọa độ Kinh độ, Vĩ độ được truyền vào. Kết quả trả về là 1 file Json, trong đó có tên của tỉnh cần lấy.

public WebClient webClient; Uri uri = new Uri( "http://where.yahooapis.com/geocode?&gflags=R&location=“+ [kinh độ] + "+" + [vĩ độ]); webClient.DownloadStringAsync(uri);

Cách sử dụng chung cho Web API

Đối với Web API dạng Link:

Đối với Web API dạng SDK: Download và cài đặt SDK Add reference và sử dụng những phương

thức mà Web API cung cấp

VD: Youtube API:https://code.google.com/p/google-gdata/downloads/

list

 

Cách sử dụng chung cho Web API

Đối với Web API dạng SDK:

Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));printVideoFeed(videoFeed);

static void printVideoFeed(Feed<Video> feed){ foreach (Video entry in feed.Entries) {

printVideoEntry(entry); }}

Cách sử dụng chung cho Web API

VD: Hiện video feed

Đối với Web API dạng thư viện Java Script:Sử dụng chính các link đến Web API đó để

lấy thông tin, khởi tạo các giá trị cho đối tượng và thực hiện

VD: link đến google map api để lấy file javascript , hiển thị google map và các phím control trên trình duyệt.

Cách sử dụng chung cho Web API

Đối với Web API dạng thư viện Java Script:

Cách sử dụng chung cho Web API

Giới thiệuYoutube API

Giới thiệu Youtube

Là Website chia sẻ video hàng đầu thế giới Số lượt sử dụng hàng ngày của Youtube

đến hơn 200.000 lượt (thống kê 2010). Người dùng có thể đăng tải các video,

comment, lưu video vào danh sách ưa thích, tạo page riêng,.. và nhiều ứng dụng khác.

Địa chỉ truy cập: www.youtube.com

Youtube API được cung cấp theo 2 dạng:Data API và Player API

Data API:Cho phép người dùng có thể thực hiện

nhiều thao tác tương tự trên website Youtube: Upload video, Sửa đổi danh sách ưa thích, tìm kiếm, comment, đánh giá,..

Giới thiệu Youtube API

Player API:Cho phép người dùng nhúng đoạn video

vào trong trang web và có thể thực hiện các thao tác Play, Pause, Stop, tăng giảm âm lượng,.. trực tiếp từ web của họ mà không phải thông qua Youtube.

Giới thiệu Youtube API

Một số API mà Youtube hỗ trợ

I. Chứng thực II. Video feed và entryIII. Truy xuất và tìm kiếm videoIV. Upload videoV. Cập nhật và xoá videoVI. Sử dụng tính năng cộng đồngVII. Lưu trữ và chọn lựa videoVIII. Kích hoạt tương tác người

dùng

Một số API mà Youtube hỗ trợ

Một số API mà Youtube hỗ trợ

Chứng thực

Chứng thực việc sử dụng Youtube API

YouTubeRequestSettings settings = new YouTubeRequestSettings("example app", developerKey);YouTubeRequest request = new YouTubeRequest(settings);

Một số API mà Youtube hỗ trợChứng thực

DeveloperKey: http://code.google.com/apis/youtube/dashboard

Một số API mà Youtube hỗ trợ

Video feed và entry

Truy xuất video feed

Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));printVideoFeed(videoFeed);

static void printVideoFeed(Feed<Video> feed){ foreach (Video entry in feed.Entries) {

printVideoEntry(entry); //xem mục 1.3 video entry contents }}

Một số API mà Youtube hỗ trợVideo feed và entry

Truy xuất video feed

Một số API mà Youtube hỗ trợVideo feed và entry

Truy xuất video entry qua id

Một số API mà Youtube hỗ trợVideo feed và entry

Uri  videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");

Video video = request.Retrieve<Video>(videoEntryUrl);printVideoEntry(video);

Truy xuất video entry qua id

Một số API mà Youtube hỗ trợVideo feed và entry

Nội dung của video entry:Sử dụng các thuộc tính sau:

Một số API mà Youtube hỗ trợVideo feed và entry

video.Title video.Description video.Keywords video.Uploader video.YouTubeEntry video.Media

video.Status video.ReadOnly video.RatingAverag

e video.Contents …

Nội dung của video entry:

Một số API mà Youtube hỗ trợVideo feed và entry

Một số API mà Youtube hỗ trợ

Truy xuất vàtìm kiếm video

Truy xuất feed theo chuẩn

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Feed Name Feed Identifier

Most Viewed most_viewed

Top Rated top_rated

Recently Featured recently_featured

Watch On Mobile watch_on_mobile

Most Discussed most_discussed

Top Favorites top_favorites

Most Responded most_responded

Most Recent most_recent

Truy xuất feed theo chuẩn

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

http://gdata.youtube.com/feeds/api/standardfeeds

/FEED_IDENTIFIERhttp://gdata.youtube.com/feeds/api/

standardfeeds/localeID/

feedID_CATEGORY_NAME

VD: http://gdata.youtube.com/feeds/api/standardfeeds/JP/

top_rated_Comedy

Truy xuất feed theo chuẩn

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Feed<Video> videoFeed = request.GetStandardFeed(YouTubeQuery.MostPopular);printVideoFeed(videoFeed);

Truy xuất feed theo chuẩn

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Truy xuất video feed upload bởi người dùng

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

http://gdata.youtube.com/feeds/api/users/username/uploadshttp://gdata.youtube.com/feeds/api/users/username/uploads/VIDEO_IDhttp://gdata.youtube.com/feeds/api/users/default/uploads (lấy feed dựa vào quyền user đã chứng thực)

VD: Uri uri = new Uri("http://gdata.youtube.com/feeds/api

/users/GoogleDevelopers/uploads");

Feed<Video> videoFeed = request.Get<Video>(uri); printVideoFeed(videoFeed);

Truy xuất video feed upload bởi người dùng

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Lấy những video liên quan

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Uri videoEntryUrl = new Uri( "http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");Video video = request.Retrieve<Video>(videoEntryUrl);Feed<Video> relatedVideos = request.GetRelatedVideos(video);printVideoFeed(relatedVideos)

Lấy những video liên quan

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Tìm kiếm video

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);

//order results by the number of views (most viewed first)query.OrderBy = "viewCount";

Ta sử dụng mẫu URL sau:http://gdata.youtube.com/feeds/api/videos?q=keyword&safeSearch=none&orderby=viewCount

VD:

Tìm kiếm video

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

// search for puppies and include restricted content in the search results// query.SafeSearch could also be set to YouTubeQuery.SafeSearchValues.Moderatequery.Query = "puppy";query.SafeSearch = YouTubeQuery.SafeSearchValues.None;

Feed<Video> videoFeed = request.Get<Video>(query);

printVideoFeed(videoFeed);

Tìm kiếm video

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Tìm kiếm video sử dụng category và keyword

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);

AtomCategory category1 = new AtomCategory("News", YouTubeNameTable.CategorySchema);AtomCategory category2 = new AtomCategory("sports", YouTubeNameTable.KeywordSchema); query.Categories.Add(new QueryCategory(category1));query.Categories.Add(new QueryCategory(category2));

Feed<Video> videoFeed = request.Get<Video>(query);printVideoFeed(videoFeed);

Tìm kiếm video sử dụng category và keyword

Một số API mà Youtube hỗ trợTruy xuất và tìm kiếm video

Một số API mà Youtube hỗ trợ

Upload video

Upload video

Một số API mà Youtube hỗ trợUpload video

Video newVideo = new Video();

newVideo.Title ="My Test Movie";newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));newVideo.Keywords = "cars, funny";newVideo.Description = "My description";newVideo.YouTubeEntry.Private = false;newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag",   YouTubeNameTable.DeveloperTagSchema));

Upload video Trực tiếp

Một số API mà Youtube hỗ trợUpload video

newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);// alternatively, you could just specify a descriptive string// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");

newVideo.YouTubeEntry.MediaSource = new MediaFileSource("c:\\file.mov",  "video/quicktime");Video createdVideo = request.Upload(newVideo);

Upload video dựa vào trình duyệt

Một số API mà Youtube hỗ trợUpload video

newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);// alternatively, you could just specify a descriptive string// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");

FormUploadToken token = request.CreateFormUploadToken(newVideo);Console.WriteLine(token.Url);Console.WriteLine(token.Token);

Upload video dựa vào trình duyệt

Một số API mà Youtube hỗ trợUpload video

newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);// alternatively, you could just specify a descriptive string// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");

FormUploadToken token = request.CreateFormUploadToken(newVideo);Console.WriteLine(token.Url);Console.WriteLine(token.Token);

Kiểm tra trạng thái upload

Một số API mà Youtube hỗ trợUpload video

if (video.IsDraft){ Console.WriteLine("Video is not live."); string stateName = video.Status.Name; if (stateName == "processing") {

Console.WriteLine("Video is still being processed."); } else if (stateName == "rejected“ || stateName == "failed")) {

Console.WriteLine("Video is still being processed."); }

Một số API mà Youtube hỗ trợ

Cập nhậtvà Xoá video

Cập nhật thông tin video

Một số API mà Youtube hỗ trợCập nhật và Xoá video

video.Description = "new description";Video updatedVideo = request.Update(video);

Xóa videorequest.Delete(video);

Một số API mà Youtube hỗ trợ

Sử dụngtính năng cộng đồng

Đánh giá video

Một số API mà Youtube hỗ trợSử dụng tính năng cộng đồng

Truy xuất Video Comment

video.Rating = 5;request.Insert(video.RatingsUri, video);

Feed comments = request.GetComments(v);foreach (Comment c in comments.Entries){

Console.WriteLine(c.Content);}

Thêm comment

Một số API mà Youtube hỗ trợSử dụng tính năng cộng đồng

Comment c = new Comment();c.Content = "This is my comment.";request.AddComment(video, c);

// retrieve the comments for the videoFeed fc = request.GetComments(video);List comments = new List(fc.Entries); // create a reply to the third commentComment c = comments[2];Comment reply = new Comment(); reply.ReplyTo(c);reply.Content = "This is the reply text"; request.AddComment(video, reply);

Truy xuất video responses

Một số API mà Youtube hỗ trợSử dụng tính năng cộng đồng

Uri  videoEntryUrl =   new Uri ("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");Video video = request.Retrieve<Video>(videoEntryUrl);Feed<Video> responses = request.GetResponseVideos(video);printVideoFeed(responses);

Thêm video responses

Một số API mà Youtube hỗ trợSử dụng tính năng cộng đồng

request.Insert(video.ResponseUri, responseVideo);

request.Insert(videoFeed, responseVideo);

Thêm video responses

request.Delete(responseVideo);

Đánh dấu video

Một số API mà Youtube hỗ trợSử dụng tính năng cộng đồng

Complaint c = new Complaint();c.Type = ComplaintEntry.ComplaintType.PORN;c.Content = "This video offends my better sensibilities.";

request.Insert(video.ComplaintUri, c);

Một số API mà Youtube hỗ trợ

Lưu trữvà Thu thập video

Truy xuất video yêu thích của người dùng

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Feed<Video> feed = request.GetFavoriteVideo(username);printVideoFeed(videoFeed);

Xoá video yêu thíchrequest.Delete(favoriteVideo);

Thêm video yêu thích

Một số API mà Youtube hỗ trợ

string videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0";YouTubeEntry videoEntry = (YouTubeEntry) service.Get(videoEntryUrl);service.Insert(new Uri(feedUrl), videoEntry);

Lưu trữ và Thu thập video

Truy xuất playlist của người dùng

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Feed<Playlist> userPlaylists = request.GetPlaylistsFeed(username);foreach (Playlist p in userPlaylists.Entries){  Console.WriteLine("Title: " + p.Title);  Console.WriteLine(p.Summary);  Console.WriteLine("Number of entries: " + p.CountHint);}

Truy xuất thông tin playlist

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Feed<PlayListMember> list = request.GetPlaylist(p);foreach (Video v in list.Entries){  printVideoEntry(v);}

Thêm playlist

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Playlist pl = new Playlist();pl.Title = "this is the title";pl.Summary = "this is my new playlist";Playlist createdPlaylist = request.Insert(  new Uri("http://gdata.youtube.com/feeds/api/users/default/playlists"), pl);

Xóa playlist

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Cập nhật playlist

request.Delete(playlist);

pl.Summary = "updated summary";request.Update(pl);

Thêm video vào playlist

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

// For Playlist object pPlayListMember pm = new PlayListMember();

// Insert <id> or <videoid> for video herepm.Id = VIDEOID;request.AddToPlaylist(p, pm);

Sửa thông tin của playlist

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

// move PlayListMember pm to the top of the playlistpm.Position = 1;request.Update(pm);

request.Delete(playListMember);

Xóa video trong playlist

Lấy subscription của người dùng

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

string feedUrl =  "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/subscriptions";YouTubeQuery query = new YouTubeQuery(feedUrl);SubscriptionFeed subFeed = service.GetSubscriptions(query);

foreach (SubscriptionEntry entry in subFeed.Entries){  Console.WriteLine("Title: " + entry.Title.Text);  Console.WriteLine(entry.Content.Src.Content);}

Thêm subscription

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.channel;s.UserName = "GoogleDevelopers";request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);

Theo dõi 1 danh sách các video yêu thích

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.favorites;s.UserName = "GoogleDevelopers";request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);

Theo dõi Playlist

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.playlist;string playlistId = "586D322B5E2764CF";s.PlaylistId = playlistId;request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);

Theo dõi theo từ khoá tìm kiếm

Một số API mà Youtube hỗ trợLưu trữ và Thu thập video

Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.query;s.QueryString = "puppies";request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);

Xoá subscriptionrequest.Delete(s);

Một số API mà Youtube hỗ trợ

Kích hoạttương tác người dùng

Truy xuất profile của người dùng

Một số API mà Youtube hỗ trợKích hoạt tương tác người dùng

string feedUrl = "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers";ProfileEntry profile = (ProfileEntry) service.Get(feedUrl);...profile.UserNameprofile.Ageprofile.Genderprofile.Relationshipprofile.Booksprofile.Companyprofile.Description…

Truy xuất contact

Một số API mà Youtube hỗ trợKích hoạt tương tác người dùng

string feedUrl = "http://gdata.youtube.com/feeds/api/users/

GoogleDevelopers/contacts";YouTubeQuery query = new YouTubeQuery(feedUrl);FriendsFeed friendFeed = service.GetFriends(query);

Xóa contactfriendEntry.Delete();

Truy xuất tin nhắn

Một số API mà Youtube hỗ trợKích hoạt tương tác người dùng

Xóa tin nhắnentry.Delete();

string feedUrl = "http://gdata.youtube.com/feeds/api/users/default/inbox";YouTubeQuery query = new YouTubeQuery(feedUrl);MessageFeed messagesFeed = service.GetMessages(query);

Gửi tin nhắn

Một số API mà Youtube hỗ trợKích hoạt tương tác người dùng

string friendsInbox = "http://gdata.youtube.com/feeds/api/users/" + receivingUser + "/inbox";string videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0";YouTubeEntry videoEntry = (YouTubeEntry) service.Get(videoEntryUrl);MessageEntry newMessage = new MessageEntry();newMessage.Title.Text = "My Subject";newMessage.Summary.Text = "This is the message body.";newMessage.Id = videoEntry.Id;service.Insert(new Uri(friendsInbox), newMessage);

DEMO

Thank You !!

top related