Last active
February 21, 2016 20:14
-
-
Save p1nesap/2331b299ec33f41247b7 to your computer and use it in GitHub Desktop.
Google YouTube API display real-time channel subscribers, view count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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