Last active
September 18, 2019 01:47
-
-
Save donpdonp/a3e2ea884ff7398820f9fe67c8046296 to your computer and use it in GitHub Desktop.
gluon omdb/imdb
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() { | |
// setup | |
setup() | |
// descriptor | |
return {name:"omdb"} | |
}) | |
var api_key | |
function setup() { | |
db.get('omdb:key', function(key){ | |
if(key) { | |
api_key = key | |
bot.say(bot.admin_channel, "omdb: api key loaded") | |
} else { | |
bot.say(bot.admin_channel, "omdb: api key missing") | |
} | |
}) | |
} | |
function go(msg) { | |
if (msg.method == "irc.privmsg") { | |
var cmd = /^![oi]mdb(\s+(.*))?/.exec(msg.params.message) | |
if(cmd) { | |
if(cmd[2]) { | |
var desc = movie(cmd[2]) | |
//bot.say(msg.params.channel, JSON.stringify(desc)) | |
if(desc.Error) { | |
// no exact match | |
var results = search(cmd[2]) | |
if(results.Error) { | |
bot.say(msg.params.channel, msg.params.nick+": "+JSON.stringify(cmd[2])+" "+results.Error) | |
} else { | |
bot.say(msg.params.channel, msg.params.nick+": "+JSON.stringify(cmd[2])+" found "+results.totalResults+" hits") | |
bot.say(msg.params.channel, JSON.stringify(results)) | |
} | |
} else { | |
bot.say(msg.params.channel, ""+desc.Title+" ("+desc.Year+") "+desc.Plot+" [omdbapi.com]") | |
} | |
} else { | |
bot.say(msg.params.channel, "!omdb <movie title>") | |
} | |
} | |
} | |
} | |
function movie(name) { | |
var nameuri = encodeURIComponent(name) | |
var json = http.get("http://www.omdbapi.com/?apikey="+api_key+"&t="+nameuri) | |
return JSON.parse(json) | |
} | |
function search(name) { | |
var nameuri = encodeURIComponent(name) | |
var json = http.get("http://www.omdbapi.com/?apikey="+api_key+"&s="+nameuri) | |
return JSON.parse(json) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment