plugins/youtube.lua

Thu, 22 Oct 2020 15:37:43 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 22 Oct 2020 15:37:43 +0100
changeset 161
c4df517edbc1
parent 115
6498ca5ed831
permissions
-rw-r--r--

config.docker.lua: Require RIDDIM_DEBUG=1 to enable debug mode

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 local http = require("net.http");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local st = require("util.stanza");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local tostring = tostring;
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 function riddim.plugins.youtube(bot)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local youtubelink_pattern = "http:%/%/www.youtube.com%/watch%?v=([%a%-%_%d]+)";
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local function bare_reply(event, reply)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 if event.stanza.attr.type == 'groupchat' then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local r = st.reply(event.stanza)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local room_jid = jid.bare(event.sender.jid);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if bot.rooms[room_jid] then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 bot.rooms[room_jid]:send(r:tag("body"):text(reply));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 else
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return event:reply(reply);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
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
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local function findYoutubeLink(event)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local body = event.body;
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if not body then return; end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 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
28
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local videoId = body:match(youtubelink_pattern);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if videoId then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 print("VideoID: "..tostring(videoId));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 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
34 print("returned code: "..tostring(code));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 print("-------------------------------------------------------------------------------------------");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 print("returned data: "..tostring(data));
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 print("-------------------------------------------------------------------------------------------");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if code ~= 200 then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if code > 0 then
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 event:reply("Received HTTP "..code.." error (video gone?)");
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 else
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 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
43 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return;
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 bare_reply(event, "Title: " .. data:match("<title>(.-)</title>"))
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end);
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
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 bot:hook("message", findYoutubeLink);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 bot:hook("groupchat/joining", function (room)
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 room:hook("message", findYoutubeLink);
076caaedab2c Import youtube plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end);
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

mercurial