Currently I’m working on an update for Christiaan’s Collection. Some time ago I started to take photos for the website and uploading them under Creative Commons license to the Flickr platform so other people can use them for free.
During the last couple of months the photos are used on websites, in presentations and as banners. The amount of views are rising on the Flickr platform. If you have a Flickr pro account you can track the views easily, but in this case I just created a shell script myself that tracks the amount of views for me. The script can be found on the wiki.
The shell script is a basic script that uses the Flickr API to retrieve the views of all the photos from your account. It does this by a simple web request to the Flickr API using the ‘curl’ program. It uses your Flickr API key that you can generate via the website and your user ID to get all the ID’s (for example ‘27635346150’) of your photos. The code below is an example of this request.
ids=$(curl -s "https://api.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key="$KEY"&user_id="$user"&per_page=500&format=rest" | grep -o -P '(?<=photo id=").*(?=" owner=)')
This script retrieves a maximum of 500 photo ID’s, if there are more then 500 photos it will take the 500 latest photos. It’s easy to add a couple of lines of code to retrieve more later on.
After getting all the photo ID’s they are read into an array, a group of values into one variable. I’m calling this variable the ‘idsarray’.
The ‘idsarray’ looks something like this: [position 0] 27610919906 [position 1] 27610920016 [position 2] 27610920076 and so on.
This makes it easy to iterate through all the values. The code below is an example of reading the output from the previous step into the array.
read -r -a idsarray <<< $ids
The measure of who we are is what we do with what we have – Vince Lombardi
The next part of the script is the for loop which iterates through all the photo ID’s in the ‘idsarray’. I’m using the variable ‘${idsarray[@]}’ to iterate through the whole array. By using the ‘@’ the for loop iterates through the whole array instead of one entry. Per photo ID the loop sends a request to the Flickr API for all the photo information. From this output the script only takes the title of the photo and the views.
The script reads the values of the text file from yesterday into the variable ‘photoviewsyesterday’ to determine the views from the day before. The code below is an example:
photoviewsyesterday=$(cat $path$filedateyesterday | grep "$id" | cut -d '|' -f 4)
The ‘$path’ and ‘$filedateyesterday’ are values that are already determined in the script. The ‘$id’ variable contains the current photo ID from the for loop. I’m using the ‘|’ sign to split values when I write output to a text file, the ‘cut’ command takes this as delimiter and takes field number 4 because that’s the position I’m storing the views in a text file.
Afterwards it determines the difference between the views from today and yesterday and stores it in the variable ‘photoviewsdifference’.
photoviewsdifference=`expr $views - $photoviewsyesterday`
The ‘expr’ command is used to create an expression in other words, makes it possible to do mathematics.
Today’s date, the photo ID, the photo title, the current views and difference between yesterdays views and today’s views are written to a text file. The current views are also added to the variable ‘totalviews’ to keep track of the total amount of views for today.
After the for loop iterates through all the photo ID’s, the script calculates the difference between the total amount of views from yesterday and today. The script writes today’s date, total amount of views and the calculated difference between the total amount of views to another text file.
This basic functionality of the script is enough for me for now. In the future it’s quite easy to put all the data in a MySQL database and use for example Highcharts to create your own charts. Also adding a couple of lines to create an archive directory or error messages if something fails is for now not necessary.
Music videos of the day