mcbot/cmds/shortenurl.lua
author Mikael Berthe <mikael@lilotux.net>
Sun, 25 Nov 2012 19:01:31 +0100
changeset 64 62248d5fbb24
child 65 2cefbe9f3ac6
permissions -rw-r--r--
Add shortenurl module, based on isbear's code This code is derivated from the lua example script shortenurl.lua in isbear's lua module for mcabber.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
-- This module is derivated from isbear's shortenurl function:
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
-- http://hg.lilotux.net/mod-mcabber-lua/file/tip/examples/shortenurl.lua
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
--
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
--  * Copyright (C) 2012      Myhailo Danylenko <isbear@ukrpost.net>
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
--  * Copyright (C) 2012      Mikael Berthe <mikael@lilotux.net>
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
--
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
require "libs.shcmd"
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
local json = require 'json'
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
local shortenurl = { ["desc"] = "URL shortener",
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
                   }
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
local encodeoptions = {
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
    strings = {
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
        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
    19
    }
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
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
local shell_escape = function (str)
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
    if str then
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
        return "'" .. str:gsub("'", "'\\''") .. "'"
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
    else
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
        return "''"
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
    end
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
end
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 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
    31
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
function shortenurl.cmd (url)
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
    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
    35
        return nil,  'You must specify an URL'
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 replystring = ''
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
    local targeturl = json.encode( { longUrl = url }, encodeoptions )
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
    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
    42
    if data then
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
        local reply = json.decode ( data )
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
        if ( type ( reply ) == 'table' ) and reply.id then
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
            return ('Full:  %s\nShort: %s'):format(url, reply.id)
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
        else
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
            local errmsg
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
            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
    49
                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
    50
                          reply.error.message)
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
            else
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
                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
    53
            end
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
            return nil, errmsg
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
        end
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
        return nil, "Got no data :-("
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
    end
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
end
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
mcbot_register_command("shortenurl", shortenurl)
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
-- vim: se ts=4 sw=4: --