Friday, January 23, 2015

Perl and Flickr Stats and Google Docs - Part 2

In this part of Perl and Flickr Stats and Google Docs, I'll be covering  the code at the end of  part 1 of Perl and Flickr Stats and Google Docs.

Update 2015-01-24: It should be noted the date that Flickr uses is based on UTC time. So the new Flickr day in the State of Washington is either 4pm or 5pm depending whether you are in Daylight Savings or Standard time.  Dealing with this situation is fairly easy, set the program to run at 5:05pm and use the date for your local time zone (this works pretty well for time zones west of UTC) and it will still be the date of the day your want statistics.  It might be better to use UTC time which is not impacted by Daylight Savings time and can work regardless of time zone.

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}\/";

Lines 1-6: Setup the use of Perl libraries and features.  I had to download the Flickr::API2 and Net::Google::Spreadsheets libraries from CPAN.

Lines 8-9: Grabs the date from localtime formatted in my preferred format of YYYY-MM-DD.

Lines 11-13: Connect and authenticate to Flickr using your API Key and Secret Key.

Lines 15-17: Use the Flickr::API2 method execute-method flickr.stats.getotalviews using the date and your Auth Token from part one of this series.

Line 19: I used to find the format of the hash and what the values looked like so I could print them out in lines 21-26.

Lines 21-26: Prints out the data I want to load into my Flickr Stats spreadsheet on Google Drive.

Lines 28-30: Use the Flickr::API2 method execute-method flickr.stats.getpopularphotos using the current date and the Auth Token again. What I want to get from this method is the first photo of the photos viewed that day.

Lines 34-37: Prints out the data I want to load as well into the spreadsheet.


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/


Next I'll work on connecting to Google Docs and authenticating and then adding rows to the spreadsheet. These will be coming in the next two weeks in parts 3 and 4 of the series.

Your thoughts and comments appreciated.