0

I'm working with some faculty members studying public health and social media. I'd like to be able to insert a number of YouTube URLs to videos I do not own and pull the public stats (views, likes/dislikes, video length, owner, comment count).

It seems that the API options require that you own the videos to get any of this data.

I've got a way to do most of what I need through xpath but want to make sure I'm not missing something obvious in the API.

closed as off-topic by phwd Feb 19 '16 at 6:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "It is a question about creating/developing a web application." – phwd
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Would the expanded response below push it more into web apps or farther away? It is about Google Sheets and YouTube (both web apps- to me anyway). Is scripting Google Sheets considered a web app? – Tom Woodward Feb 19 '16 at 15:20
1

You can do this, just not through the analytics option.

https://www.googleapis.com/youtube/v3/videos?part=statistics%2C+contentDetails&id=Pxb5lSPLy9c&key={YOURKEY}

via this response

I ended up turning this into a Google Script function so it could be called regularly and add the data to a Google Sheet. I also added a bit to the data (snippet,contentDetails,statistics,status) being requested to get the name and a few other things you might want to grab.

function getYTdata(userData) {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getSheetByName("data"); //or whatever you name your sheet
  var apiKey = '********YOUR_API_KEY***********'; //leave the single quotes
  var vidId = 'm5vw4ajnWGA'; //single video for now, putting in the loop later
  var url = 'https://www.googleapis.com/youtube/v3/videos?id=' + vidId + '&key=' + apiKey + '&part=snippet,contentDetails,statistics,status'; // replace this with your API client ID
  //take a look at the raw JSON for this request at https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=AIzaSyBAPaX4HvWBniJ5F6vjUUZ-VsdxPLPzWzE&part=snippet,contentDetails,statistics,status
  var currentTime = new Date();

  var response = UrlFetchApp.fetch(url); // get feed
  var json = response.getContentText(); // 
  var data = JSON.parse(json);
  var stats = [];

    stats.push(data.items[0].snippet.title); //video title
    stats.push(data.items[0].statistics.viewCount); //view count
    stats.push(data.items[0].statistics.likeCount); //like count
    stats.push(data.items[0].statistics.dislikeCount); //dislike count
    stats.push(data.items[0].statistics.favoriteCount); //fav count
    stats.push(data.items[0].statistics.commentCount); //comment count
    stats.push(data.items[0].contentDetails.duration); //duration but in a weird format PT6M44S is 6 min 44 secs
    stats.push(data.items[0].snippet.publishedAt); //publish date
    stats.push(data.items[0].snippet.channelTitle); //title of the channel or user?

    SpreadsheetApp.getActiveSheet().appendRow(stats)

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.