📜 ⬆️ ⬇️

We use 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 at Habré there are only brief references to it.

Flickr developers enable all sufferers to use a powerful API that provides access to all imaginable and inconceivable functions, data, capabilities. The Flickr API is well documented, there is a complete 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 enough to start developing your application. Modules to facilitate the use of the Flickr API are for most popular programming languages, including Objective-C and Java , which are currently widely used in development for IOS and Android, the list with links can be found here (Scroll down the page).

Subject of writing the application (as an example)

A little about one of the most popular ideas of Flickr - there is an algorithm for selecting the most interesting photos of the day, which every day generates a list of 500 most interesting (according to the algorithm) photos where each user wants to go. And this is probably one of the most popular (but easy to implement) topics for writing applications using the Flickr API. In order to quickly complete this brief excursion into Flickr, because this is not the subject of this article, I will give a couple of links, of which you will immediately understand what is being said. The most popular photos for each 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 am also sure that such applications already exist for Android and IOS, in general, the “scale of the tragedy” is global, as is the possibility of implementing and developing this idea.
')
Perl usage example

After you have done everything you need 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 can be seen from the description - you can specify the day for which you want to get the list, the number of photos in the issue and additional parameters.
Now $ cont contains an xml response, of the type specified in the description of the API method . I will give here a piece (today's issue 20110809) - and so if we do print $ cont, we get:

 <?xml version="1.0" encoding="utf-8" ?> <rsp stat="ok"> <photos page="1" pages="5" perpage="100" total="500"> <photo id="6017677390" owner="16956431@N06" secret="45ceb165dc" server="6012" farm="7" title="" ispublic="1" isfriend="0" isfamily="0" /> <photo id="6018692165" owner="28911620@N00" secret="fa3550b6be" server="6144" farm="7" title="Tottenham riots" ispublic="1" isfriend="0" isfamily="0" /> <photo id="6016971301" owner="23450806@N04" secret="985e5ba585" server="6141" farm="7" title="." ispublic="1" isfriend="0" isfamily="0" /> ........................... ........................... ........................... <photo id="6017757609" owner="23548413@N00" secret="00f14219f3" server="6130" farm="7" title="harvest field" ispublic="1" isfriend="0" isfamily="0" /> <photo id="6016821183" owner="39952864@N07" secret="dbf3a64a3f" server="6028" farm="7" title="duisburg_060811" ispublic="1" isfriend="0" isfamily="0" /> </photos> </rsp> 


And so, what is there to what or about the formation of links to Flickr images - each field is how you understood the description of each photo, about links to official images . documentation Or in short - the link to any Flickr image looks like 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 hesitate - click, check.
That is, now you just need to “parse” this output, everyone does it as he likes, XML parsers for pearl are not listed, yes, you can parse directly into your head, at your own risk, because the output structure may change in the future.

Now you have everything to display the best photos on Flickr for a certain date, and it’s up to you to insert it where you want to add it to your website, to a desktop widget, to mobile applications, etc.

Well, to finish, a simple example for the formation of html page. Let me give you a piece of code that parses the XML response from the Flickr API head-on, right there in one loop prints the code to form an html page with images.
 @cont_strings = split(/\n/,$cont); foreach $string (@cont_strings) { next if $string !~ /photo/; next if $string =~ /<photos/; next if $string =~ /\/photos>/; $string =~ m:photo\sid="(.*)"\sowner="(.*)"\ssecret="(.*)"\sserver="(.*)"\sfarm="(.*)"\stitle="(.*)"\sispublic:; $photo_id = $1; $owner = $2; $secret = $3; $server = $4; $farm = $5; $title = $6; print "<div>"; print "<a target=blank_ href = http://www.flickr.com/photos/".$owner."/".$photo_id."><img title=\"$title\" src=http://farm".$farm.".static.flickr.com/".$server."/".$photo_id."_".$secret.".jpg></a>"; print "</div> 

The code is working, I actually write and check it in real time, and I don’t pull it from somewhere, so if all of the above is compiled and pasted into a cgi script, then the page will display the 100 best images per day, from the whole Flickr. Also, when you click on the image, you will go to the page of this image in the album of the author.
For example, I will 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.
image
Let it be stupid 100 photos on the whole page, but it is very sad, but everything that is written above is written solely for reference. 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 that .

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 the one shown above with Perl. So if there are 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 will get something new from the above.

ZY I was surprised to find that the Flickr blog on Habre does not exist, if it were, then the answer to the question where to publish would be unequivocal. And either in Yahoo or in Perl, well, for the first one, it’s not at all appropriate, for the second one, of course, it’s not very much either (because for an article on Perl, it’s not very suitable because of the large amount of information about Flickr and Flickr API), but not in such a degree.

Source: https://habr.com/ru/post/125961/


All Articles