Skip to content

Instantly share code, notes, and snippets.

@p1nesap
Last active February 21, 2016 20:14
Show Gist options
  • Save p1nesap/2331b299ec33f41247b7 to your computer and use it in GitHub Desktop.
Save p1nesap/2331b299ec33f41247b7 to your computer and use it in GitHub Desktop.
Google YouTube API display real-time channel subscribers, view count
function createDoc() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
//Put your unique API key in UNIQUE_KEY section below
var url = "https://www.googleapis.com/youtube/v3/channels?id=UNIQUE_KEY&part=statistics&fields=items(statistics(viewCount, subscriberCount))";
var response = UrlFetchApp.fetch(url);
var astext = response.getContentText();
//var nobrak = astext.replace(/[\])}[{(]/g, '');
//var strip = astext.replace(/[\(\)\[\]{},'"]/g,"").replace("items:", "").replace("statistics:", "").replace("viewCount", "Total Views").replace("subscriberCount", "Subscribers");
var data = JSON.parse(astext);
var views = data.items[0].statistics.viewCount;
var viewsb = data.items[0].statistics.subscriberCount;
Logger.log(data);
parasub = body.insertParagraph(23, "Subscribers: " + viewsb);
paraview = body.insertParagraph(24, "Total Views: " + views);
paraview.setFontSize(16);
parasub.setFontSize(16);
body.editAsText().deleteText(1587, 1624);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment