gpt4 book ai didi

ios - Youtube API v3 search.list 返回不相关的视频 Swift

转载 作者:搜寻专家 更新时间:2023-10-31 22:48:12 24 4
gpt4 key购买 nike

我正在尝试做一些非常简单的事情;在我的应用程序中获得与您搜索 Youtube.com 并按 上传计数 排序时显示的相同的视频结果。

几乎其他一切都正常,我可以:

  • 获取缩略图、标题和 channel 名称
  • 播放视频

*也在努力获取每个视频的观看次数(我听说您需要 创建两个请求?)

真正让我困惑的是这段代码:

var urlString = "https://www.googleapis.com/youtube/v3/search?
part=snippet
&fields=items(id,snippet(title,channelTitle,thumbnails))
&order=viewCount
&q=\(searchBar.text)
&type=video
&maxResults=25&key=\(apiKey)"

产生这个结果:

enter image description here

而不是像这样:

*不包括播放列表

enter image description here

我的代码有什么问题?

  func searchBarSearchButtonClicked(searchBar: UISearchBar) {

searchBar.resignFirstResponder()

// Form the request URL string.
var urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id,snippet(title,channelTitle,thumbnails))&order=viewCount&q=\(searchBar.text)&type=video&maxResults=25&key=\(apiKey)"

urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

// Create a NSURL object based on the above string.
let targetURL = NSURL(string: urlString)

// Get the results.
performGetRequest(targetURL, completion: { (data, HTTPStatusCode, error) -> Void in
if HTTPStatusCode == 200 && error == nil {
// Convert the JSON data to a dictionary object.
let resultsDict = (try! NSJSONSerialization.JSONObjectWithData(data!, options: [])) as! Dictionary<NSObject, AnyObject>

// Get all search result items ("items" array).
let items: Array<Dictionary<NSObject, AnyObject>> = resultsDict["items"] as! Array<Dictionary<NSObject, AnyObject>>

// Loop through all search results and keep just the necessary data.
for var i=0; i<items.count; ++i {
let snippetDict = items[i]["snippet"] as! Dictionary<NSObject, AnyObject>
// let statisticsDict = items[i]["statistics"] as! Dictionary<NSObject, AnyObject>

// Create a new dictionary to store the video details.
var videoDetailsDict = Dictionary<NSObject, AnyObject>()
videoDetailsDict["title"] = snippetDict["title"]
videoDetailsDict["channelTitle"] = snippetDict["channelTitle"]
videoDetailsDict["thumbnail"] = ((snippetDict["thumbnails"] as! Dictionary<NSObject, AnyObject>)["default"] as! Dictionary<NSObject, AnyObject>)["url"]
videoDetailsDict["videoID"] = (items[i]["id"] as! Dictionary<NSObject, AnyObject>)["videoId"]
// videoDetailsDict["viewCount"] = statisticsDict["viewCount"]

self.videosArray.append(videoDetailsDict)

// Reload the tableview.
self.tableView.reloadData()

}

}
else {
print("HTTP Status Code = \(HTTPStatusCode)")
print("Error while loading channel videos: \(error)")
}

})

}


// MARK: Custom method implementation

func performGetRequest(targetURL: NSURL!, completion: (data: NSData?, HTTPStatusCode: Int, error: NSError?) -> Void) {
let request = NSMutableURLRequest(URL: targetURL)
request.HTTPMethod = "GET"

let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()

let session = NSURLSession(configuration: sessionConfiguration)

let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completion(data: data, HTTPStatusCode: (response as! NSHTTPURLResponse).statusCode, error: error)
})
})

task.resume()
}

最佳答案

这个问题其实很简单,让我感到宽慰/沮丧。

在 url 中,这是问题所在:

\(searchBar.text)

searchBar.text 是可选的,这就是返回的结果与 youtube.com 搜索不同的原因。简单修复:

\(searchBar.text!)

关于ios - Youtube API v3 search.list 返回不相关的视频 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33489230/

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