AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; 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.
$oFbCache = ObjCreate("Chilkat.Cache")
; The cache will use 1 level of 256 sub-directories.
$oFbCache.Level = 1
; Use a directory path that makes sense on your operating system..
$oFbCache.AddRoot "C:/fbCache"
; This example assumes a previously obtained an access token
$oOauth2 = ObjCreate("Chilkat.OAuth2")
$oOauth2.AccessToken = "FACEBOOK-ACCESS-TOKEN"
$oRest = ObjCreate("Chilkat.Rest")
; Connect to Facebook.
$bSuccess = $oRest.Connect("graph.facebook.com",443,True,True)
If ($bSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
; Provide the authentication credentials (i.e. the access key)
$oRest.SetAuthOAuth2($oOauth2)
; 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".
$oRest.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.
$oRest.AddQueryParam("limit","100")
; Get the 1st page of photos ids.
; See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
Local $sResponseJson = $oRest.FullRequestNoBody("GET","/v2.7/me/photos")
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
$oPhotoJson = ObjCreate("Chilkat.JsonObject")
$oSaPhotoUrls = ObjCreate("Chilkat.StringArray")
$oSbPhotoIdPath = ObjCreate("Chilkat.StringBuilder")
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.EmitCompact = False
$oJson.Load($sResponseJson)
Local $i
Local $sPhotoId
Local $sImageUrl
; Get the "after" cursor.
Local $sAfterCursor = $oJson.StringOf("paging.cursors.after")
While $oJson.LastMethodSuccess = True
ConsoleWrite("-------------------" & @CRLF)
ConsoleWrite("afterCursor = " & $sAfterCursor & @CRLF)
; For each photo id in this page...
$i = 0
Local $iNumItems = $oJson.SizeOfArray("data")
While $i < $iNumItems
$oJson.I = $i
$sPhotoId = $oJson.StringOf("data[i].id")
ConsoleWrite("photoId = " & $sPhotoId & @CRLF)
; 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.
Local $sPhotoJsonStr = $oFbCache.FetchText($sPhotoId)
If ($oFbCache.LastMethodSuccess = False) Then
; It's not locally available, so get it from Facebook..
$oSbPhotoIdPath.Clear
$oSbPhotoIdPath.Append("/v2.7/")
$oSbPhotoIdPath.Append($sPhotoId)
$oRest.ClearAllQueryParams()
$oRest.AddQueryParam("fields","id,album,images")
ConsoleWrite("Fetching photo node from Facebook..." & @CRLF)
; This REST request will continue using the existing connection.
; If the connection was closed, it will automatically reconnect to send the request.
$sPhotoJsonStr = $oRest.FullRequestNoBody("GET",$oSbPhotoIdPath.GetAsString())
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
; Add the photo JSON to the local cache.
$oFbCache.SaveTextNoExpire($sPhotoId,"",$sPhotoJsonStr)
EndIf
; 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.
$oPhotoJson.Load($sPhotoJsonStr)
$sImageUrl = $oPhotoJson.StringOf("images[0].source")
If ($oPhotoJson.LastMethodSuccess = True) Then
; Actually, we'll add a small JSON document that contains both the image ID and the URL.
$oImgUrlJson = ObjCreate("Chilkat.JsonObject")
$oImgUrlJson.AppendString("id",$sPhotoId)
$oImgUrlJson.AppendString("url",$sImageUrl)
$oSaPhotoUrls.Append($oImgUrlJson.Emit())
ConsoleWrite("imageUrl = " & $sImageUrl & @CRLF)
EndIf
$i = $i + 1
Wend
; 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.
$oRest.ClearAllQueryParams()
$oRest.AddQueryParam("type","uploaded")
$oRest.AddQueryParam("limit","20")
$oRest.AddQueryParam("after",$sAfterCursor)
; Get the next page of photo ids.
$sResponseJson = $oRest.FullRequestNoBody("GET","/v2.7/me/photos")
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
$oJson.Load($sResponseJson)
$sAfterCursor = $oJson.StringOf("paging.cursors.after")
Wend
ConsoleWrite("No more pages of photos." & @CRLF)
; 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.
$oHttp = ObjCreate("Chilkat.Http")
; We'll cache the image data so that if run again, we don't re-download the same image again.
Local $iNumUrls = $oSaPhotoUrls.Count
$i = 0
$oUrlJson = ObjCreate("Chilkat.JsonObject")
Local $oImageBytes
$oFac = ObjCreate("Chilkat.FileAccess")
While $i < $iNumUrls
$oUrlJson.Load($oSaPhotoUrls.GetString($i))
$sPhotoId = $oUrlJson.StringOf("id")
$sImageUrl = $oUrlJson.StringOf("url")
; Check the local cache for the image data.
; Only download and save if not already cached.
$oImageBytes = $oFbCache.FetchFromCache($sImageUrl)
If ($oFbCache.LastMethodSuccess = False) Then
; This photo needs to be downloaded.
$oSbImageUrl = ObjCreate("Chilkat.StringBuilder")
$oSbImageUrl.Append($sImageUrl)
; Let's form a filename..
Local $sExtension = ".jpg"
If ($oSbImageUrl.Contains(".gif",False) = True) Then
$sExtension = ".gif"
EndIf
If ($oSbImageUrl.Contains(".png",False) = True) Then
$sExtension = ".png"
EndIf
If ($oSbImageUrl.Contains(".tiff",False) = True) Then
$sExtension = ".tiff"
EndIf
If ($oSbImageUrl.Contains(".bmp",False) = True) Then
$sExtension = ".bmp"
EndIf
$oSbLocalFilePath = ObjCreate("Chilkat.StringBuilder")
$oSbLocalFilePath.Append("C:/Photos/facebook/uploaded/")
$oSbLocalFilePath.Append($sPhotoId)
$oSbLocalFilePath.Append($sExtension)
$oImageBytes = $oHttp.QuickGet($sImageUrl)
If ($oHttp.LastMethodSuccess <> True) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
; We've downloaded the photo image bytes into memory.
; Save it to the cache AND save it to the output file.
$oFbCache.SaveToCacheNoExpire($sImageUrl,"",$oImageBytes)
$oFac.WriteEntireFile($oSbLocalFilePath.GetAsString(),$oImageBytes)
ConsoleWrite("Downloaded to " & $oSbLocalFilePath.GetAsString() & @CRLF)
EndIf
$i = $i + 1
Wend
ConsoleWrite("Finished downloading all Facebook photos!" & @CRLF)