Skip to content

Instantly share code, notes, and snippets.

@KBelau
Last active June 11, 2019 14:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KBelau/6f549448e2de3cd0381e735a902cbd23 to your computer and use it in GitHub Desktop.
Save KBelau/6f549448e2de3cd0381e735a902cbd23 to your computer and use it in GitHub Desktop.
Template - YouTube Brand Guardian
/* Auto-Generated File - Don't mess with it. */
var __LINE_FILE_LOOKUP = {};
function __CURRENT_LINE() { try { throw new Error(); } catch (e) { return parseInt(e.stack.split("\n")[1].match(/:[0-9]*/igm)[0].substring(1)); } }
function __LOAD(file, relative, line, name) { __LINE_FILE_LOOKUP[line] = (relative ? file : name); return UrlFetchApp.fetch((relative ? (__URL + file) : (file)) + "?d=" + new Date().getTime()).getBlob().getDataAsString(); }
function __WRAP_FN(fn) { return function() { try { return fn.apply(this, arguments); } catch (e) { if (e.hasOwnProperty("_f")) throw e; throw __FMT_ERROR(e); } }; } var __WRAP_FN_LINE = __CURRENT_LINE();
function __FMT_ERROR(e) { if (typeof(e) !== "object") return e; var stck = e.stack.split("\n"), result = []; for (var i = 0; i < stck.length; ++i) { var matches = stck[i].match(/#([0-9]*)\(eval\)/igm);
if (matches === null) { result.push(stck[i].replace("Code", "<code>")); } else if (matches.length == 1) { if (parseInt(stck[i].match(/:[0-9]*/igm)[0].substring(1)) == __WRAP_FN_LINE)
continue; result.push(stck[i].replace(/Code(#[0-9]*\(eval\))*/igm, "<internal-startup>")); } else { var line = matches[1].replace(/[^0-9]*/igm, "");
if (__LINE_FILE_LOOKUP.hasOwnProperty(line)) { result.push(stck[i].replace(/Code#[0-9]*\(eval\)#[0-9]*\(eval\)/im, __LINE_FILE_LOOKUP[line])); } else {
result.push(stck[i].replace(/Code#[0-9]*\(eval\)/im, "<internal-startup>")); } } } var err = new Error((e.message + "\n" + result.join("\n")).trim()); err._f = true; return err; }
try {
/*DEPENDENCY_TEMPLATE[=[eval(__LOAD("$URL", false, __CURRENT_LINE(), "$NAME"));]=]*/
/*BEGIN_DEPENDENCIES*/
/*END_DEPENDENCIES*/
/*SCRIPT_TEMPLATE[=[eval(__LOAD("$LIB", true, __CURRENT_LINE()));]=]*/
/*BEGIN_SCRIPTS*/
eval(__LOAD("main.js", true, __CURRENT_LINE()));
/*END_SCRIPTS*/
} catch (e) { throw __FMT_ERROR(e); }
for (var keys = Object.keys(this), i = 0, len = keys.length; i < len; ++i) { if (typeof(this[keys[i]]) === "function") { this[keys[i]] = __WRAP_FN(this[keys[i]]); } }
//Set the Minimum Likes/Dislikes ratio i.e. a ratio is 10 would be 10 likes for every 1 dislike
var minratio = <Your Like to Dislike Threshold>;
function main() {
var Spreadsheet = SpreadsheetApp.openByUrl("<Spreadsheet>");
var Sheet = Spreadsheet.getSheetByName("Ratio Violation");
Sheet.clear();
Sheet.appendRow(["URL", "Views", "Dislikes", "Likes", "Ratio", "Reason"]);
var placements = AdWordsApp.videoTargeting().placements().forDateRange("LAST_30_DAYS").get();
var dupecheck = [];
while (placements.hasNext()) {
var placement = placements.next();
var url = placement.getUrl();
if (url != dupecheck) {
var dupecheck = [];
var ary = url.split('/');
var vidid = ary[2];
if (vidid !== undefined) {
var list = YouTube.Videos.list("Statistics", {
id: vidid
});
var snippet = YouTube.Videos.list("Snippet", {
id: vidid
});
var snippetitems = snippet.items;
var items = list.items;
var ritems = items[0];
var rsnitems = snippetitems[0];
if (ritems !== undefined) {
var stats = ritems["statistics"];
var title = rsnitems["title"];
Logger.log(title);
var views = stats["viewCount"];
var dis = stats["dislikeCount"];
var like = stats["likeCount"];
var com = ["commentCount"];
var ratio = like / dis;
if (ratio < minratio) {
Sheet.appendRow([url, views, dis, like, ratio, "Ratio too low"]);
}
dupecheck.push(url);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment