Introduction
要使用這個 GData 之前,確保你已經了解 Google Api feed 內容是什麼,若還不了解請查詢 Understanding Video Feeds and Entries
最近工作內容要用 Youtube API V2 撈資料,在 android 上很簡單,解解 JSON 爬爬 XML 就好,但是在 iOS 上就覺得很麻煩了。因此搜尋了一下發現原來 Google 有出 GData objective-c client 端的程式,雖然最後一次更新已經有點久了,但是功能幾本上沒有缺。
使用起來也很方便,完全不需要去爬 XML (註1),開發速度就快不少!而且也支援 batch operation,如果要用 Google API 的話,這一套推薦使用!
註1:為什麼要爬 XML ?因為有些 Google API 的 batch operation 回傳的資料只支援 XML 格式,沒有 JSON 格式,不然我也不想爬 XML 呀!
Installation
安裝最快速的方法就是透過 cocoapods,一鍵安裝。
platform :ios, '6.0'
pod 'GData'
pod 'AFNetworking'
Example
這個範例是透過 GData,抓出 Youtube 的 most popular 排行榜,並秀在 tableView 上面。
首先,建立出 youtube service。並找出想要的 feedURL,透過 service 去撈。
// Create youtube service
GDataServiceGoogleYouTube* service = [[GDataServiceGoogleYouTube alloc] init];
[service setYouTubeDeveloperKey:YOUTUBE_API_V2_KEY];
// Get "most popular" feed url
NSURL* feedURL = [GDataServiceGoogleYouTube youTubeURLForFeedID:kGDataYouTubeFeedIDMostPopular];
// Start fetch
[service fetchFeedWithURL:feedURL
completionHandler:^(GDataServiceTicket* ticket, GDataFeedBase* feed, NSError* error) {
if (ticket.statusCode / 100 != 2)
{
[[NSNotificationCenter defaultCenter] postNotificationName:YoutubeApiFetchFailedNotification
object:nil];
return;
}
NSMutableArray* youtubeVideos = [NSMutableArray array];
// Convert to our model
for (GDataEntryYouTubeVideo* entry in [feed entries])
{
MyYoutubeModel* youtubeModel = [[MyYoutubeModel alloc] init];
youtubeModel.title = entry.title.stringValue;
youtubeModel.viewCount = entry.statistics.viewCount;
GDataMediaThumbnail* thumbnail = entry.mediaGroup.mediaThumbnails[0];
youtubeModel.thumbnailUrl = thumbnail.URLString;
[youtubeVideos addObject:youtubeModel];
}
// Post to view controller
[[NSNotificationCenter defaultCenter] postNotificationName:YoutubeApiFetchSuccessNotification
object:[youtubeVideos copy]];
}];
以上短短幾行程式就可以撈出目前 youtube 的 most popular 影片啦。這個 library 方便的地方在於, 它都幫你包好了 ,像是 title, view count, thumbnails 等,不用再去爬那容易出錯的 XML 了,寫起來真的快速又方便呀!
(可以對照 Understanding Video Feeds and Entries 就知道每個欄位的意義囉)
如果你想 Query 的話,也可以用以下 function 來 query。
- (GDataServiceTicket *)fetchFeedWithQuery:(GDataQuery *)query
completionHandler:(void (^)(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error))handler;
Batch Request
另外更重要的一點就是有支援 batch processing,有關 batch processing 的說明可以參考 Using Batch Processing,以及 GData Objective-c Client 的Batch requests。
Batch Processing,重點就是要拿到 BATCH URL 跟產生 Batch Entries,其中 batch url 要先透過第一次的回傳值取得 NSURL* batchURL = [[feed batchLink] URL]
以下我們利用剛剛 most popular 抓到的影片,透過 batch request 取得全部影片的詳細資訊:
- (void) batchRequestWithVideos:(NSMutableArray*) videos
{
// Create Youtube service
GDataServiceGoogleYouTube* service = [[GDataServiceGoogleYouTube alloc] init];
[service setYouTubeDeveloperKey:YOUTUBE_API_V2_KEY];
// Retrieving Data for a Single Video
// you can check https://developers.google.com/youtube/2.0/developers_guide_protocol_video_entries
NSURL* feedURL = [GDataServiceGoogleYouTube youTubeURLForFeedID:kGDataYouTubeFeedIDFull];
[service fetchFeedWithURL:feedURL
completionHandler:^(GDataServiceTicket* ticket, GDataFeedBase* feed, NSError* error) {
if (ticket.statusCode / 100 != 2)
{
[[NSNotificationCenter defaultCenter] postNotificationName:YoutubeApiFetchFailedNotification
object:nil];
return;
}
// Get Batch URL
NSURL* batchURL = [[feed batchLink] URL];
GDataFeedYouTubeVideo* feedYouTubeVideo = [GDataFeedYouTubeVideo videoFeed];
// Create batch request entries
// it just like this:
/*
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/"
xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:yt="http://gdata.youtube.com/schemas/2007">
<batch:operation type="query" />
<entry>
<id>http://gdata.youtube.com/feeds/api/videos/hLQl3WQQoQ0</id>
</entry>
<entry>
<id>http://gdata.youtube.com/feeds/api/videos/SkTt9k4Y-a8</id>
</entry>
<entry>
<id>http://gdata.youtube.com/feeds/api/videos/pRpeEdMmmQ0</id>
</entry>
<entry>
<id>http://gdata.youtube.com/feeds/api/videos/2FM4UPrAjnc</id>
</entry>
</feed>
* */
for (MyYoutubeModel* video in videos)
{
GDataEntryYouTubeVideo* youTubeVideoEntry = [GDataEntryYouTubeVideo videoEntry];
youTubeVideoEntry.identifier = [NSString stringWithFormat:@"%@%@",
@"http://gdata.youtube.com/feeds/api/videos/",
video.videoId];
[feedYouTubeVideo addEntry:youTubeVideoEntry];
}
GDataBatchOperation* op;
op = [GDataBatchOperation batchOperationWithType:kGDataBatchOperationQuery];
[feedYouTubeVideo setBatchOperation:op];
// Now we can send a batch request for all videos with only "one" request.
[self fetchBatchWithService:service feed:feedYouTubeVideo
batchURL:batchURL];
}];
}
- (void) fetchBatchWithService:(GDataServiceGoogleYouTube*) service feed:(GDataFeedYouTubeVideo*) youtubeVideoFeed batchURL:(NSURL*) batchURL
{
[service fetchFeedWithBatchFeed:youtubeVideoFeed forBatchFeedURL:batchURL
completionHandler:^(GDataServiceTicket* ticket, GDataFeedBase* feed, NSError* error) {
if (ticket.statusCode / 100 != 2)
{
[[NSNotificationCenter defaultCenter] postNotificationName:YoutubeApiFetchFailedNotification
object:nil];
return;
}
NSMutableArray* youtubeEntries = [[NSMutableArray alloc] init];
for (GDataEntryYouTubeVideo* entry in [feed entries])
{
// here's all your video entry
NSLog(@">>>> entry:%@", entry);
}
}];
}
這樣一來就不用連續打十幾個 request 去要資料了,很方便。
Summary
如果要快速開發 GData 相關 app,可以用這套 GData Objective-C Client 來加快開發速度唷!
Reference: