Advertisement
dumle29

Flickr image puller.

Mar 17th, 2014
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. V1.3.1  // Updated the script to accommodate flickrs new API change. This now uses SSL.
  3. V1.3    // I added the option for the user to specify the resolution the picture received should be. This uses
  4.     Flikrs URL suffixes, so to determine what suffix you want, go here: https://www.flickr.com/services/api/misc.urls.html
  5.     Then edit the size_suffix variable. As a note, the file type can be .jpg .png or .gif. It has to be the original format tho.
  6. V1.2    // I really should test my updates before posting them here... It works now. Yes I tested it :P
  7. V1.1    // Script no longer pops up with alerts boxes, and random, but not the same feature now works.
  8. V1.0    // Posted, worked sorta, but forgot to remove alert() statements, and in case of the same images were picked, it would miss an image.
  9.  
  10. Flickr image puller.
  11. This script pulls random images from a flickr account, and changes the src of any
  12. image tag, with the class flickr-img, into the src of the randomly picked image.
  13. The script also sets the alt tag to the name of the picture, to be HTML compliant.
  14. The script also won't allow the same image to be picked twice, unless there's
  15. no more pictures to pick from.
  16.  
  17. Made by Mikkel Jeppesen, use it as you please, but please leave credit :)
  18. */
  19.  
  20. /*
  21. The user ID can be found by going to the photostream of the user
  22. you want to pull the images from, and checking the url.
  23. It's your responsibility to have permission to use the images you use
  24. */
  25. var user_id = 'xxxxxxxxx@yxx';
  26.  
  27. //You can get an api key here: http://www.flickr.com/services/apps/create/apply
  28. var api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  29.  
  30. var size_suffix = 'z.jpg'; //To change the size of the returned image. See header changelog v1.3 for more info.
  31.  
  32. function getRandomInt (minInt, maxInt, amount, maxAvail)
  33. {
  34.     var randArr = new Array();
  35.     randArr[0] = 0;
  36.     var r = 0;
  37.     var e = 0;
  38.     var redo = false;
  39.     for(var i=0; i<amount; i++)
  40.     {
  41.        
  42.         r = Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt;
  43.         for(var p=0; p<randArr.length; p++)
  44.         {
  45.             if(r == randArr[p] && randArr.length <= maxAvail)
  46.             {
  47.                 redo = true;
  48.             }
  49.         }
  50.         if(redo)
  51.         {
  52.             i--;       
  53.         }
  54.         else
  55.         {
  56.             randArr[i] = r;
  57.         }
  58.     }
  59.     return randArr
  60. }
  61.  
  62. function jsonFlickrApi(rsp)
  63. {
  64.     if (rsp.stat != 'ok')
  65.     {
  66.         return;
  67.     }
  68.     var img_url = '';
  69.     var imgAlt = '';
  70.     var imgCount = rsp.photos.photo.length;
  71.     var elmCount = $('.flickr-img').length;
  72.     var imgIndexArr = getRandomInt(0,imgCount,elmCount, imgCount);
  73.            
  74.     $('.flickr-img').each(function(i)
  75.     {
  76.         img_url = 'http://farm' + rsp.photos.photo[imgIndexArr[i]].farm + '.static.flickr.com/' + rsp.photos.photo[imgIndexArr[i]].server + '/' + rsp.photos.photo[imgIndexArr[i]].id + '_' +rsp.photos.photo[imgIndexArr[i]].secret + '_' + size_suffix;
  77.         imgAlt = rsp.photos.photo[imgIndexArr[i]].title;
  78.         $(this).attr('src',img_url);
  79.         $(this).attr('alt',imgAlt);
  80.     })
  81. }
  82.  
  83. $(document).ready(function()
  84. {
  85.     $.getScript('https://api.flickr.com/services/rest/?format=json&sort=random&method=flickr.people.getPhotos&api_key='+api_key+'&user_id='+user_id)
  86. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement