获取 YouTube 视频观看次数 YouTube API

Posted

技术标签:

【中文标题】获取 YouTube 视频观看次数 YouTube API【英文标题】:Get YouTube Video Views YouTube API 【发布时间】:2018-01-11 18:50:09 【问题描述】:

我正在使用 YouTube API 为我的 YouTube 频道创建一个应用。我通过这样做从使用 Alamofire 的播放列表中获取视频:

let parameters = ["part":"snippet","maxResults":50,"channelId":CHANNEL_ID,"playlistId":UPLOADS_PLAYLIST_ID,"key":API_KEY] as [String : Any]

class VideoModel: NSObject 

    var videoArray = [Video]()


    func getFeedVideo() 
        Alamofire.request("https://www.googleapis.com/youtube/v3/playlistItems", parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON  (response) in

            if let JSON = response.result.value 

                if let dictionary = JSON as? [String: Any] 

                    var arrayOfVideos = [Video]()

                    guard let items = dictionary["items"] as? NSArray else  return 

                    for items in dictionary["items"] as! NSArray 
                        print(items)
                        //  Create video objects off of the JSON response

                        let videoObj = Video()
                        videoObj.videoID = (items as AnyObject).value(forKeyPath: "snippet.resourceId.videoId") as! String
                        videoObj.videoTitle = (items as AnyObject).value(forKeyPath: "snippet.title") as! String
                        videoObj.videoDescription = (items as AnyObject).value(forKeyPath: "snippet.description") as! String
                        videoObj.videoThumbnailUrl = (items as AnyObject).value(forKeyPath: "snippet.thumbnails.maxres.url") as! String


                        arrayOfVideos.append(videoObj)

                    
                    self.videoArray = arrayOfVideos
                    if self.delegate != nil 
                        self.delegate!.dataReady()
                    
                
            
        
    

我一直在四处寻找,但找不到一个简单的方法来做到这一点。我找到了这样的答案:How to get number of video views with YouTube API? 但是,我使用的是 Swift。有人有什么想法吗?

【问题讨论】:

How to get number of video views with YouTube API?的可能重复 @Jacob:有什么问题?您是否收到数据?你想获得视频计数吗?您想获取视频 ID 以将其附加到 Youtube 视频播放器吗? ***.com/questions/3331176/…的可能重复 @nathan 我想获得 YouTube 视频的观看次数。 【参考方案1】:

工作解决方案。我会使用 Decodable 而不是原始 JSON,但我在最后一个请求中遵循了您的代码。

let apiKey = "API_KEY"
let playlistId = "PLAYLIST_ID"
let url = URL(string: "https://www.googleapis.com/youtube/v3/playlistItems?playlistId=\(playlistId)&maxResults=25&part=snippet%2CcontentDetails&key=\(apiKey)")!

// Get videos
Alamofire.request(url).responseJSON  (response) in

    guard let JSON = response.result.value,
        let dictionary = JSON as? [String: Any],
        let items = dictionary["items"] as? [AnyObject]
        else  return 

    let videos: [Video] = items.map 
        let videoObj = Video()
        videoObj.videoID = $0.value(forKeyPath: "snippet.resourceId.videoId") as! String
        videoObj.videoTitle = $0.value(forKeyPath: "snippet.title") as! String
        videoObj.videoDescription = $0.value(forKeyPath: "snippet.description") as! String
        videoObj.videoThumbnailUrl = $0.value(forKeyPath: "snippet.thumbnails.default.url") as! String
        return videoObj
    

    let videoIds = videos.map( $0.videoID ).joined(separator: ",")
    let statsUrl = URL(string: "https://www.googleapis.com/youtube/v3/videos?part=statistics&id=\(videoIds)&key=\(apiKey)")!

    // Get view count for all videos
    Alamofire.request(statsUrl).responseJSON  (response) in

        guard let JSON = response.result.value,
            let dictionary = JSON as? [String: Any],
            let items = dictionary["items"] as? [AnyObject]
            else  return 

        for i in 0..<items.count 
            let views = items[i].value(forKeyPath: "statistics.viewCount") as! String
            videos[i].viewCount = Int(views)!
        

        print(videos)
    

【讨论】:

以上是关于获取 YouTube 视频观看次数 YouTube API的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 YouTube API V3 获取视频观看次数?

youtube api v3 - 从播放列表中获取视频,其中包含按观看次数排序的观看次数数据

在 YouTube V3 API for java 中获取观看次数

检索单个视频的观看次数 - Youtube API V3.0 - JavaScript

如何使用 Java API 访问 YouTube 观看次数

带有“autoplay=1”的嵌入式 youtube 视频。它是不是计入观看次数?