Sample code for 30+ languages & platforms
Tcl Requires Chilkat v9.5.0.62+

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  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.
set fbCache [new_CkCache]

#  The cache will use 1 level of 256 sub-directories.
CkCache_put_Level $fbCache 1
#  Use a directory path that makes sense on your operating system..
CkCache_AddRoot $fbCache "C:/fbCache"

#  This example assumes a previously obtained an access token
set oauth2 [new_CkOAuth2]

CkOAuth2_put_AccessToken $oauth2 "FACEBOOK-ACCESS-TOKEN"

set rest [new_CkRest]

#  Connect to Facebook.
set success [CkRest_Connect $rest "graph.facebook.com" 443 1 1]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkCache $fbCache
    delete_CkOAuth2 $oauth2
    delete_CkRest $rest
    exit
}

#  Provide the authentication credentials (i.e. the access key)
CkRest_SetAuthOAuth2 $rest $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".
CkRest_AddQueryParam $rest "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.
CkRest_AddQueryParam $rest "limit" "100"

#  Get the 1st page of photos ids.
#  See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
set responseJson [CkRest_fullRequestNoBody $rest "GET" "/v2.7/me/photos"]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkCache $fbCache
    delete_CkOAuth2 $oauth2
    delete_CkRest $rest
    exit
}

set photoJson [new_CkJsonObject]

set saPhotoUrls [new_CkStringTable]

set sbPhotoIdPath [new_CkStringBuilder]

set json [new_CkJsonObject]

CkJsonObject_put_EmitCompact $json 0
CkJsonObject_Load $json $responseJson

#  Get the "after" cursor.
set afterCursor [CkJsonObject_stringOf $json "paging.cursors.after"]
while {[CkJsonObject_get_LastMethodSuccess $json] == 1} {

    puts "-------------------"
    puts "afterCursor = $afterCursor"

    #  For each photo id in this page...
    set i 0
    set numItems [CkJsonObject_SizeOfArray $json "data"]
    while {$i < $numItems} {
        CkJsonObject_put_I $json $i
        set photoId [CkJsonObject_stringOf $json "data[i].id"]
        puts "photoId = $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.
        set photoJsonStr [CkCache_fetchText $fbCache $photoId]
        if {[CkCache_get_LastMethodSuccess $fbCache] == 0} then {
            #  It's not locally available, so get it from Facebook..
            CkStringBuilder_Clear $sbPhotoIdPath
            CkStringBuilder_Append $sbPhotoIdPath "/v2.7/"
            CkStringBuilder_Append $sbPhotoIdPath $photoId

            CkRest_ClearAllQueryParams $rest
            CkRest_AddQueryParam $rest "fields" "id,album,images"

            puts "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.
            set photoJsonStr [CkRest_fullRequestNoBody $rest "GET" [CkStringBuilder_getAsString $sbPhotoIdPath]]
            if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
                puts [CkRest_lastErrorText $rest]
                delete_CkCache $fbCache
                delete_CkOAuth2 $oauth2
                delete_CkRest $rest
                delete_CkJsonObject $photoJson
                delete_CkStringTable $saPhotoUrls
                delete_CkStringBuilder $sbPhotoIdPath
                delete_CkJsonObject $json
                exit
            }

            #  Add the photo JSON to the local cache.
            CkCache_SaveTextNoExpire $fbCache $photoId "" $photoJsonStr
        }

        #  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.
        CkJsonObject_Load $photoJson $photoJsonStr
        set imageUrl [CkJsonObject_stringOf $photoJson "images[0].source"]
        if {[CkJsonObject_get_LastMethodSuccess $photoJson] == 1} then {

            #  Actually, we'll add a small JSON document that contains both the image ID and the URL.
            set imgUrlJson [new_CkJsonObject]

            CkJsonObject_AppendString $imgUrlJson "id" $photoId
            CkJsonObject_AppendString $imgUrlJson "url" $imageUrl
            CkStringTable_Append $saPhotoUrls [CkJsonObject_emit $imgUrlJson]
            puts "imageUrl = $imageUrl"
        }

        set i [expr $i + 1]
    }

    #  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.
    CkRest_ClearAllQueryParams $rest
    CkRest_AddQueryParam $rest "type" "uploaded"
    CkRest_AddQueryParam $rest "limit" "20"
    CkRest_AddQueryParam $rest "after" $afterCursor

    #  Get the next page of photo ids.
    set responseJson [CkRest_fullRequestNoBody $rest "GET" "/v2.7/me/photos"]
    if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
        puts [CkRest_lastErrorText $rest]
        delete_CkCache $fbCache
        delete_CkOAuth2 $oauth2
        delete_CkRest $rest
        delete_CkJsonObject $photoJson
        delete_CkStringTable $saPhotoUrls
        delete_CkStringBuilder $sbPhotoIdPath
        delete_CkJsonObject $json
        delete_CkJsonObject $imgUrlJson
        exit
    }

    CkJsonObject_Load $json $responseJson
    set afterCursor [CkJsonObject_stringOf $json "paging.cursors.after"]
}

puts "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.  
set http [new_CkHttp]

#  We'll cache the image data so that if run again, we don't re-download the same image again.
set numUrls [CkStringTable_get_Count $saPhotoUrls]
set i 0
set urlJson [new_CkJsonObject]

set imageBytes [new_CkByteData]

set fac [new_CkFileAccess]

while {$i < $numUrls} {
    CkJsonObject_Load $urlJson [CkStringTable_stringAt $saPhotoUrls $i]
    set photoId [CkJsonObject_stringOf $urlJson "id"]
    set imageUrl [CkJsonObject_stringOf $urlJson "url"]

    #  Check the local cache for the image data.
    #  Only download and save if not already cached.
    set success [CkCache_FetchFromCache $fbCache $imageUrl $imageBytes]
    if {[CkCache_get_LastMethodSuccess $fbCache] == 0} then {
        #   This photo needs to be downloaded.

        set sbImageUrl [new_CkStringBuilder]

        CkStringBuilder_Append $sbImageUrl $imageUrl

        #  Let's form a filename..
        set extension ".jpg"
        if {[CkStringBuilder_Contains $sbImageUrl ".gif" 0] == 1} then {
            set extension ".gif"
        }

        if {[CkStringBuilder_Contains $sbImageUrl ".png" 0] == 1} then {
            set extension ".png"
        }

        if {[CkStringBuilder_Contains $sbImageUrl ".tiff" 0] == 1} then {
            set extension ".tiff"
        }

        if {[CkStringBuilder_Contains $sbImageUrl ".bmp" 0] == 1} then {
            set extension ".bmp"
        }

        set sbLocalFilePath [new_CkStringBuilder]

        CkStringBuilder_Append $sbLocalFilePath "C:/Photos/facebook/uploaded/"
        CkStringBuilder_Append $sbLocalFilePath $photoId
        CkStringBuilder_Append $sbLocalFilePath $extension

        set success [CkHttp_QuickGet $http $imageUrl $imageBytes]
        if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
            puts [CkHttp_lastErrorText $http]
            delete_CkCache $fbCache
            delete_CkOAuth2 $oauth2
            delete_CkRest $rest
            delete_CkJsonObject $photoJson
            delete_CkStringTable $saPhotoUrls
            delete_CkStringBuilder $sbPhotoIdPath
            delete_CkJsonObject $json
            delete_CkJsonObject $imgUrlJson
            delete_CkHttp $http
            delete_CkJsonObject $urlJson
            delete_CkByteData $imageBytes
            delete_CkFileAccess $fac
            delete_CkStringBuilder $sbImageUrl
            delete_CkStringBuilder $sbLocalFilePath
            exit
        }

        #  We've downloaded the photo image bytes into memory.
        #  Save it to the cache AND save it to the output file.
        CkCache_SaveToCacheNoExpire $fbCache $imageUrl "" $imageBytes
        CkFileAccess_WriteEntireFile $fac [CkStringBuilder_getAsString $sbLocalFilePath] $imageBytes

        puts "Downloaded to [CkStringBuilder_getAsString $sbLocalFilePath]"
    }

    set i [expr $i + 1]
}

puts "Finished downloading all Facebook photos!"

delete_CkCache $fbCache
delete_CkOAuth2 $oauth2
delete_CkRest $rest
delete_CkJsonObject $photoJson
delete_CkStringTable $saPhotoUrls
delete_CkStringBuilder $sbPhotoIdPath
delete_CkJsonObject $json
delete_CkJsonObject $imgUrlJson
delete_CkHttp $http
delete_CkJsonObject $urlJson
delete_CkByteData $imageBytes
delete_CkFileAccess $fac
delete_CkStringBuilder $sbImageUrl
delete_CkStringBuilder $sbLocalFilePath