cURL problems

Ok, so my lyrics grabber script is a little messed. This line:

set theURL to "http://www.lyricwiki.org/" & theArtist & ":" & theSong
return theURL

for example returns

If I paste that URL into my browser it goes to the correct page.

However when I try to cURL it like so:

set theURL to "http://www.lyricwiki.org/" & theArtist & ":" & theSong
get theURL
do shell script "/usr/bin/curl --user-agent '' " & quoted form of result
set theLyrics to result

I get the html to an error page. Like this

Just wondering if anyone knows whats up with that?

Your browser automatically redirects, but curl does not.

See also: curl man page

Alternatively:

do shell script "/usr/bin/curl 'http://lyricwiki.org/api.php?func=getSong&artist=As_I_Lay_Dying&song=The_Sound_of_Truth&fmt=text'"

wow, thats amazing, now i dont even need to parse the html. Btw how did you figure that out? That shell script?

Hi, I’ve writen a small script that returns me the lyrics for the playing song in iTunes and I’ve used the call SOAP method! But I’ve some problems with accentuated characters!
Now I’ve found this great solution using cURL! But there’s a problem, If in the song name or the artist there’s the quote ( ’ ) char the script get this error:

how I can solve it?

The script is:

tell application "iTunes"
	set song to name of current track
	set art to artist of current track
end tell


do shell script "curl 'http://lyricwiki.org/api.php?func=getSong&artist=" & simpleString(art) & "&song=" & simpleString(song) & "&fmt=text'"

on simpleString(theString)
	set n to length of theString
	set stringOut to ""
	
	repeat with i from 1 to n
		if (character i of theString is " ") then
			set stringOut to stringOut & "_"
		else
			set stringOut to stringOut & (character i of theString)
		end if
	end repeat
	
	return stringOut
end simpleString

Thanks in advance!

Hi,

use the quoted form of syntax, it handles the quotation


do shell script "curl " & quoted form of ("http://lyricwiki.org/api.php?func=getSong&artist=" & simpleString(art) & "&song=" & simpleString(song) & "&fmt=text")

the handler can be simplified a bit


on simpleString(theString)
	return do shell script "echo " & quoted form of theString & " | tr ' ' _"
end simpleString

Very thanks StefanK! :slight_smile:
I’ve not thought to use the “quoted form” method!

I’m trying to add a timeout to the cURL comand to terminate the request if the server is busy! I’ve tried this:


do shell script "curl -m/--max-time 10" & quoted form of ("http://lyricwiki.org/api.php?func=getSong&artist=" & theArtist & "&song=" & theSong & "&fmt=text")

but it doesn’t work! It asks me a proper numerical parameter! what it means ? I’ve typed 10 !!!

the max-time parameter is either
-m
or
–max-time

and after the number must be a space character

Oops! I’m sorry! I’m a very beginer with one ‘n’ !!! :frowning:

Thanks!!!:slight_smile:

A question!? When cURL timeout, the do shell script what returns?
In other words, how can I know if cURL has terminated with a timeout?

it throws an error


try
	set theLyrics to (do shell script "curl -m 10 " & quoted form of ("http://lyricwiki.org/api.php?func=getSong&artist=" & theArtist & "&song=" & theSong & "&fmt=text"))
	if theLyrics is "Not found" then
		-- do something for "not found"
	end if
on error e
	if e contains "curl: (28)" then display dialog last paragraph of e -- timeout error
end try

I’m not sure how you’d tell if it timed out. I couldn’t get it to time out, even with a -m of 1. I imagine that you would get a shorter result. So you could use something like (on the below): count characters of lyrics
That could be used to detect “not found” also.

I think it would also be a good idea to escape spaces or other characters that would make it not work. The easiest way is to let someone else do that. I use the free TextCommands scripting addition, which is available in that section of this site.


set theArtist to my makeURL("Sandy Denny")
set theSong to my makeURL("Farewell, Farewell")

set req to quoted form of ("http://lyricwiki.org/api.php?func=getSong&artist=" & theArtist & "&song=" & theSong & "&fmt=text")

set lyrics to do shell script "curl -m 10 " & req

on makeURL(txt)
	tell application "TextCommands"
		set theURL to encode URL txt
	end tell
	return theURL
end makeURL

Thanks!!!:wink: