examples/shortenurl.lua
changeset 141 1e36a08d7734
parent 136 2b04fad2f61a
equal deleted inserted replaced
140:89841bd3db8c 141:1e36a08d7734
     5 --  * goo.gl (online service)
     5 --  * goo.gl (online service)
     6 --  * curl (program)
     6 --  * curl (program)
     7 --  * lua-json (https://github.com/harningt/luajson/)
     7 --  * lua-json (https://github.com/harningt/luajson/)
     8 --  * shell_escape (mcabberrc.lua)
     8 --  * shell_escape (mcabberrc.lua)
     9 --  * option lua_shorten_post_url
     9 --  * option lua_shorten_post_url
       
    10 --  * switches -s or -n
    10 -- TODO:
    11 -- TODO:
    11 --  * other backends
    12 --  * other backends
    12 --  * detect curl/wget
    13 --  * detect curl/wget
    13 --  * ensure curl will not mess screen up
    14 --  * ensure curl will not mess screen up
    14 --  * -s switch to send url automatically
       
    15 
    15 
    16 local json = require 'json'
    16 local json = require 'json'
    17 
    17 
    18 local encodeoptions = {
    18 local encodeoptions = {
    19 	strings = {
    19 	strings = {
    24 local curlcommand = "curl -s https://www.googleapis.com/urlshortener/v1/url%s -H 'Content-type: application/json' -d %s"
    24 local curlcommand = "curl -s https://www.googleapis.com/urlshortener/v1/url%s -H 'Content-type: application/json' -d %s"
    25 
    25 
    26 main.command ( 'shorten',
    26 main.command ( 'shorten',
    27 	function ( args )
    27 	function ( args )
    28 
    28 
    29 		if ( not args ) or not args:match ( '%w' ) then
    29 		local url  = args[1]
       
    30 		local post = main.yesno ( main.option ( 'lua_shorten_post_url' ) )
       
    31 		if not url then -- ugly. need to redesign argument parsing
       
    32 			if args.s then
       
    33 				url  = args.s
       
    34 				post = true
       
    35 			elseif args.n then
       
    36 				url  = args.n
       
    37 				post = false
       
    38 			end
       
    39 		end
       
    40 
       
    41 		if ( not url ) or ( not url:match ( '%w' ) ) then
    30 			print ( 'shorten: You must specify url' )
    42 			print ( 'shorten: You must specify url' )
    31 			return
    43 			return
    32 		end
    44 		end
    33 
    45 
    34 		local jid         = main.current_buddy ()
    46 		local jid         = main.current_buddy ()
    39 			querystring   = '?key=' .. key
    51 			querystring   = '?key=' .. key
    40 		end
    52 		end
    41 
    53 
    42 		local replystring = ''
    54 		local replystring = ''
    43 		
    55 		
    44 		main.bgread ( ( curlcommand ):format ( querystring, shell_escape ( json.encode ( { longUrl = args }, encodeoptions ) ) ),
    56 		main.bgread ( ( curlcommand ):format ( querystring, shell_escape ( json.encode ( { longUrl = url }, encodeoptions ) ) ),
    45 			function ( data )
    57 			function ( data )
    46 				if data then
    58 				if data then
    47 					replystring = replystring .. data
    59 					replystring = replystring .. data
    48 					return true
    60 					return true
    49 				else -- eof
    61 				else -- eof
    50 					local reply = json.decode ( replystring )
    62 					local reply = json.decode ( replystring )
    51 					if ( type ( reply ) == 'table' ) and reply.id then
    63 					if ( type ( reply ) == 'table' ) and reply.id then
    52 						if jid and main.yesno ( main.option ( 'lua_shorten_post_url' ) ) then
    64 						if jid and post then
    53 							main.run ( ('say_to -q %s %s'):format ( jid, reply.id ) )
    65 							main.run ( ('say_to -q %s %s'):format ( jid, reply.id ) )
    54 						else
    66 						else
    55 							print ( ('Shortened url: %s'):format ( reply.id ) )
    67 							print ( ('Full:  %s\n           Short: %s'):format ( url, reply.id ) )
    56 						end
    68 						end
    57 					else
    69 					else
    58 						if type ( reply ) == 'table' and ( type ( reply.error ) == 'table' ) then
    70 						if type ( reply ) == 'table' and ( type ( reply.error ) == 'table' ) then
    59 							print ( ('Failed to shorten url: %u %s'):format ( reply.error.code, reply.error.message ) )
    71 							print ( ('Failed to shorten url: %u %s'):format ( reply.error.code, reply.error.message ) )
    60 						else
    72 						else
    63 					end
    75 					end
    64 					return false
    76 					return false
    65 				end
    77 				end
    66 			end )
    78 			end )
    67 
    79 
    68 	end )
    80 	end, true )
    69 
    81 
    70 -- vim: se ts=4 sw=4: --
    82 -- vim: se ts=4 sw=4: --