plugins/youtube.lua

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

mercurial