YoutubeAPI - nhom-lthd

Download Report

Transcript YoutubeAPI - nhom-lthd

Web API
Youtube API
Thành viên:
Châu Hoàn Sơn – 0912392
Nguyễn Quốc Nhã – 0912317
Nguyễn Văn Lộc – 0912xxx
Nội dung tìm hiểu
I.
II.
III.
IV.
V.
VI.
VII.
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
Một số API mà Youtube hỗ trợ
Demo
Khái niệm
API: Application Programming Interface
(Giao diện lập trình ứng dụng)


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…
Web API
 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,..
Lợi ích của 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
 …
Các dạng Web API
được cung cấp
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ách sử dụng chung
cho Web API
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:
Đối với Web API dạng Link:
đ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 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:
VD:
Hiện 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);
}
}
Cách sử dụng chung cho Web API
 Đố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:
Giới thiệu
Youtube 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
Giới thiệu Youtube API
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.
Một số API mà
Youtube hỗ trợ
Một số API mà Youtube hỗ trợ
I.
II.
III.
IV.
V.
VI.
VII.
VIII.
Chứng thực
Video feed và entry
Truy xuất và tìm kiếm video
Upload video
Cập nhật và xoá video
Sử dụng tính năng cộng đồng
Lưu trữ và chọn lựa video
Kích hoạt tương tác người dùng
Một số API mà Youtube hỗ trợ
Chứng thực
Một số API mà Youtube hỗ trợ
Chứng thực
 Chứng thực việc sử dụng Youtube API
DeveloperKey:
http://code.google.com/apis/youtube/dashboard
YouTubeRequestSettings settings =
new YouTubeRequestSettings("example app", developerKey);
YouTubeRequest request = new YouTubeRequest(settings);
Một số API mà Youtube hỗ trợ
Video feed và entry
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
Uri videoEntryUrl = new Uri(
"http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");
Video video = request.Retrieve<Video>(videoEntryUrl);
printVideoEntry(video);
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
 Nội dung của video entry:
Sử dụng các thuộc tính sau:






video.Title
video.Description
video.Keywords
video.Uploader
video.YouTubeEntry
video.Media





video.Status
video.ReadOnly
video.RatingAverage
video.Contents
…
Một số API mà Youtube hỗ trợ
Video feed và entry
 Nội dung của video entry:
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ợ
Truy xuất và tìm kiếm video
 Truy xuất feed theo chuẩn
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
Một số API mà Youtube hỗ trợ
Truy xuất và tìm kiếm video
 Truy xuất feed theo chuẩn
http://gdata.youtube.com/feeds/api/standardfeeds
/FEED_IDENTIFIER
http://gdata.youtube.com/feeds/api/standardfeeds
/localeID/feedID_CATEGORY_NAME
VD:
http://gdata.youtube.com/feeds/api/
standardfeeds/JP/top_rated_Comedy
Một số API mà Youtube hỗ trợ
Truy xuất và tìm kiếm video
 Truy xuất feed theo chuẩn
Feed<Video> videoFeed =
request.GetStandardFeed(YouTubeQuery.MostPopular);
printVideoFeed(videoFeed);
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
 Truy xuất video feed upload bởi người dùng
http://gdata.youtube.com/feeds/api/users/username/uploads
http://gdata.youtube.com/feeds/api/users/username/uploads/VIDEO_ID
http://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);
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
 Lấy những video liên quan
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)
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
 Tìm kiếm video
Ta sử dụng mẫu URL sau:
http://gdata.youtube.com/feeds/api/videos?
q=keyword&safeSearch=none&orderby=viewCount
VD:
YouTubeQuery query = new
YouTubeQuery(YouTubeQuery.DefaultVideoUri);
//order results by the number of views (most viewed first)
query.OrderBy = "viewCount";
Một số API mà Youtube hỗ trợ
Truy xuất và tìm kiếm video
 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.Moderate
query.Query = "puppy";
query.SafeSearch = YouTubeQuery.SafeSearchValues.None;
Feed<Video> videoFeed = request.Get<Video>(query);
printVideoFeed(videoFeed);
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
 Tìm kiếm video sử dụng category và keyword
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);
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ợ
Upload video
Một số API mà Youtube hỗ trợ
Upload video
 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));
Một số API mà Youtube hỗ trợ
Upload video
 Upload video Trực tiếp
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);
Một số API mà Youtube hỗ trợ
Upload video
 Upload video dựa vào trình duyệt
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);
Một số API mà Youtube hỗ trợ
Upload video
 Upload video dựa vào trình duyệt
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);
Một số API mà Youtube hỗ trợ
Upload video
 Kiểm tra trạng thái upload
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ật
và Xoá video
Một số API mà Youtube hỗ trợ
Cập nhật và Xoá video
 Cập nhật thông tin video
video.Description = "new description";
Video updatedVideo = request.Update(video);
 Xóa video
request.Delete(video);
Một số API mà Youtube hỗ trợ
Sử dụng
tính năng cộng đồng
Một số API mà Youtube hỗ trợ
Sử dụng tính năng cộng đồng
 Đánh giá video
video.Rating = 5;
request.Insert(video.RatingsUri, video);
 Truy xuất Video Comment
Feed comments = request.GetComments(v);
foreach (Comment c in comments.Entries)
{
Console.WriteLine(c.Content);
}
Một số API mà Youtube hỗ trợ
Sử dụng tính năng cộng đồng
 Thêm comment
Comment c = new Comment();
c.Content = "This is my comment.";
request.AddComment(video, c);
// retrieve the comments for the video
Feed fc = request.GetComments(video);
List comments = new List(fc.Entries);
// create a reply to the third comment
Comment c = comments[2];
Comment reply = new Comment();
reply.ReplyTo(c);
reply.Content = "This is the reply text";
request.AddComment(video, reply);
Một số API mà Youtube hỗ trợ
Sử dụng tính năng cộng đồng
 Truy xuất video responses
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);
Một số API mà Youtube hỗ trợ
Sử dụng tính năng cộng đồng
 Thêm video responses
request.Insert(video.ResponseUri, responseVideo);
request.Insert(videoFeed, responseVideo);
 Thêm video responses
request.Delete(responseVideo);
Một số API mà Youtube hỗ trợ
Sử dụng tính năng cộng đồng
 Đánh dấu video
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
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
Feed<Video> feed = request.GetFavoriteVideo(username);
printVideoFeed(videoFeed);
 Xoá video yêu thích
request.Delete(favoriteVideo);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Thêm video yêu thích
string videoEntryUrl =
"http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0";
YouTubeEntry videoEntry = (YouTubeEntry)
service.Get(videoEntryUrl);
service.Insert(new Uri(feedUrl), videoEntry);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Truy xuất playlist của người dùng
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);
}
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Truy xuất thông tin playlist
Feed<PlayListMember> list = request.GetPlaylist(p);
foreach (Video v in list.Entries)
{
printVideoEntry(v);
}
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Thêm playlist
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);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Xóa playlist
request.Delete(playlist);
 Cập nhật playlist
pl.Summary = "updated summary";
request.Update(pl);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Thêm video vào playlist
// For Playlist object p
PlayListMember pm = new PlayListMember();
// Insert <id> or <videoid> for video here
pm.Id = VIDEOID;
request.AddToPlaylist(p, pm);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Sửa thông tin của playlist
// move PlayListMember pm to the top of the playlist
pm.Position = 1;
request.Update(pm);
 Xóa video trong playlist
request.Delete(playListMember);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Lấy subscription của người dùng
string feedUrl
= "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/s
ubscriptions";
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);
}
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Thêm subscription
Subscription s = new Subscription();
s.Type = SubscriptionEntry.SubcriptionType.channel;
s.UserName = "GoogleDevelopers";
request.Insert(new
Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Theo dõi 1 danh sách các video yêu thích
Subscription s = new Subscription();
s.Type = SubscriptionEntry.SubcriptionType.favorites;
s.UserName = "GoogleDevelopers";
request.Insert(new
Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Theo dõi Playlist
Subscription s = new Subscription();
s.Type = SubscriptionEntry.SubcriptionType.playlist;
string playlistId = "586D322B5E2764CF";
s.PlaylistId = playlistId;
request.Insert(new
Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);
Một số API mà Youtube hỗ trợ
Lưu trữ và Thu thập video
 Theo dõi theo từ khoá tìm kiếm
Subscription s = new Subscription();
s.Type = SubscriptionEntry.SubcriptionType.query;
s.QueryString = "puppies";
request.Insert(new
Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);
 Xoá subscription
request.Delete(s);
Một số API mà Youtube hỗ trợ
Kích hoạt
tương tác người dùng
Một số API mà Youtube hỗ trợ
Kích hoạt tương tác người dùng
 Truy xuất profile của người dùng
string feedUrl = "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers";
ProfileEntry profile = (ProfileEntry) service.Get(feedUrl);
...
profile.UserName
profile.Age
profile.Gender
profile.Relationship
profile.Books
profile.Company
profile.Description
…
Một số API mà Youtube hỗ trợ
Kích hoạt tương tác người dùng
 Truy xuất contact
string feedUrl = "http://gdata.youtube.com/feeds/api/users/
GoogleDevelopers/contacts";
YouTubeQuery query = new YouTubeQuery(feedUrl);
FriendsFeed friendFeed = service.GetFriends(query);
 Xóa contact
friendEntry.Delete();
Một số API mà Youtube hỗ trợ
Kích hoạt tương tác người dùng
 Truy xuất tin nhắn
string feedUrl =
"http://gdata.youtube.com/feeds/api/users/default/inbox";
YouTubeQuery query = new YouTubeQuery(feedUrl);
MessageFeed messagesFeed = service.GetMessages(query);
 Xóa tin nhắn
entry.Delete();
Một số API mà Youtube hỗ trợ
Kích hoạt tương tác người dùng
 Gửi tin nhắn
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 !!