author | Mikael Berthe <mikael@lilotux.net> |
Tue, 27 Nov 2012 16:26:04 +0100 | |
changeset 66 | d9c00a9fe9d5 |
parent 65 | 2cefbe9f3ac6 |
permissions | -rw-r--r-- |
64
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
|
66
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
2 |
-- This module is derivated from isbear's shortenurl function: |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
3 |
-- http://hg.lilotux.net/mod-mcabber-lua/file/tip/examples/shortenurl.lua |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
4 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
5 |
-- Copyright (C) 2012 Myhailo Danylenko <isbear@ukrpost.net> |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
6 |
-- Copyright (C) 2012 Mikael Berthe <mikael@lilotux.net> |
64
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
7 |
-- |
66
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
8 |
-- This program is free software; you can redistribute it and/or modify |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
9 |
-- it under the terms of the GNU General Public License as published by |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
10 |
-- the Free Software Foundation; either version 2 of the License, or (at |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
11 |
-- your option) any later version. |
64
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
-- |
66
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
13 |
-- Please check the license in the COPYING file at the root of the tree. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
14 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
65
diff
changeset
|
15 |
|
64
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
16 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
require "libs.shcmd" |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
19 |
local json = require 'json' |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
local shortenurl = { ["desc"] = "URL shortener", |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
22 |
} |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 |
local encodeoptions = { |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
25 |
strings = { |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
26 |
encodeSet = '\\"%z\1-\031', -- do not escape /, google does not understand that |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
27 |
} |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
28 |
} |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
29 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
30 |
local shell_escape = function (str) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
if str then |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
return "'" .. str:gsub("'", "'\\''") .. "'" |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 |
else |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 |
return "''" |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 |
end |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
end |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
local curlcommand = "curl -s https://www.googleapis.com/urlshortener/v1/url -H 'Content-type: application/json' -d %s" |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
function shortenurl.cmd (url) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
if ( not url ) or ( not url:match ( '%w' ) ) then |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 |
return nil, 'You must specify an URL' |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 |
end |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
local replystring = '' |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 |
local targeturl = json.encode( { longUrl = url }, encodeoptions ) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 |
local data = shcmd(curlcommand:format(shell_escape(targeturl))) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 |
if data then |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 |
local reply = json.decode ( data ) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 |
if ( type ( reply ) == 'table' ) and reply.id then |
65
2cefbe9f3ac6
shortenurl: cosmetical change
Mikael Berthe <mikael@lilotux.net>
parents:
64
diff
changeset
|
53 |
return ('Full: %s\nShort: %s'):format(url, reply.id) |
64
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 |
else |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
55 |
local errmsg |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 |
if type ( reply ) == 'table' and ( type ( reply.error ) == 'table' ) then |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
errmsg = ('Failed to shorten url: %u %s'):format(reply.error.code, |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 |
reply.error.message) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 |
else |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 |
errmsg = ('Failed to shorten url: Unknown response:\n%s'):format(replystring) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 |
end |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
return nil, errmsg |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 |
end |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
return nil, "Got no data :-(" |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 |
end |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
end |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 |
mcbot_register_command("shortenurl", shortenurl) |
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 |
|
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
-- vim: se ts=4 sw=4: -- |