Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2013 23:31
Show Gist options
  • Save anonymous/7242109 to your computer and use it in GitHub Desktop.
Save anonymous/7242109 to your computer and use it in GitHub Desktop.
A ChatZilla plugin script that uses the "YouTube Data API v3" to grab the title, duration and publication date from youtube links others post into an IRC channel. It displays the info locally for you to see, and optionally inserts the info into your input history so just hitting Up-arrow and Enter will send it to the channel. This script won't w…
plugin.id = "youtubetitles";
/* Licensed under the Academic Free License version 3.0
- See a copy of the AFL-3.0 at https://spdx.org/licenses/AFL-3.0
- Any copyright dedicated to the public domain: https://creativecommons.org/publicdomain/zero/1.0/
*/
plugin.init =
function _init(glob) {
plugin.major = 0;
plugin.minor = 7;
plugin.version = plugin.major + "." + plugin.minor;
plugin.description = "A script to locally echo the title of a youtube link";
plugin.prefary.push(["ytPutHistory", true, ""]);
plugin.prefary.push(["ytAPIkey", "PASTE_YOUR_KEY_HERE", ""]);
return "OK"; // get a client key from https://code.google.com/apis/console/ by selecting "YouTube Data API v3"
}
plugin.enable =
function _enable() {
client.eventPump.addHook([{set:"channel", type:"privmsg"}], hookMessage, this.id + "-hook-message");
return true;
}
plugin.disable =
function _disable() {
client.eventPump.removeHookByName(this.id + "-hook-message");
return true;
}
function hookMessage(e) {
try {
// Check for a youtube link in the msg
var message = e.msg;
if (/you(tube\.com|tu\.be)\//i.test(message))
url2title(message,e.channel,plugin.prefs["ytAPIkey"]);
} catch(ex) {
// We should never let exceptions get out of a hook, or bad things happen.
}
}
function url2title(link,chan,myKey) {
if (link.toLowerCase().indexOf("youtu.be/") != -1) link = link.replace(/\.be\//i,"v=");
if (link.indexOf("v=") == -1) return;
var vid = link.split("v=")[1].split(/[\?\&\#\s]/)[0],
xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.googleapis.com/youtube/v3/videos?id=" + vid + "&key=" + myKey +
"&part=snippet,contentDetails&fields=items(snippet(title,publishedAt),contentDetails/duration)");
xhr.onload = function(e) {
var vidata = JSON.parse(this.response).items[0],
title = "\x02" + vidata.snippet.title + "\x02",
published = new Date(vidata.snippet.publishedAt),
time = " [" + vidata.contentDetails.duration.substr(2).toLowerCase() + "] ",
output = title + time + published.toDateString().substr(4).replace(/(\d)\s(\d)/,"$1, $2");
chan.display(output);
/* with about:config?filter=extensions.irc.plugins.youtubetitles.ytPutHistory set to *true*
press up-arrow for the last youtube title */
if (plugin.prefs["ytPutHistory"]) {
client.inputHistory.unshift(output);
if (client.inputHistoryLogger) client.inputHistoryLogger.append(output);
if (client.inputHistory.length > client.MAX_HISTORY) client.inputHistory.pop();
client.lastHistoryReferenced = -1;
}
}
xhr.send(); // current limit: 50 million calls per day / 3,000 per second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment