Champlain

 view release on metacpan or  search on metacpan

examples/flickr.pl  view on Meta::CPAN


	my $photo = pop @{ $data->{photos} };
	$data->{photo} = $photo;

	my $args = {
		photo_id => $photo->{id},
	};
	$soup->do_flickr_request(
		'flickr.photos.getSizes' => $args,
		\&flickr_photos_getSizes_callback, $data,
	);

	return TRUE;
}


#
# This callback gets called each time that flikr answers to a 'size' query. Here
# the square size is the only one that gets inspected all other sizes are
# silently ignored.
#
# This function will trigger the download of the square image.
#
sub flickr_photos_getSizes_callback {
	my ($soup, $xml, $headers, $data) = @_;
	my $parser = XML::LibXML->new();
	my $doc = $parser->parse_string($xml);

	# Display only the thumbnails ("Square" images)
	my ($node) = $doc->findnodes('/rsp/sizes/size[@label = "Square"]');
	if ($node) {
		my $uri = $node->getAttribute('source');

		# The image download is made from a different server than the RPC calls
		$data->{soup}->do_get(
			$uri,
			\&flickr_download_photo_callback,
			$data->{photo}{marker},
		);
	}

	# Go on to the next photo
	flickr_photos_getSizes($soup, $data);
}


#
# This callback gets called each time that a flikr image is downloaded. Once a
# picture is successfully downloaded it will replace the one in the current
# marker.
#
sub flickr_download_photo_callback {
	my ($soup, $content, $headers, $marker) = @_;

	if ($headers->{Status} !~ /^2\d\d/) {
		warn "$headers->{Status} $headers->{Reason}";
		return;
	}

	# Load the image with a Pixbuf Loader
	my ($mime) = split(/\s*;/, $headers->{'content-type'}, 1);

	my $loader = Gtk2::Gdk::PixbufLoader->new_with_mime_type($mime);
	$loader->write($content);
	$loader->close;
	my $pixbuf = $loader->get_pixbuf;

	# Transform the Pixbuf into a Clutter::Texture
	my $texture = Clutter::Texture->new();
	$texture->set_from_rgb_data(
		$pixbuf->get_pixels,
		$pixbuf->get_has_alpha,
		$pixbuf->get_width,
		$pixbuf->get_height,
		$pixbuf->get_rowstride,
		($pixbuf->get_has_alpha ? 4 : 3),
		[]
	);

	# Add a marker for the image
	$marker->set_image($texture);
}


#
# Creates a label that shows what's going on.
#
sub make_label {

	my $black = Clutter::Color->new(0x00, 0x00, 0x00, 0xff);
	my $button_text = Clutter::Text->new("Sans 16", '', $black);
	$button_text->set_position(10, 10);

	return $button_text;
}




#
# A very simple implementation of an asynchronous HTTP client that integrates
# with Glib's main loop.
#
# Usage:
#
#   my $soup = My::Soup::Flickr->new($key); # The key is for web service calls
#   $soup->do_flickr_request(
#     'flickr.photos.getSizes' => {photo_id => $id},
#     \&flickr_photos_getSizes_callback, $data,
#   });
#
package My::Soup::Flickr;

use Glib qw(TRUE FALSE);
use AnyEvent::HTTP;
use URI;


sub new {
	my $class = shift;
	my ($key) = @_;



( run in 0.911 second using v1.01-cache-2.11-cpan-524268b4103 )