plugins/youtube.lua

Tue, 31 Jan 2012 18:10:01 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 31 Jan 2012 18:10:01 +0000
changeset 91
076caaedab2c
child 115
6498ca5ed831
permissions
-rw-r--r--

Import youtube plugin

91
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Copyright (C) 2010 Thilo Cestonaro
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 --
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- This project is MIT/X11 licensed.
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 require("net.httpclient_listener");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local http = require("net.http");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local st = require("util.stanza");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local tostring = tostring;
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function riddim.plugins.youtube(bot)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local youtubelink_pattern = "http:%/%/www.youtube.com%/watch%?v=([%a%-%_%d]+)";
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local function bare_reply(event, reply)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if event.stanza.attr.type == 'groupchat' then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local r = st.reply(event.stanza)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local room_jid = jid.bare(event.sender.jid);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if bot.rooms[room_jid] then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 bot.rooms[room_jid]:send(r:tag("body"):text(reply));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 else
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 return event:reply(reply);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local function findYoutubeLink(event)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local body = event.body;
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if not body then return; end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if event.delay then return; end -- Don't process old messages from groupchat
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local videoId = body:match(youtubelink_pattern);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if videoId then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 print("VideoID: "..tostring(videoId));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 http.request("http://gdata.youtube.com/feeds/api/videos/"..tostring(videoId).."?v=2", nil, function (data, code, request)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 print("returned code: "..tostring(code));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 print("-------------------------------------------------------------------------------------------");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 print("returned data: "..tostring(data));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 print("-------------------------------------------------------------------------------------------");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if code ~= 200 then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if code > 0 then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 event:reply("Received HTTP "..code.." error (video gone?)");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 else
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 event:reply("Unable to fetch the XEP list from xmpp.org: "..data:gsub("%-", " "));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return;
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 bare_reply(event, "Title: " .. data:match("<title>(.-)</title>"))
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 bot:hook("message", findYoutubeLink);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 bot:hook("groupchat/joining", function (room)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 room:hook("message", findYoutubeLink);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57

mercurial