DataFlex Requires Chilkat v9.5.0.62+
DataFlex
Facebook Download all Photos to Local Files
See more Facebook Examples
Demonstrates 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.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoFbCache
Variant vOauth2
Handle hoOauth2
Handle hoRest
String sResponseJson
Handle hoPhotoJson
Handle hoSaPhotoUrls
Handle hoSbPhotoIdPath
Handle hoJson
Integer i
String sPhotoId
String sImageUrl
String sAfterCursor
Integer iNumItems
String sPhotoJsonStr
Handle hoImgUrlJson
Handle hoHttp
Integer iNumUrls
Handle hoUrlJson
Variant hoImageBytes
Handle hoFac
Handle hoSbImageUrl
String sExtension
Handle hoSbLocalFilePath
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatCache)) To hoFbCache
If (Not(IsComObjectCreated(hoFbCache))) Begin
Send CreateComObject of hoFbCache
End
// The cache will use 1 level of 256 sub-directories.
Set ComLevel Of hoFbCache To 1
// Use a directory path that makes sense on your operating system..
Send ComAddRoot To hoFbCache "C:/fbCache"
// This example assumes a previously obtained an access token
Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
If (Not(IsComObjectCreated(hoOauth2))) Begin
Send CreateComObject of hoOauth2
End
Set ComAccessToken Of hoOauth2 To "FACEBOOK-ACCESS-TOKEN"
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
// Connect to Facebook.
Get ComConnect Of hoRest "graph.facebook.com" 443 True True To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// Provide the authentication credentials (i.e. the access key)
Get pvComObject of hoOauth2 to vOauth2
Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess
// 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".
Get ComAddQueryParam Of hoRest "type" "uploaded" To iSuccess
// 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.
Get ComAddQueryParam Of hoRest "limit" "100" To iSuccess
// Get the 1st page of photos ids.
// See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
Get ComFullRequestNoBody Of hoRest "GET" "/v2.7/me/photos" To sResponseJson
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoPhotoJson
If (Not(IsComObjectCreated(hoPhotoJson))) Begin
Send CreateComObject of hoPhotoJson
End
Get Create (RefClass(cComChilkatStringTable)) To hoSaPhotoUrls
If (Not(IsComObjectCreated(hoSaPhotoUrls))) Begin
Send CreateComObject of hoSaPhotoUrls
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPhotoIdPath
If (Not(IsComObjectCreated(hoSbPhotoIdPath))) Begin
Send CreateComObject of hoSbPhotoIdPath
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Set ComEmitCompact Of hoJson To False
Get ComLoad Of hoJson sResponseJson To iSuccess
// Get the "after" cursor.
Get ComStringOf Of hoJson "paging.cursors.after" To sAfterCursor
While ((ComLastMethodSuccess(hoJson)) = True)
Showln "-------------------"
Showln "afterCursor = " sAfterCursor
// For each photo id in this page...
Move 0 To i
Get ComSizeOfArray Of hoJson "data" To iNumItems
While (i < iNumItems)
Set ComI Of hoJson To i
Get ComStringOf Of hoJson "data[i].id" To sPhotoId
Showln "photoId = " sPhotoId
// 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.
Get ComFetchText Of hoFbCache sPhotoId To sPhotoJsonStr
Get ComLastMethodSuccess Of hoFbCache To bTemp1
If (bTemp1 = False) Begin
// It's not locally available, so get it from Facebook..
Send ComClear To hoSbPhotoIdPath
Get ComAppend Of hoSbPhotoIdPath "/v2.7/" To iSuccess
Get ComAppend Of hoSbPhotoIdPath sPhotoId To iSuccess
Get ComClearAllQueryParams Of hoRest To iSuccess
Get ComAddQueryParam Of hoRest "fields" "id,album,images" To iSuccess
Showln "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.
Get ComGetAsString Of hoSbPhotoIdPath To sTemp1
Get ComFullRequestNoBody Of hoRest "GET" sTemp1 To sPhotoJsonStr
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// Add the photo JSON to the local cache.
Get ComSaveTextNoExpire Of hoFbCache sPhotoId "" sPhotoJsonStr To iSuccess
End
// 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.
Get ComLoad Of hoPhotoJson sPhotoJsonStr To iSuccess
Get ComStringOf Of hoPhotoJson "images[0].source" To sImageUrl
Get ComLastMethodSuccess Of hoPhotoJson To bTemp1
If (bTemp1 = True) Begin
// Actually, we'll add a small JSON document that contains both the image ID and the URL.
Get Create (RefClass(cComChilkatJsonObject)) To hoImgUrlJson
If (Not(IsComObjectCreated(hoImgUrlJson))) Begin
Send CreateComObject of hoImgUrlJson
End
Get ComAppendString Of hoImgUrlJson "id" sPhotoId To iSuccess
Get ComAppendString Of hoImgUrlJson "url" sImageUrl To iSuccess
Get ComEmit Of hoImgUrlJson To sTemp1
Get ComAppend Of hoSaPhotoUrls sTemp1 To iSuccess
Showln "imageUrl = " sImageUrl
End
Move (i + 1) To i
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.
Get ComClearAllQueryParams Of hoRest To iSuccess
Get ComAddQueryParam Of hoRest "type" "uploaded" To iSuccess
Get ComAddQueryParam Of hoRest "limit" "20" To iSuccess
Get ComAddQueryParam Of hoRest "after" sAfterCursor To iSuccess
// Get the next page of photo ids.
Get ComFullRequestNoBody Of hoRest "GET" "/v2.7/me/photos" To sResponseJson
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLoad Of hoJson sResponseJson To iSuccess
Get ComStringOf Of hoJson "paging.cursors.after" To sAfterCursor
Loop
Showln "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.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// We'll cache the image data so that if run again, we don't re-download the same image again.
Get ComCount Of hoSaPhotoUrls To iNumUrls
Move 0 To i
Get Create (RefClass(cComChilkatJsonObject)) To hoUrlJson
If (Not(IsComObjectCreated(hoUrlJson))) Begin
Send CreateComObject of hoUrlJson
End
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
While (i < iNumUrls)
Get ComStringAt Of hoSaPhotoUrls i To sTemp1
Get ComLoad Of hoUrlJson sTemp1 To iSuccess
Get ComStringOf Of hoUrlJson "id" To sPhotoId
Get ComStringOf Of hoUrlJson "url" To sImageUrl
// Check the local cache for the image data.
// Only download and save if not already cached.
Get ComFetchFromCache Of hoFbCache sImageUrl To hoImageBytes
Get ComLastMethodSuccess Of hoFbCache To bTemp1
If (bTemp1 = False) Begin
// This photo needs to be downloaded.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbImageUrl
If (Not(IsComObjectCreated(hoSbImageUrl))) Begin
Send CreateComObject of hoSbImageUrl
End
Get ComAppend Of hoSbImageUrl sImageUrl To iSuccess
// Let's form a filename..
Move ".jpg" To sExtension
Get ComContains Of hoSbImageUrl ".gif" False To bTemp1
If (bTemp1 = True) Begin
Move ".gif" To sExtension
End
Get ComContains Of hoSbImageUrl ".png" False To bTemp1
If (bTemp1 = True) Begin
Move ".png" To sExtension
End
Get ComContains Of hoSbImageUrl ".tiff" False To bTemp1
If (bTemp1 = True) Begin
Move ".tiff" To sExtension
End
Get ComContains Of hoSbImageUrl ".bmp" False To bTemp1
If (bTemp1 = True) Begin
Move ".bmp" To sExtension
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbLocalFilePath
If (Not(IsComObjectCreated(hoSbLocalFilePath))) Begin
Send CreateComObject of hoSbLocalFilePath
End
Get ComAppend Of hoSbLocalFilePath "C:/Photos/facebook/uploaded/" To iSuccess
Get ComAppend Of hoSbLocalFilePath sPhotoId To iSuccess
Get ComAppend Of hoSbLocalFilePath sExtension To iSuccess
Get ComQuickGet Of hoHttp sImageUrl To hoImageBytes
Get ComLastMethodSuccess Of hoHttp To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
// We've downloaded the photo image bytes into memory.
// Save it to the cache AND save it to the output file.
Get ComSaveToCacheNoExpire Of hoFbCache sImageUrl "" vImageBytes To iSuccess
Get ComGetAsString Of hoSbLocalFilePath To sTemp1
Get ComWriteEntireFile Of hoFac sTemp1 vImageBytes To iSuccess
Get ComGetAsString Of hoSbLocalFilePath To sTemp1
Showln "Downloaded to " sTemp1
End
Move (i + 1) To i
Loop
Showln "Finished downloading all Facebook photos!"
End_Procedure