Archive

Posts Tagged ‘statistics’

Using Flickr API to get the views, faves and comments of your most popular images

September 25, 2009 Leave a comment

One of the first things I wanted to do with the Flickr API was to get some stats on my most popular images.

You can get this info through the web front end, but there’s no option to download the stats in delimited format (such as CSV) so it can be analysed in a spreadsheet.

I wanted to work out if there was a pattern emerging in the key stats for my 200 most popular images…

  1. Number of Views
  2. Number of Favourites
  3. Number of Groups posted to
  4. Number of sets an image is in

Using a Python script (v2.5) and Beej’s FlickR API, this is fairly straightforward. It doesn’t require authentication.

The script runs slowly as it ‘plays nice’, leaving a seconds pause between calls, courtesy of the time.sleep() function. I don’t want to thrash the server.

# -*- coding: UTF8 -*-

import flickrapi
import datetime
import time
import string

# enter your api key below
api_key = 'PUT_YOUR_API_KEY_HERE' 

# enter the user id below (you can use flickr.people.findByUsername to get this for any user)
# it'll look something like 99999999@N99
userid='USER_ID_TO_SEARCH'

# delimiter. Use comma if you want, I tend to use ~
DELIMITER="~"

# dump number of views in delimited format

if __name__ == '__main__':
    #output format : "photoid,title,views,faves,groups,sets"
    flickr = flickrapi.FlickrAPI(api_key)
    photos = flickr.photos_Search(user_id= userid,extras='views', per_page='200', page='1', sort='interestingness-desc')
    for photo in photos.find('photos'):

        title = string.replace(photo.get('title'),",","") #in case you want to use comment as a delimiter ;0)
        
        # number of views
        id = photo.get('id')
        views=photo.get('views')
        
        # fave count (up to 50)
        faves = flickr.photos_getFavorites(photo_id=id,per_page=50)
        countfaves=faves.find('photo').get('total')
        time.sleep(1)
        
        # pools and sets posted to
        contexts=flickr.photos_getAllContexts(photo_id=id)
        posted_groups=len(contexts.findall('.//pool'))
        posted_sets=len(contexts.findall('.//set'))
        time.sleep(1)
        
        # comments
        comments=flickr.photos_comments_getList(photo_id=id)
        countcomments=len(comments.findall('.//comment'))
        
        # output as delimited text
        tokens=(id,title,str(views),str(countcomments),str(countfaves),str(posted_groups),str(posted_sets))
        converted=DELIMITER.join(tokens)
        
        print converted

This script dumps to the console, rather than a file; but it’s easily modified to write to a file. It should work with comma as a separator (for CSV use) as the title tag is stripped of commas…

Here’s some sample output from my photostream…

The format is :
photo id~title~views~favourites~groups~sets


2113237108~north-berwick-old-pier~259~25~21~6~3
3598511429~paris photo heatmap~736~6~12~5~3
3609118442~heart texture~861~10~26~0~2
2304836447~persistence de-motivator~4964~1~4~1~1
2347673075~bergen-ole-bull-plass-lensbaby~184~12~7~6~4
1621047086~banners-down-princes-street~325~11~8~4~2
3688253826~St Anthony's Chapel Edinburgh~124~15~15~9~1
2717978614~st marys~88~9~7~3~2

Once you have the output saved to a text file, you can import it into a spreadsheet (like OpenOffice or Excel) and play around with the figures 🙂

Advertisements