examples/shortenurl.lua
changeset 127 9157566033e8
parent 126 5f0025da31e6
child 136 2b04fad2f61a
equal deleted inserted replaced
126:5f0025da31e6 127:9157566033e8
    19 	strings = {
    19 	strings = {
    20 		encodeSet = '\\"%z\1-\031', -- do not escape /, google does not understand that
    20 		encodeSet = '\\"%z\1-\031', -- do not escape /, google does not understand that
    21 	}
    21 	}
    22 }
    22 }
    23 
    23 
    24 local curlcommand = "curl -s https://www.googleapis.com/urlshortener/v1/url -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 		if ( not args ) or not args:match ( '%w' ) then
    30 			print ( 'shorten: You must specify url' )
    30 			print ( 'shorten: You must specify url' )
    31 			return
    31 			return
    32 		end
    32 		end
    33 
    33 
    34 		local jid = main.current_buddy ()
    34 		local jid         = main.current_buddy ()
       
    35 
       
    36 		local querystring = ''
       
    37 		local key         = main.option ( 'lua_shorten_googl_key' )
       
    38 		if key and key:match ( '%w' ) then
       
    39 			querystring   = '?key=' .. key
       
    40 		end
       
    41 
    35 		local replystring = ''
    42 		local replystring = ''
    36 		main.bgread ( ( curlcommand ):format ( shell_escape ( json.encode ( { longUrl = args }, encodeoptions ) ) ),
    43 		
       
    44 		main.bgread ( ( curlcommand ):format ( querystring, shell_escape ( json.encode ( { longUrl = args }, encodeoptions ) ) ),
    37 			function ( data )
    45 			function ( data )
    38 				if not data then
    46 				if data then
    39 					-- eof
    47 					replystring = replystring .. data
       
    48 					return true
       
    49 				else -- eof
    40 					local reply = json.decode ( replystring )
    50 					local reply = json.decode ( replystring )
    41 					if reply and reply.id then
    51 					if reply and reply.id then
    42 						if jid and main.yesno ( main.option ( 'lua_shorten_post_url' ) ) then
    52 						if jid and main.yesno ( main.option ( 'lua_shorten_post_url' ) ) then
    43 							main.run ( ('say_to -q %s %s'):format ( jid, reply.id ) )
    53 							main.run ( ('say_to -q %s %s'):format ( jid, reply.id ) )
    44 						else
    54 						else
    45 							print ( ('Shortened url: %s'):format ( reply.id ) )
    55 							print ( ('Shortened url: %s'):format ( reply.id ) )
    46 						end
    56 						end
    47 					else
    57 					else
    48 						-- XXX extract message from json?
    58 						if type ( reply.error ) == 'table' then
    49 						print ( ('Failed to shorten url: %s'):format ( replystring ) )
    59 							print ( ('Failed to shorten url: %u %s'):format ( reply.error.code, reply.error.message ) )
       
    60 						else
       
    61 							print ( ('Failed to shorten url: Unknown response:\n%s'):format ( replystring ) )
       
    62 						end
    50 					end
    63 					end
    51 					return false
    64 					return false
    52 				else
       
    53 					replystring = replystring .. data
       
    54 					return true
       
    55 				end
    65 				end
    56 			end )
    66 			end )
    57 
    67 
    58 	end )
    68 	end )
    59 
    69