NAME

WebService::Freesound - Perl wrapper around Freesound OAuth2 API!

VERSION

Version 0.02

SYNOPSIS

#!/usr/bin/perl
use WebService::Freesound;

my %args = (
   client_id     => '03bbed9a541baf763526',
   client_secret => 'abcde1234598765fedcba78629091899aef32101',
   session_file  => '/var/www/myapp/freesoundrc',
);

my $freesound = WebService::Freesound->new (%args);

# Freesound 3-Step OAuth process:
#
# Step 1.
# Get authorisation URL
#
my $authorization_url = $freesound->get_authorization_url();

# Step 2.
# Redirect the user to authorization url, or paste into browser.
# Or pop up an <iframe> with this URL as src.
#
use CGI;
my $q = new CGI;
$q->redirect($authorization_url);

# OR in Toolkit::Template : Will put the Freesound.org's authentication
# window on your site, with Approve or Deny buttons.
#
<iframe src=[% authorization_url %]>No iframe support</iframe>

# Step 3.
# A 'code' will be made available from the authorization_url from Freesound.
# Use it here to get access_token, refresh_token and expiry times. These are
# stored internally to the object and on disk. In theory, you should not need
# to see them, but are accessible via their respective accessors.
#
my $rc = $freesound->get_new_oauth_tokens ($code));
if ($rc) {
   print $freesound->error;
}

# At any time you can call check_authority to see if you are still authorised,
# and this will return 1 or undef/$freesound->error.  It will refresh the tokens
# if refresh_if_expired is set.
#
my $rc = $freesound->check_authority (refresh_if_expired => 1/undef);
if ($rc) {
   print $freesound->error;
}

# All done with OAuth2 now it ..should.. just work forever (or until you revoke
# the authorization at L<https://www.freesound.org/home/app_permissions/>, when
# logged in, or set refresh_if_expired to undef).
#
# Get Freesound data, see L<https://www.freesound.org/docs/api/resources_apiv2.html>
# Returns a L<HTTP::Response> or undef and $freesound->error.
#
my $rc = $freesound->check_authority (refresh_if_expired => 1);
if ($rc) {
   my $response = $freesound->query ("query..." or "filter..." etc) # no query question mark required.
}

# Download the sample from a Freesound id into the specified directory with a
# progress update in counter_file - for web apps get javascript to fire an Ajax call
# to read this file until 100% complete.  Returns the path to the sample or undef
# and $freesound->error.
#
my $rc = $freesound->check_authority (refresh_if_expired => 1);
if ($rc) {
   my $file = $freesound->download ($id, '/var/www/myapp/downloads/', $counter_file);
}

DESCRIPTION

This module provides a Perl wrapper around the https://Freesound.org RESTful API.

Freesound is a collaborative database of Creative Commons Licensed sounds. It allows you to browse, download and share sounds. This Perl wrapper at present allows you 'read-only' access to Freesound, ie browse and download. Upcoming versions could provide upload, describe and edit your own sounds (though I expect it might just be easier to use their website for this).

The complete Freesound API is documented at https://www.freesound.org/docs/api/index.html

In order to use this Perl module you will need get an account at Freesound (https://www.freesound.org/home/register/) and then to register your application with them at https://www.freesound.org/apiv2/apply. Your application will then be given a client ID and a client secret which you will need to use to get OAuth2 authorisation.

The OAuth2 Dance is described at Freesound, https://www.freesound.org/docs/api/authentication.html and officially at RFC6749. It is a three step process as suggested above.

This module should look after the authorisation once done, ie when the expiry time arrives it can automatically refresh the tokens. The auth tokens are therefore kept as a file specified by "session_file", which should be read-only by you/www-data only.

When downloading a sound sample from Freesound a progress meter is available in "counter_file" which is useful in web contexts as a progress bar. Format of the file is :

<bytes-written>:<byes-total>:<percentage> # for example "10943051:12578220:87", ie 87% of 12578220 bytes written.

This is optional.

Also the download will download the sample file as its name and type suffix (some Freesound names have suffixes, some don't), so something like "/var/www/myapp/downloads/Pretty tune on piano.wav", ".../violin tremolo G5.aif" etc.

The query method allows you to put any text string into its parameter so that you have the full capabilities of Freesound search, filter, sort etc, as described here : https://www.freesound.org/docs/api/resources_apiv2.html

If used as part of a web app, then the process could be :

METHODS

INTERNAL SUBROUTINES/METHODS

Please don't use these as they may change on a whim.

AUTHOR

Andy Cragg, <andyc at caesuramedia.org>

BUGS

This is beta code and may contain bugs - please feel free to fix them and send patches.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc WebService::Freesound

You can also look for information at:

ACKNOWLEDGEMENTS

I had a look at WebService::Soundlcoud by Mohan Prasad Gutta, http://search.cpan.org/~mpgutta/ for some ideas.

LICENSE AND COPYRIGHT

Copyright 2016 Andy Cragg.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.