Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Facebook Download all Photos to Local FilesDemonstrates how to download all of one's Facebook photos to a local filesystem directory. This sample code keeps a local cache to avoid re-downloading the same photos twice. The program can be run again after a time, and it will download only photos that haven't yet been downloaded.
integer li_rc oleobject loo_FbCache oleobject loo_Oauth2 oleobject loo_Rest integer li_Success string ls_ResponseJson oleobject loo_PhotoJson oleobject loo_SaPhotoUrls oleobject loo_SbPhotoIdPath oleobject loo_Json integer i string ls_PhotoId string ls_ImageUrl string ls_AfterCursor integer li_NumItems string ls_PhotoJsonStr oleobject loo_ImgUrlJson oleobject loo_Http integer li_NumUrls oleobject loo_UrlJson oleobject loo_Fac oleobject loo_SbImageUrl string ls_Extension oleobject loo_SbLocalFilePath // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This example will use a local disk cache to avoid re-fetching the same // photo id after it's been fetched once. loo_FbCache = create oleobject // Use "Chilkat_9_5_0.Cache" for versions of Chilkat < 10.0.0 li_rc = loo_FbCache.ConnectToNewObject("Chilkat.Cache") if li_rc < 0 then destroy loo_FbCache MessageBox("Error","Connecting to COM object failed") return end if // The cache will use 1 level of 256 sub-directories. loo_FbCache.Level = 1 // Use a directory path that makes sense on your operating system.. loo_FbCache.AddRoot("C:/fbCache") // This example assumes a previously obtained an access token loo_Oauth2 = create oleobject // Use "Chilkat_9_5_0.OAuth2" for versions of Chilkat < 10.0.0 li_rc = loo_Oauth2.ConnectToNewObject("Chilkat.OAuth2") loo_Oauth2.AccessToken = "FACEBOOK-ACCESS-TOKEN" loo_Rest = create oleobject // Use "Chilkat_9_5_0.Rest" for versions of Chilkat < 10.0.0 li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest") // Connect to Facebook. li_Success = loo_Rest.Connect("graph.facebook.com",443,1,1) if li_Success <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_FbCache destroy loo_Oauth2 destroy loo_Rest return end if // Provide the authentication credentials (i.e. the access key) loo_Rest.SetAuthOAuth2(loo_Oauth2) // There are two choices: // We can choose to download the photos the person is tagged in or has uploaded // by setting type to "tagged" or "uploaded". loo_Rest.AddQueryParam("type","uploaded") // To download all photos, we begin with an outer loop that iterates over // the list of photo nodes in pages. Each page returned contains a list of // photo node ids. Each photo node id must be retrieved to get the download URL(s) // of the actual image. // I don't know the max limit for the number of records that can be downloaded at once. loo_Rest.AddQueryParam("limit","100") // Get the 1st page of photos ids. // See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information. ls_ResponseJson = loo_Rest.FullRequestNoBody("GET","/v2.7/me/photos") if loo_Rest.LastMethodSuccess <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_FbCache destroy loo_Oauth2 destroy loo_Rest return end if loo_PhotoJson = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_PhotoJson.ConnectToNewObject("Chilkat.JsonObject") loo_SaPhotoUrls = create oleobject // Use "Chilkat_9_5_0.StringArray" for versions of Chilkat < 10.0.0 li_rc = loo_SaPhotoUrls.ConnectToNewObject("Chilkat.StringArray") loo_SbPhotoIdPath = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbPhotoIdPath.ConnectToNewObject("Chilkat.StringBuilder") loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") loo_Json.EmitCompact = 0 loo_Json.Load(ls_ResponseJson) // Get the "after" cursor. ls_AfterCursor = loo_Json.StringOf("paging.cursors.after") do while loo_Json.LastMethodSuccess = 1 Write-Debug "-------------------" Write-Debug "afterCursor = " + ls_AfterCursor // For each photo id in this page... i = 0 li_NumItems = loo_Json.SizeOfArray("data") do while i < li_NumItems loo_Json.I = i ls_PhotoId = loo_Json.StringOf("data[i].id") Write-Debug "photoId = " + ls_PhotoId // We need to fetch the JSON for this photo. Check to see if it's in the local disk cache, // and if not, then get it from Facebook. ls_PhotoJsonStr = loo_FbCache.FetchText(ls_PhotoId) if loo_FbCache.LastMethodSuccess = 0 then // It's not locally available, so get it from Facebook.. loo_SbPhotoIdPath.Clear() loo_SbPhotoIdPath.Append("/v2.7/") loo_SbPhotoIdPath.Append(ls_PhotoId) loo_Rest.ClearAllQueryParams() loo_Rest.AddQueryParam("fields","id,album,images") Write-Debug "Fetching photo node from Facebook..." // This REST request will continue using the existing connection. // If the connection was closed, it will automatically reconnect to send the request. ls_PhotoJsonStr = loo_Rest.FullRequestNoBody("GET",loo_SbPhotoIdPath.GetAsString()) if loo_Rest.LastMethodSuccess <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_FbCache destroy loo_Oauth2 destroy loo_Rest destroy loo_PhotoJson destroy loo_SaPhotoUrls destroy loo_SbPhotoIdPath destroy loo_Json return end if // Add the photo JSON to the local cache. loo_FbCache.SaveTextNoExpire(ls_PhotoId,"",ls_PhotoJsonStr) end if // Parse the photo JSON and add the main photo download URL to saPhotoUrls // There may be multiple URLs in the images array, but the 1st one is the largest and main photo URL. // The others are smaller sizes of the same photo. loo_PhotoJson.Load(ls_PhotoJsonStr) ls_ImageUrl = loo_PhotoJson.StringOf("images[0].source") if loo_PhotoJson.LastMethodSuccess = 1 then // Actually, we'll add a small JSON document that contains both the image ID and the URL. loo_ImgUrlJson = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_ImgUrlJson.ConnectToNewObject("Chilkat.JsonObject") loo_ImgUrlJson.AppendString("id",ls_PhotoId) loo_ImgUrlJson.AppendString("url",ls_ImageUrl) loo_SaPhotoUrls.Append(loo_ImgUrlJson.Emit()) Write-Debug "imageUrl = " + ls_ImageUrl end if i = i + 1 loop // Prepare for getting the next page of photos ids. // We can continue using the same REST object. // If already connected, we'll continue using the existing connection. // Otherwise, a new connection will automatically be made if needed. loo_Rest.ClearAllQueryParams() loo_Rest.AddQueryParam("type","uploaded") loo_Rest.AddQueryParam("limit","20") loo_Rest.AddQueryParam("after",ls_AfterCursor) // Get the next page of photo ids. ls_ResponseJson = loo_Rest.FullRequestNoBody("GET","/v2.7/me/photos") if loo_Rest.LastMethodSuccess <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_FbCache destroy loo_Oauth2 destroy loo_Rest destroy loo_PhotoJson destroy loo_SaPhotoUrls destroy loo_SbPhotoIdPath destroy loo_Json destroy loo_ImgUrlJson return end if loo_Json.Load(ls_ResponseJson) ls_AfterCursor = loo_Json.StringOf("paging.cursors.after") loop Write-Debug "No more pages of photos." // Now iterate over the photo URLs and download each to a file. // We can use Chilkat HTTP. No Facebook authorization (access token) is required to download // the photo once the URL is known. loo_Http = create oleobject // Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 li_rc = loo_Http.ConnectToNewObject("Chilkat.Http") // We'll cache the image data so that if run again, we don't re-download the same image again. li_NumUrls = loo_SaPhotoUrls.Count i = 0 loo_UrlJson = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_UrlJson.ConnectToNewObject("Chilkat.JsonObject") loo_Fac = create oleobject // Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess") do while i < li_NumUrls loo_UrlJson.Load(loo_SaPhotoUrls.GetString(i)) ls_PhotoId = loo_UrlJson.StringOf("id") ls_ImageUrl = loo_UrlJson.StringOf("url") // Check the local cache for the image data. // Only download and save if not already cached. loo_ImageBytes = loo_FbCache.FetchFromCache(ls_ImageUrl) if loo_FbCache.LastMethodSuccess = 0 then // This photo needs to be downloaded. loo_SbImageUrl = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbImageUrl.ConnectToNewObject("Chilkat.StringBuilder") loo_SbImageUrl.Append(ls_ImageUrl) // Let's form a filename.. ls_Extension = ".jpg" if loo_SbImageUrl.Contains(".gif",0) = 1 then ls_Extension = ".gif" end if if loo_SbImageUrl.Contains(".png",0) = 1 then ls_Extension = ".png" end if if loo_SbImageUrl.Contains(".tiff",0) = 1 then ls_Extension = ".tiff" end if if loo_SbImageUrl.Contains(".bmp",0) = 1 then ls_Extension = ".bmp" end if loo_SbLocalFilePath = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbLocalFilePath.ConnectToNewObject("Chilkat.StringBuilder") loo_SbLocalFilePath.Append("C:/Photos/facebook/uploaded/") loo_SbLocalFilePath.Append(ls_PhotoId) loo_SbLocalFilePath.Append(ls_Extension) loo_ImageBytes = loo_Http.QuickGet(ls_ImageUrl) if loo_Http.LastMethodSuccess <> 1 then Write-Debug loo_Http.LastErrorText destroy loo_FbCache destroy loo_Oauth2 destroy loo_Rest destroy loo_PhotoJson destroy loo_SaPhotoUrls destroy loo_SbPhotoIdPath destroy loo_Json destroy loo_ImgUrlJson destroy loo_Http destroy loo_UrlJson destroy loo_Fac destroy loo_SbImageUrl destroy loo_SbLocalFilePath return end if // We've downloaded the photo image bytes into memory. // Save it to the cache AND save it to the output file. loo_FbCache.SaveToCacheNoExpire(ls_ImageUrl,"",loo_ImageBytes) loo_Fac.WriteEntireFile(loo_SbLocalFilePath.GetAsString(),loo_ImageBytes) Write-Debug "Downloaded to " + loo_SbLocalFilePath.GetAsString() end if i = i + 1 loop Write-Debug "Finished downloading all Facebook photos!" destroy loo_FbCache destroy loo_Oauth2 destroy loo_Rest destroy loo_PhotoJson destroy loo_SaPhotoUrls destroy loo_SbPhotoIdPath destroy loo_Json destroy loo_ImgUrlJson destroy loo_Http destroy loo_UrlJson destroy loo_Fac destroy loo_SbImageUrl destroy loo_SbLocalFilePath |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.