1

I'm trying to get the view counts for Youtube videos.

"snippet": {
    "publishedAt": datetime,
    "channelId": string,
    "title": string,
    "description": string,
    "thumbnails": {
      (key): {
        "url": string,
        "width": unsigned integer,
        "height": unsigned integer
      }
    },
    "channelTitle": string,
    "tags": [
      string
    ],
    "categoryId": string,
    "liveBroadcastContent": string,
    "defaultAudioLanguage": string
  },

  "statistics": {
    "viewCount": unsigned long,
    "likeCount": unsigned long,
    "dislikeCount": unsigned long,
    "favoriteCount": unsigned long,
    "commentCount": unsigned long

This is the JSON that needs parsing. I found a tutorial online with snippet correctly parsed like so:

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"]

                    // Append the desiredPlaylistItemDataDict dictionary to the videos array.
                    self.videosArray.append(videoDetailsDict)

You can see my attempts at trying to get the statistics data by doing the some type of parsing shown here for snippet but I have no luck, the result is alway nil for "viewCount" in videoDetailsDict.

What am I doing wrong?

Wesley Cho
  • 495
  • 8
  • 23
  • you should start changing your dictionary declarations from Dictionary to Dictionary – Leo Dabus Oct 18 '15 at 19:48
  • make sure `targetURL` contains `statistics` in the part – Saumini Navaratnam Oct 19 '15 at 13:48
  • `var urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet,statistics&q=\(searchBar.text)&type=video&maxResults=15&key=\(apiKey)"` doesn't work. Do you have to do the GETs separately? – Wesley Cho Oct 19 '15 at 17:50
  • 3
    Possible duplicate of [How to use YouTube API V3?](http://stackoverflow.com/questions/30290483/how-to-use-youtube-api-v3) – cimmanon Feb 12 '16 at 19:55

1 Answers1

0

Make sure to add statistics in the part parameter as follows: https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.videos.list?part=snippet%252Cstatistics&id=QJYMdGpJQFs&_h=10&

Plus, if you want just the video count(or any specific info) I recommend you to use the fields parameter to make a smaller and clearer response.

i.e getting just the viewCount with fields=items/statistics(viewCount) https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.videos.list?part=snippet%252Cstatistics&id=QJYMdGpJQFs&fields=items%252Fstatistics(viewCount)&_h=5&

henrique
  • 1,072
  • 10
  • 17