Using the Flickr API from Perl
Good afternoon, dear% habr_username%!
If anyone does not know, we are talking about one of the most popular photo hosting sites in the world - Flickr ( wiki about Flickr ).
Briefly about the Flickr API, because on the hub there are only brief references to it.
Flickr developers give everyone the opportunity to use a powerful API that provides access to all conceivable and inconceivable functions, data, capabilities. The Flickr API is well documented, there is a full description of all requests ( example ), there is a FAQ , there is a “quick start ”, these are the main sources of knowledge about the Flickr API, they are quite enough to start developing your application. Modules to facilitate the use of the Flickr API are available for most popular programming languages, including Objective-C and Java , which are currently widely used in development for IOS and Android, a list with links can be found here (Scrolling down the page).
Application subject (as an example)
A little about one of the most popular Flickr ideas - there is an algorithm for selecting the most interesting photos of the day, which every day forms a list of 500 most interesting (according to the algorithm) photos, which every user wants to go to. And this is perhaps one of the most popular (but at the same time easy to implement) topics for writing applications using the Flickr API. In order to quickly complete this brief excursion to Flickr, because this is not the subject of this article, I will give a couple of links from which it will immediately become clear what is being discussed. The most popular photos for every day of the month (the page is directly in Flickr itself), now a couple of third-party web projects - Flickriver , flickrhivemind. One can only guess about the number of such projects, I’m also sure that similar applications are already available for Android and IOS, in general, the “scale of the tragedy” is global, as well as the possibilities for implementing and developing this idea.
Example usage from Perl
After you have done everything necessary to get started (registered on flickr, received a key, a secret hash code for your application, etc. ), you can begin. And so for the Perl language, there is also a module that calls the Flickr :: API . Well, let's try to use it.
#!/usr/bin/perl
use warnings;
use strict;
use Flickr::API;
my $flickr_api_key = ваш_flickr_api_key;
my $flickr_api_secret = ваш_flickr_api_secret;
my $api = new Flickr::API({ 'key' => $flickr_api_key,
'secret' => $flickr_api_secret});
my $resp = $api->execute_method('flickr.interestingness.getlist', {
'per_page' => 100
})
my $cont = $$resp{_content};
Here we use the API method flickr.interestingness.getList to get a list of popular photos, as you can see from the description - you can specify the day for which we want to get a list, the number of photos in the issue and additional parameters.
Now $ cont contains an xml response, of the kind specified in the API method description . Let me give you a piece (today's output 20110809) - and so if we do print $ cont, we get:
...........................
...........................
...........................
And so, what's the point or about forming links to Flickr images - each field is how you understood the description of each photo, about links to official images . the documentation . Or in short - the link to any Flickr image has the form -
http ://farm"farm из выдачи".static.flickr.com/"server из выдачи"/"photo id из выдачи"_"secret из выдачи".jpg
. That is, for example, for the first photo from our issue, we get https://habrastorage.org/getpro/habr/post_images/055/f43/5b2/055f435b217e402bb660663b7c6c1463.png . Do not be shy - click, check.
That is, now you just need to “parse” this issue, everyone does it as he likes, you can’t list XML parsers for a pearl, but you can parse directly at your own risk, because the structure of the issue may change in the future.
Now you have everything to display the best photos on Flickr for a certain date, and where to put it is up to you to decide whether you want on your website, want it in a widget for your desktop, want it in applications for mobile devices, etc.
Well, to finish, a simple example for generating an html page. I’ll give you a piece of code that parses the XML response from the Flickr API on the forehead and immediately prints the code in one cycle to form an html image page.
@cont_strings = split(/\n/,$cont);
foreach $string (@cont_strings) {
next if $string !~ /photo/;
next if $string =~ //;
$string =~ m:photo\sid="(.*)"\sowner="(.*)"\ssecret="(.*)"\sserver="(.*)"\sfarm="(.*)"\stitle="(.*)"\sispublic:;
$photo_id = $1;
$owner = $2;
$secret = $3;
$server = $4;
$farm = $5;
$title = $6;
print "
The code is working, I actually write and check in real time, but I don’t jerk from somewhere, so if I collect and paste all the above into a cgi script, then the page will display the 100 best images of the day, from all Flickr. Also, when you click on an image, you will go to the page of this image in the author’s album.
For example, I’ll insert here one photo of how it will look on your page (with a link to the author’s page), again the first of our issue. Yes it will be stupid 100 photos on the whole page, yes it is very sad, but everything that is written above is written solely for acquaintance. The main thing is that you have the necessary data array and if you add a little CSS and javascript, it will turn out no worse than this .

In this article I wanted to introduce you to the Flickr API and show the ease of its use, having 30 lines of code, we got a simple application. I don’t think that using the Flickr API from other programming languages is much more complicated than shown above with Perl. So if you have any ideas, it will not be difficult to develop applications using the Flickr API for any platform and any tasks. I regret that I had to write a lot of material not related to the title of the topic, but necessary for its understanding, I hope someone draws something new from the above.
Z.Y. I was surprised to find that the Flickr blog does not exist on the hub; if there was, then the answer to the question of where to publish would be unambiguous. And either in Yahoo or in Perl, well, for the first one it’s not at all suitable, for the second, of course, it’s not very good either (because for the article on Perl it’s not very suitable because of the large amount of information about the Flickr and Flickr API) to such an extent.