Get flickr photos with Classic ASP & Flickr API
Posted on 08.05.09 14:56:28 by Ariel G. Saputra in Classic ASP, Tutorials
I’am using flickr quite a while but not too much just to store screenshots of my web portfolio, then when I build a web design portfolio page I decided to get those screenshots directly from flickr rather than re-upload them in my server. In php there are a lot of examples on the net but I found none for Classic ASP.
After trial error here are the script I use to get flickr photos with flickr API and Classic ASP, it may not perfect but at least it worked for me
Here I use 2 flickr API methods
- flickr.photosets.getPhotos, to get list photos of a photosets since i store all those screenshots in a photosets
- flickr.photos.getInfo, to get the detail photo information
List of functions:
- flickrGetUrlPhotosets, setup the “flickr.photosets.getPhotos” url with necessary parameter
- flickrGetUrlPhotoInfo, setup the “flickr.photos.getInfo” url with required params
- flickrGetPhotoInfo, get detail information about particular photo
- flickrGetPhotosetsPhoto, retrive all the photo of a flickr photosets
function flickrGetUrlPhotosets
' get the API url flickr.photosets.getPhotos function flickrGetUrlPhotosets(extras,privacy_filter,per_page,page,media) url = "http://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos" &_ "&api_key="&API_KEY &_ "&photoset_id="&PHOTOSET_ID if not isEmpty(per_page) then url = url & "&per_page="&per_page if not isEmpty(page) then url = url & "&page="&page if not isEmpty(media) then url = url & "&media="&media if not isEmpty(privacy_filter) then url = url & "&privacy_filter="&privacy_filter if not isEmpty(extras) then url = url & "&extras="&extras flickrGetUrlPhotosets = url end function
function flickrGetUrlPhotoInfo
' get the ASPI url flickr.photos.getInfo function flickrGetUrlPhotoInfo(photo_id,secret) url = "http://www.flickr.com/services/rest/?method=flickr.photos.getInfo" &_ "&api_key="&API_KEY &_ "&photo_id="&photo_id &_ "&secret="&secret flickrGetUrlPhotoInfo = url end function
function flickrGetPhotoInfo
' retrive photo information
' return an array (title, description, thumb, preview)
Function flickrGetPhotoInfo(flickrAPIUrl)
dim xmlhttpPhoto,intTimeout
intTimeout = 5000
on error resume next
set xmlhttpPhoto = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
xmlhttpPhoto.setTimeouts intTimeout, intTimeout, intTimeout, intTimeout
xmlhttpPhoto.Open "GET", flickrAPIUrl, false
xmlhttpPhoto.Send
if err <> 0 then exit function
dim xmlPhotoDetail
set xmlPhotoDetail = Server.CreateObject("Microsoft.XMLDOM")
xmlPhotoDetail.async = false
xmlPhotoDetail.setProperty "SelectionLanguage", "XPath"
xmlPhotoDetail.loadxml(xmlhttpPhoto.ResponseText)
dim dPhoto(4)
Set photoNode = xmlPhotoDetail.selectNodes("/rsp/photo/title")
For Each photoDetailNode in photoNode
dPhoto(0) = photoDetailNode.Text
next
Set photoNode = xmlPhotoDetail.selectNodes("/rsp/photo/description")
For Each photoDetailNode in photoNode
dPhoto(1) = photoDetailNode.Text
next
Set photoNode = xmlPhotoDetail.selectNodes("/rsp/photo")
For Each photoDetailNode in photoNode
photourl = "http://farm" & photoDetailNode.getAttribute("farm") & ".static.flickr.com/" &_
photoDetailNode.getAttribute("server") & "/" &_
photoDetailNode.getAttribute("id") & "_" &_
photoDetailNode.getAttribute("secret")
dPhoto(2) = photourl & "_s.jpg"
dPhoto(3) = photourl & ".jpg"
next
Set photoDetailNode = Nothing
Set photoNode = Nothing
Set xmlPhotoDetail = Nothing
flickrGetPhotoInfo = dPhoto
End Function
function flickrGetPhotosetsPhoto
' retrive photos in a photosets
' return array
function flickrGetPhotosetsPhoto(flickrAPIUrl)
dim result()
dim xmlhttp,intTimeout
intTimeout = 5000
on error resume next
set xmlhttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
xmlhttp.Open "GET", flickrAPIUrl, false
xmlhttp.setTimeouts intTimeout, intTimeout, intTimeout, intTimeout
xmlhttp.Send
if err <> 0 then exit function
set source1 = Server.CreateObject("Microsoft.XMLDOM")
source1.async = false
source1.setProperty "SelectionLanguage", "XPath"
source1.loadxml(xmlhttp.ResponseText)
Set PhotosNodes = source1.selectNodes("/rsp/photoset/photo")
photoCount = 0
For Each PhotoNodeItem in PhotosNodes
redim preserve result(photoCount+1)
result(photoCount) = flickrGetPhotoInfo(flickrGetUrlPhotoInfo(PhotoNodeItem.getAttribute("id"),PhotoNodeItem.getAttribute("secret")))
photoCount = photoCount + 1
Next
Set PhotoNodeItem = Nothing
Set PhotosNodes = Nothing
Set source1 = Nothing
flickrGetPhotosetsPhoto = result
end function
To use it pretty simple, first define your api key and photosets ID where all the photos stored
Define your API Key & Photosets ID
' define your API KEY & photosets ID const API_KEY = "your_api_key" const PHOTOSET_ID = "your_photosets_id"
Then call the function flickrGetPhotosetsPhoto with necessary parameters, and store it in a variable, once the process completed you process the variable as needed. Below is the exmaple of imlementation.
Call the main function & process as necessary
Html = ""
FlickrPhotos = flickrGetPhotosetsPhoto(flickrGetUrlPhotosets("",1,5,1,"photos"))
if isArray(FlickrPhotos) then
Html = Html & "<ul>"
for i=Lbound(FlickrPhotos) to UBound(FlickrPhotos)
if isArray(FlickrPhotos(i)) then
d = FlickrPhotos(i)
Html = Html & "<li>"&_
"<a href="""+d(3)+""" title="""+d(0)+""">"&_
"<img src="""+d(2)+""" alt="""+d(0)+""" />"&_
"</a>"&_
"</li>"
end if
next
Html = Html & "</ul>"
end if
Response.Write(Html)
So What’s the next? well the most important is caching the photos and the result in your server to save your server resources. You can also modify the function to get paging etc.
Have fun with ASP
Author: Ariel G. Saputra
2 Comments For This Post
3 Trackbacks For This Post
-
14.05.09 11:27:48 at 11:27
Create TinyURL with Classic ASP (VBScript) | ASP Blog Says:
[...] Get flickr photos with Classic ASP & Flickr API [...]
-
14.05.09 11:52:12 at 11:52
Update Twitter Status with Classic ASP (VBScript) | ASP Blog Says:
[...] Get flickr photos with Classic ASP & Flickr API [...]
-
15.05.09 03:37:06 at 03:37
Short URL with Classic ASP | ASP Blog Says:
[...] have more spare time in back couple days and I use it to play a bit with Classic ASP, flickr and twitter for a reason, but then pop out an idea to create a very simple library tom implement [...]



SzR
22/06/09 21:10
thanks for the post, really very usefull:)
have fun with asp=)
barao!
03/02/10 02:16
hey thanks for the script! very helpfull, but in my site http://www.fernandafreixosa.com/galeria_boxes.asp the photo load is too slow, can u help me, plz?
tk