# Get-FlickrPhotos v.0 # coded by Daniele Muscetta on a rainy sunday - 13 January 2008 # # # inspired by flickoffr.php by kosso: # http://kosso.wordpress.com/2007/01/31/free-php5-script-to-grab-your-flickr-photos/ # http://noklog.com/flickoffr.phps param([string]$username=$(throw "You must specify a username to search for")) # IMPORTANT: get a real flickr API key from here [ http://www.flickr.com/services/api/keys/ ] # this is a fictictious api key and WILL NOT work! $api_key = "12345678901234567890123456789012" $count = 0 $totalCount = 0 #we want to start from the first page $page = 1 #set default dir (could make it a parameter too for a nicer script....) $global:backupdir = "D:\appo\psflickr\" #need .Net to do the web fetching anyway [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null function global:Get-Photos ([string]$username,[int]$page) { # Get user_id given username # Build the API request to look up the flickr user_id $params = @{api_key = $api_key; method = 'flickr.people.findByUsername'; username = $username} $url = "http://api.flickr.com/services/rest/?" $params.GetEnumerator() | % {$url = $url + [System.Web.HttpUtility]::UrlEncode($_.Key) + "=" + [System.Web.HttpUtility]::UrlEncode($_.Value) + "&"} $xml = [xml](New-Object net.webclient).DownloadString($url) if ($xml.rsp.stat -ne "ok"){ throw "Cannot find that username on Flickr!" } $user_id = $xml.rsp.user.id if($page -eq 1){ $output = "Found user id for " + $username + ": " + $user_id Write-Host $output } # Get photos, given user_id # Build the API request to search for photos by this user $per_page = 100 #change to retrieve more or less photos, 500 is the max supported by the API AFAIK - a higher number here means you will run through less pages so you will make less (but heavier) requests to flickr web service $params = @{api_key = $api_key; method = 'flickr.photos.search'; user_id = $user_id; page = $page; per_page = $per_page} $url = "http://api.flickr.com/services/rest/?" $params.GetEnumerator() | % {$url = $url + [System.Web.HttpUtility]::UrlEncode($_.Key) + "=" + [System.Web.HttpUtility]::UrlEncode($_.Value) + "&"} $simplephotos = [xml](New-Object net.webclient).DownloadString($url) if ($simplephotos.rsp.stat -ne "ok"){ throw "Cannot find any photo!" } $global:totalCount = $simplephotos.rsp.photos.total if($page -eq 1){ $output = "Found a total of " + $global:totalCount + " photos" Write-Host $output } #does the actual enumeration of your photos and downloads them $clnt = new-object System.Net.WebClient foreach ($photo in $simplephotos.rsp.photos.photo){ $global:count = $global:count + 1 #the urls to the different photo sizes are built like this: #http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_o_d.jpg // original #http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_d.jpg // medium #http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_m_d.jpg // small #http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_t_d.jpg // thumbnail #http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_s_d.jpg // square #changing the suffix as per the scheme above will download a different version $suffix = "_d.jpg" $photourl = "http://farm" + $photo.farm + ".static.flickr.com/" + $photo.server + "/" + $photo.id + "_" + $photo.secret + $suffix $filename = $backupdir + $photo.id + "_" + $photo.secret + $suffix $output = "Downloading the photo that you can see at http://www.flickr.com/photos/" + $user_id + "/" + $photo.id + " to " + $filename Write-Host $output #the following line does the actual download of the photo, so you can comment it to just try to see if paths are built correctly, etc $clnt.DownloadFile($photourl,$filename) } return $page } do { $lastpage = Get-Photos $username $page $page = $lastpage + 1 } while ($global:count -lt $global:totalCount) #clearing up $global:count = $zero $global:totalCount = $zero $page = $zero $lastpage= $zero