Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Classic ASP Web API Examples

Primary Categories






(Classic ASP) Facebook Download all Photos to Local Files

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

ActiveX for 32-bit and 64-bit Windows

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
' 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 = Server.CreateObject("Chilkat_9_5_0.Cache")
' The cache will use 1 level of 256 sub-directories.
fbCache.Level = 1
' Use a directory path that makes sense on your operating system..
fbCache.AddRoot "C:/fbCache"

' This example assumes a previously obtained an access token
set oauth2 = Server.CreateObject("Chilkat_9_5_0.OAuth2")
oauth2.AccessToken = "FACEBOOK-ACCESS-TOKEN"

set rest = Server.CreateObject("Chilkat_9_5_0.Rest")

' Connect to Facebook.
success = rest.Connect("",443,1,1)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( rest.LastErrorText) & "</pre>"
End If

' Provide the authentication credentials (i.e. the access key)
success = rest.SetAuthOAuth2(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".
success = 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.
success = rest.AddQueryParam("limit","100")

' Get the 1st page of photos ids.
' See for more information.
responseJson = rest.FullRequestNoBody("GET","/v2.7/me/photos")
If (rest.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( rest.LastErrorText) & "</pre>"
End If

set photoJson = Server.CreateObject("Chilkat_9_5_0.JsonObject")
set saPhotoUrls = Server.CreateObject("Chilkat_9_5_0.StringArray")
set sbPhotoIdPath = Server.CreateObject("Chilkat_9_5_0.StringBuilder")

set json = Server.CreateObject("Chilkat_9_5_0.JsonObject")
json.EmitCompact = 0
success = json.Load(responseJson)

' Get the "after" cursor.
afterCursor = json.StringOf("paging.cursors.after")
Do While json.LastMethodSuccess = 1

    Response.Write "<pre>" & Server.HTMLEncode( "-------------------") & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( "afterCursor = " & afterCursor) & "</pre>"

    ' For each photo id in this page...
    i = 0
    numItems = json.SizeOfArray("data")
    Do While i < numItems
        json.I = i
        photoId = json.StringOf("data[i].id")
        Response.Write "<pre>" & Server.HTMLEncode( "photoId = " & photoId) & "</pre>"

        ' 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.
        photoJsonStr = fbCache.FetchText(photoId)
        If (fbCache.LastMethodSuccess = 0) Then
            ' It's not locally available, so get it from Facebook..
            success = sbPhotoIdPath.Append("/v2.7/")
            success = sbPhotoIdPath.Append(photoId)

            success = rest.ClearAllQueryParams()
            success = rest.AddQueryParam("fields","id,album,images")

            Response.Write "<pre>" & Server.HTMLEncode( "Fetching photo node from Facebook...") & "</pre>"

            ' This REST request will continue using the existing connection.
            ' If the connection was closed, it will automatically reconnect to send the request.
            photoJsonStr = rest.FullRequestNoBody("GET",sbPhotoIdPath.GetAsString())
            If (rest.LastMethodSuccess <> 1) Then
                Response.Write "<pre>" & Server.HTMLEncode( rest.LastErrorText) & "</pre>"
            End If

            ' Add the photo JSON to the local cache.
            success = fbCache.SaveTextNoExpire(photoId,"",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.
        success = photoJson.Load(photoJsonStr)
        imageUrl = photoJson.StringOf("images[0].source")
        If (photoJson.LastMethodSuccess = 1) Then

            ' Actually, we'll add a small JSON document that contains both the image ID and the URL.
            set imgUrlJson = Server.CreateObject("Chilkat_9_5_0.JsonObject")
            success = imgUrlJson.AppendString("id",photoId)
            success = imgUrlJson.AppendString("url",imageUrl)
            success = saPhotoUrls.Append(imgUrlJson.Emit())
            Response.Write "<pre>" & Server.HTMLEncode( "imageUrl = " & imageUrl) & "</pre>"
        End If

        i = 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.
    success = rest.ClearAllQueryParams()
    success = rest.AddQueryParam("type","uploaded")
    success = rest.AddQueryParam("limit","20")
    success = rest.AddQueryParam("after",afterCursor)

    ' Get the next page of photo ids.
    responseJson = rest.FullRequestNoBody("GET","/v2.7/me/photos")
    If (rest.LastMethodSuccess <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( rest.LastErrorText) & "</pre>"
    End If

    success = json.Load(responseJson)
    afterCursor = json.StringOf("paging.cursors.after")

Response.Write "<pre>" & Server.HTMLEncode( "No more pages of photos.") & "</pre>"

' 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 = Server.CreateObject("Chilkat_9_5_0.Http")

' We'll cache the image data so that if run again, we don't re-download the same image again.
numUrls = saPhotoUrls.Count
i = 0
set urlJson = Server.CreateObject("Chilkat_9_5_0.JsonObject")

set fac = Server.CreateObject("Chilkat_9_5_0.FileAccess")

Do While i < numUrls
    success = urlJson.Load(saPhotoUrls.GetString(i))
    photoId = urlJson.StringOf("id")
    imageUrl = urlJson.StringOf("url")

    ' Check the local cache for the image data.
    ' Only download and save if not already cached.
    imageBytes = fbCache.FetchFromCache(imageUrl)
    If (fbCache.LastMethodSuccess = 0) Then
        '  This photo needs to be downloaded.

        set sbImageUrl = Server.CreateObject("Chilkat_9_5_0.StringBuilder")
        success = sbImageUrl.Append(imageUrl)

        ' Let's form a filename..
        extension = ".jpg"
        If (sbImageUrl.Contains(".gif",0) = 1) Then
            extension = ".gif"
        End If

        If (sbImageUrl.Contains(".png",0) = 1) Then
            extension = ".png"
        End If

        If (sbImageUrl.Contains(".tiff",0) = 1) Then
            extension = ".tiff"
        End If

        If (sbImageUrl.Contains(".bmp",0) = 1) Then
            extension = ".bmp"
        End If

        set sbLocalFilePath = Server.CreateObject("Chilkat_9_5_0.StringBuilder")
        success = sbLocalFilePath.Append("C:/Photos/facebook/uploaded/")
        success = sbLocalFilePath.Append(photoId)
        success = sbLocalFilePath.Append(extension)

        imageBytes = http.QuickGet(imageUrl)
        If (http.LastMethodSuccess <> 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
        End If

        ' We've downloaded the photo image bytes into memory.
        ' Save it to the cache AND save it to the output file.
        success = fbCache.SaveToCacheNoExpire(imageUrl,"",imageBytes)
        success = fac.WriteEntireFile(sbLocalFilePath.GetAsString(),imageBytes)

        Response.Write "<pre>" & Server.HTMLEncode( "Downloaded to " & sbLocalFilePath.GetAsString()) & "</pre>"
    End If

    i = i + 1

Response.Write "<pre>" & Server.HTMLEncode( "Finished downloading all Facebook photos!") & "</pre>"



© 2000-2019 Chilkat Software, Inc. All Rights Reserved.