Contents |
This page is the main manual page for accessing the old IMSLP API. See IMSLP:API for a description of the new IMSLP API.
IMSLP offers a simple Ruby script to access this API. Anyone interested in obtaining the script should e-mail User:Feldmahler with a description of the purpose for the request.
IMSLP has a separate API to query information not contained on work pages. The entry page is Special:IMSLPAPI.
General Usage Syntax: http://imslp.org/Special:IMSLPAPI/<return format>/<function>/<first double url-encoded arg>/<second double url-encoded arg>/<and so on>...
General Return Format: All return values are serialized using the PHP serialize() function. A string with the error is returned on failure, a non-string with the result on success.
function query_imslp_api( $function, $args = array() )
{
return unserialize( file_get_contents( "http://imslp.org/wiki/Special:IMSLPAPI/{$function}/" . implode( '/', array_map( 'rawurlencode',
$args ) ) ) );
}
unserialize is the main function; do_uns is a helper function and should not be called directly.
def unserialize( string )
string = StringIO.new( string )
def string.read_until( char )
val = ''
while (c = self.read( 1 )) != char
val << c
end
val
end
do_uns( string )
end
def do_uns( string )
case (type = string.read(2)[0,1])
when 'a' # associative array, a:length:{[index][value]...}
val = {}
string.read_until( '{' ).to_i.times { |i|
key = do_uns( string ); val[key] = do_uns( string ) }
string.read( 1 ) # skip the ending }
when 's' # string, s:length:"data";
len = string.read_until( ':' ).to_i + 3 # quotes, separator
val = string.read( len )[1...-2] # read it, kill useless quotes
when 'i' # integer, i:123
val = string.read_until( ';' ).to_i
when 'd' # double (float), d:1.23
val = string.read_until( ';' ).to_f
when 'N' # NULL, N;
val = nil
when 'b' # bool, b:0 or 1
val = ( string.read(2)[0] == ?1 ? true : false )
else
raise TypeError, "Unable to unserialize type '#{type}'"
end
val
end