Unsplash profile stats

I created a script that will show Views, Likes and Downloads of all images uploaded by a user in Unsplash. You’ve got the code below but will need a clientID from the Unsplash API (it’s free). All suggestions and improvements are welcomed.

Hope you like it!

const clientID = 'XXXXXXXXXXXXXXXXX'
const username = 'nkuutz'

let totalPhotosRequest = new Request('https://api.unsplash.com/users/'+username+'?client_id='+clientID);
let totalPhotos = await totalPhotosRequest.loadJSON();
totalPhotos = totalPhotos.total_photos;

let table = new UITable()

let nPages = Math.ceil(totalPhotos/10);
let appendedImages = '';

for(let n=1;n<nPages+1;n++){
    
    let req = new Request('https://api.unsplash.com/users/'+username+'/photos?client_id='+clientID+'&page='+n);
    let response = await req.loadJSON();
    
    let diff = totalPhotos - (n-1)*10;
    
    if(diff < 10){
        nPhotos = diff;
    } else {
        nPhotos = 10;
    }
    
    for(let i=0;i<nPhotos-1;i++){
      
      let row = new UITableRow()
      
      let photoID = response[i].id
       
      let photo = new Request('https://api.unsplash.com/photos/'+photoID+'?client_id='+clientID);
      let photoInfo = await photo.loadJSON();
    
      let nLikes = photoInfo.likes;
      let nViews = photoInfo.views;
      let nDownloads = photoInfo.downloads;
    
      let source = photoInfo.urls.thumb;
      
      let imageCell = row.addImageAtURL(source)
      let viewsCell = row.addText('👀 '+nViews.toLocaleString())
      let likesCell = row.addText('❤️ '+nLikes.toLocaleString())
      let downloadsCell = row.addText('⬇️ '+nDownloads.toLocaleString())          
      imageCell.widthWeight = 20
      viewsCell.widthWeight = 30
      likesCell.widthWeight = 25
      downloadsCell.widthWeight = 25
        
      row.height = 60
      row.cellSpacing = 10
        
      table.addRow(row)
    }
}

if (config.runsWithSiri) {
  Speech.speak("Here's your stats.")
}

QuickLook.present(table)
2 Likes

This is really great! I only uploaded one photo to Unsplash though. Maybe I should upload more stuff.

1 Like

It really comes in handy when you’ve got a bunch of photos to check the stats on. No need to use safari at all, just run the script and voila!

2 Likes