gpt4 book ai didi

ios - 按 channel 名称检索 YouTube channel 视频

转载 作者:行者123 更新时间:2023-11-28 19:58:03 26 4
gpt4 key购买 nike

我正在寻找 Youtube Data API V3 中的两个 API 调用。

首先我想通过指定 channel 名称来获取 channel ID。

获得 channel ID 后,我想从该 channel 获取 n 个视频。

我正在寻找必须进行的 API 调用。

另外,有人知道 channel 或视频 ID 是否有可能在某个时候发生变化吗?如果他们可以针对相同的视频/ channel 进行更改,那么我不应该在我的代码中对 ID 进行硬编码。

谢谢

最佳答案

对于想要运行示例的所有新手:考虑一个有助于理解获取、解析、显示等整个周期的函数,并将 youtube channel 的视频专门带到您的 TableView 中。我不是在这里写 tableview 部分

-(void)initiateRequestToYoutubeApiAndGetChannelInfo
{
NSString * urlYouCanUseAsSample = @"https://www.googleapis.com/youtube/v3/search?key={YOUR_API_KEY_WITHOUT_CURLY_BRACES}&channelId={CHANNEL_ID_YOU_CAN_GET_FROM_ADDRESS_BAR_WITHOUT_CURLY_BRACES}&part=snippet,id&order=date&maxResults=20";

NSURL *url = [[NSURL alloc] initWithString: urlYouCanUseAsSample];

// Create your request
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// Send the request asynchronously remember to reload tableview on global thread
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

// Callback, parse the data and check for errors
if (data && !connectionError) {
NSError *jsonError;

NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];

if (!jsonError) {
// better put a breakpoint here to see what is the result and how it is brought to you. Channel id name etc info should be there

NSLog(@"%@",jsonResult);

/// separating "items" dictionary and making array

//
id keyValuePairDict = jsonResult;
NSMutableArray * itemList = keyValuePairDict[@"items"];
for (int i = 0; i< itemList.count; i++) {

/// separating VIDEO ID dictionary from items dictionary and string video id
id v_id0 = itemList[i];
NSDictionary * vid_id = v_id0[@"id"];
id v_id = vid_id;
NSString * video_ID = v_id[@"videoId"];

//you can fill your local array for video ids at this point

// [video_IDS addObject:video_ID];

/// separating snippet dictionary from itemlist array
id snippet = itemList[i];
NSDictionary * snip = snippet[@"snippet"];

/// separating TITLE and DESCRIPTION from snippet dictionary
id title = snip;
NSString * title_For_Video = title[@"title"];
NSString * desc_For_Video = title[@"description"];

//you can fill your local array for titles & desc at this point

// [video_titles addObject:title_For_Video];
// [video_description addObject:desc_For_Video];




/// separating thumbnail dictionary from snippet dictionary

id tnail = snip;
NSDictionary * thumbnail_ = tnail[@"thumbnails"];

/// separating highresolution url dictionary from thumbnail dictionary

id highRes = thumbnail_;
NSDictionary * high_res = highRes[@"high"];

/// separating HIGH RES THUMBNAIL IMG URL from high res dictionary

id url_for_tnail = high_res;
NSString * thumbnail_url = url_for_tnail[@"url"];
//you can fill your local array for titles & desc at this point

[video_thumbnail_url addObject:thumbnail_url];


}
// reload your tableview on main thread
//[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
performSelectorOnMainThread:@selector(reloadInputViews) withObject:nil waitUntilDone:NO];


// you can log all local arrays for convenience
// NSLog(@"%@",video_IDS);
// NSLog(@"%@",video_titles);
// NSLog(@"%@",video_description);
// NSLog(@"%@",video_thumbnail_url);
}
else
{
NSLog(@"an error occurred");
}
}
}];
}

关于ios - 按 channel 名称检索 YouTube channel 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com