Saturday, January 17, 2015

Perl and Flickr Stats and Google Docs

I have a Google Doc spreadsheet that I use to track my Flickr stats every day.  The Flickr stats only stay online for 30 days so I have wanted to what my stats look like over a longer period of time.  So I figured that it would be nice to automate getting the statistics from Flickr.  Sample of the types of statistics I capture below.



I decided on  using Perl after I found it had both a Flickr (Flickr-API2) and Google Spreadsheet (Net-Google-Spreadsheets) module. I am assuming you know how to download and install Perl modules.

Both Flickr and Google Docs require authentication to access and I found that to be the toughest part to configure.  Also, for Flickr you will need to get an API key as well as work through the authentication process. For the Flickr authentication process I used the article Creative Commons, Flickr and presentations: A bit of tinkering | The Weblog of (a) David Jones with some modifications.

Once you have your API Key and Secret Key from Flickr you need to get a "frob" so can use all the features of Flickr-API2.

Here is the code I used to get the "frob" and grant permissions to the app.

flickr-auth.pl
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
use feature qw(say switch);
use Data::Dumper;
use Flickr::API2;

my $api = new Flickr::API2({'key'    => '<<your FLICKR API KEY HERE>>',
                            'secret' => '<<your FLICKR SECRET KEY HERE>>'});

# Get the Frob
my $result = $api->execute_method( 'flickr.auth.getFrob' );
my $frob = $result->{frob}->{_content};

# Get a special URL to tell Flickr to authorize the script
my $url = $api->raw->request_auth_url( 'delete', $frob );
print Dumper( $url );
# wait until I visit the URL and hit enter (change URL to https)
<stdin>;


 my $res = $api->execute_method( 'flickr.auth.getToken', { 'frob' => $frob} );
 print Dumper( $res )

When you run this from the command line, you will get something like the following:


[flickr]$ perl flickr-auth.pl
$VAR1 = bless( do{\(my $o = 'http://api.flickr.com/services/auth/?api_sig=<<your-flickr-api-sig>>&frob=<<flick-your-fob>>0&perms=delete&api_key=<<you-flick-key>>')}, 'URI::http' );
Note The program stops here waiting for a response.  Do not hit return here until you have gone through the next couple of steps.

Copy out the http line and place this in your browser, before hitting return in your webpage, change the http to https. You should get a page that looks like this, if you wish to continue, press the "OK, I'll Authorize it" button.


And you should get the response below:


At this point go back to your Perl session and hit return.

$VAR1 = {
          'auth' => {
                      'user' => {
                                  'fullname' => '<<Your Flickr Name>>',
                                  'nsid' => '<<Flicr User ID>>',
                                  'username' => '<<Flickr User Name>>'
                                },
                      'perms' => {
                                   '_content' => 'delete'
                                 },
                      'token' => {
                                   '_content' => '<<Flickr Auth Token>>'
                                 }
                    },
          'stat' => 'ok'
        };

You will need the Flickr Auith Token for your Perl program.  Remember to keep the Auth Token and you Secret Key secure.

Below is a sample of using the API once you have gotten past the authentication process.  I'll cover this in the next post.

flickr-stats.pl
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use feature qw(say switch);
use Data::Dumper;
use POSIX qw(strftime);
use Flickr::API2;
use Flickr::API2::Request;
use Net::Google::Spreadsheets;

my $date = strftime "%Y-%m-%d", localtime;
print $date . "\n";

  my $api = new Flickr::API2({'key'    => '<<Flickr API Key>>',
                             'secret' => '<<Flickr Secret Key>>',
                             'perms' => 'read'});

  my $gtv = $api->execute_method("flickr.stats.gettotalviews",
         {date=>$date,
          auth_token=>'<<Flickr Auth Token>>'}); 

#print Dumper($gtv);

say "Total views is " . $gtv->{stats}->{total}->{views};
say "Photostream views is " . $gtv->{stats}->{photostream}->{views};
say "Photos views is " . $gtv->{stats}->{photos}->{views};
say "Sets views is " . $gtv->{stats}->{sets}->{views};
say "Collections views is " . $gtv->{stats}->{collections}->{views};
say "Galleries views is " . $gtv->{stats}->{galleries}->{views};

 my $gpp = $api->execute_method("flickr.stats.getpopularphotos",
          {date=>$date,
           auth_token=>'<<Flickr Auth Token>>'});
 
# print Dumper($gpp);
 
say "Top Photo id is " . $gpp->{photos}->{photo}[0]->{id};
say "Top Photo view is " . $gpp->{photos}->{photo}[0]->{stats}->{views};
say "Top Photo Title is " . $gpp->{photos}->{photo}[0]->{title};
say "https:\/\/www.flickr.com\/photos\/mikebates\/$gpp->{photos}->{photo}[0]->{id}\/";

Output:
[flickr]$ perl flickr-stats.pl
2015-01-17
Total views is 243
Photostream views is 22
Photos views is 219
Sets views is 2
Collections views is 0
Galleries views is 0
Top Photo id is 15635946094
Top Photo view is 4
Top Photo Title is 20150111-1338-00028-Island Drive Walkabout-2
https://www.flickr.com/photos/mikebates/15635946094/