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

Visual FoxPro Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Visual FoxPro) 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

LOCAL loFbCache
LOCAL loOauth2
LOCAL loRest
LOCAL lnSuccess
LOCAL lcResponseJson
LOCAL loPhotoJson
LOCAL loSaPhotoUrls
LOCAL loSbPhotoIdPath
LOCAL loJson
LOCAL i
LOCAL lcPhotoId
LOCAL lcImageUrl
LOCAL lcAfterCursor
LOCAL lnNumItems
LOCAL lcPhotoJsonStr
LOCAL loImgUrlJson
LOCAL loHttp
LOCAL lnNumUrls
LOCAL loUrlJson
LOCAL loImageBytes
LOCAL loFac
LOCAL loSbImageUrl
LOCAL lcExtension
LOCAL loSbLocalFilePath

*  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.
loFbCache = CreateObject('Chilkat_9_5_0.Cache')
*  The cache will use 1 level of 256 sub-directories.
loFbCache.Level = 1
*  Use a directory path that makes sense on your operating system..
loFbCache.AddRoot("C:/fbCache")

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

loRest = CreateObject('Chilkat_9_5_0.Rest')

*  Connect to Facebook.
lnSuccess = loRest.Connect("graph.facebook.com",443,1,1)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loFbCache
    RELEASE loOauth2
    RELEASE loRest
    CANCEL
ENDIF

*  Provide the authentication credentials (i.e. the access key)
loRest.SetAuthOAuth2(loOauth2)

*  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".
loRest.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.
loRest.AddQueryParam("limit","100")

*  Get the 1st page of photos ids.
*  See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
lcResponseJson = loRest.FullRequestNoBody("GET","/v2.7/me/photos")
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loFbCache
    RELEASE loOauth2
    RELEASE loRest
    CANCEL
ENDIF

loPhotoJson = CreateObject('Chilkat_9_5_0.JsonObject')
loSaPhotoUrls = CreateObject('Chilkat_9_5_0.StringArray')
loSbPhotoIdPath = CreateObject('Chilkat_9_5_0.StringBuilder')

loJson = CreateObject('Chilkat_9_5_0.JsonObject')
loJson.EmitCompact = 0
loJson.Load(lcResponseJson)

*  Get the "after" cursor.
lcAfterCursor = loJson.StringOf("paging.cursors.after")
DO WHILE loJson.LastMethodSuccess = 1

    ? "-------------------"
    ? "afterCursor = " + lcAfterCursor

    *  For each photo id in this page...
    i = 0
    lnNumItems = loJson.SizeOfArray("data")
    DO WHILE i < lnNumItems
        loJson.I = i
        lcPhotoId = loJson.StringOf("data[i].id")
        ? "photoId = " + lcPhotoId

        *  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.
        lcPhotoJsonStr = loFbCache.FetchText(lcPhotoId)
        IF (loFbCache.LastMethodSuccess = 0) THEN
            *  It's not locally available, so get it from Facebook..
            loSbPhotoIdPath.Clear()
            loSbPhotoIdPath.Append("/v2.7/")
            loSbPhotoIdPath.Append(lcPhotoId)

            loRest.ClearAllQueryParams()
            loRest.AddQueryParam("fields","id,album,images")

            ? "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.
            lcPhotoJsonStr = loRest.FullRequestNoBody("GET",loSbPhotoIdPath.GetAsString())
            IF (loRest.LastMethodSuccess <> 1) THEN
                ? loRest.LastErrorText
                RELEASE loFbCache
                RELEASE loOauth2
                RELEASE loRest
                RELEASE loPhotoJson
                RELEASE loSaPhotoUrls
                RELEASE loSbPhotoIdPath
                RELEASE loJson
                CANCEL
            ENDIF

            *  Add the photo JSON to the local cache.
            loFbCache.SaveTextNoExpire(lcPhotoId,"",lcPhotoJsonStr)
        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.
        loPhotoJson.Load(lcPhotoJsonStr)
        lcImageUrl = loPhotoJson.StringOf("images[0].source")
        IF (loPhotoJson.LastMethodSuccess = 1) THEN

            *  Actually, we'll add a small JSON document that contains both the image ID and the URL.
            loImgUrlJson = CreateObject('Chilkat_9_5_0.JsonObject')
            loImgUrlJson.AppendString("id",lcPhotoId)
            loImgUrlJson.AppendString("url",lcImageUrl)
            loSaPhotoUrls.Append(loImgUrlJson.Emit())
            ? "imageUrl = " + lcImageUrl
        ENDIF

        i = i + 1
    ENDDO

    *  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.
    loRest.ClearAllQueryParams()
    loRest.AddQueryParam("type","uploaded")
    loRest.AddQueryParam("limit","20")
    loRest.AddQueryParam("after",lcAfterCursor)

    *  Get the next page of photo ids.
    lcResponseJson = loRest.FullRequestNoBody("GET","/v2.7/me/photos")
    IF (loRest.LastMethodSuccess <> 1) THEN
        ? loRest.LastErrorText
        RELEASE loFbCache
        RELEASE loOauth2
        RELEASE loRest
        RELEASE loPhotoJson
        RELEASE loSaPhotoUrls
        RELEASE loSbPhotoIdPath
        RELEASE loJson
        RELEASE loImgUrlJson
        CANCEL
    ENDIF

    loJson.Load(lcResponseJson)
    lcAfterCursor = loJson.StringOf("paging.cursors.after")
ENDDO

? "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.
loHttp = 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.
lnNumUrls = loSaPhotoUrls.Count
i = 0
loUrlJson = CreateObject('Chilkat_9_5_0.JsonObject')

loFac = CreateObject('Chilkat_9_5_0.FileAccess')

DO WHILE i < lnNumUrls
    loUrlJson.Load(loSaPhotoUrls.GetString(i))
    lcPhotoId = loUrlJson.StringOf("id")
    lcImageUrl = loUrlJson.StringOf("url")

    *  Check the local cache for the image data.
    *  Only download and save if not already cached.
    loImageBytes = loFbCache.FetchFromCache(lcImageUrl)
    IF (loFbCache.LastMethodSuccess = 0) THEN
        *   This photo needs to be downloaded.

        loSbImageUrl = CreateObject('Chilkat_9_5_0.StringBuilder')
        loSbImageUrl.Append(lcImageUrl)

        *  Let's form a filename..
        lcExtension = ".jpg"
        IF (loSbImageUrl.Contains(".gif",0) = 1) THEN
            lcExtension = ".gif"
        ENDIF

        IF (loSbImageUrl.Contains(".png",0) = 1) THEN
            lcExtension = ".png"
        ENDIF

        IF (loSbImageUrl.Contains(".tiff",0) = 1) THEN
            lcExtension = ".tiff"
        ENDIF

        IF (loSbImageUrl.Contains(".bmp",0) = 1) THEN
            lcExtension = ".bmp"
        ENDIF

        loSbLocalFilePath = CreateObject('Chilkat_9_5_0.StringBuilder')
        loSbLocalFilePath.Append("C:/Photos/facebook/uploaded/")
        loSbLocalFilePath.Append(lcPhotoId)
        loSbLocalFilePath.Append(lcExtension)

        loImageBytes = loHttp.QuickGet(lcImageUrl)
        IF (loHttp.LastMethodSuccess <> 1) THEN
            ? loHttp.LastErrorText
            RELEASE loFbCache
            RELEASE loOauth2
            RELEASE loRest
            RELEASE loPhotoJson
            RELEASE loSaPhotoUrls
            RELEASE loSbPhotoIdPath
            RELEASE loJson
            RELEASE loImgUrlJson
            RELEASE loHttp
            RELEASE loUrlJson
            RELEASE loFac
            RELEASE loSbImageUrl
            RELEASE loSbLocalFilePath
            CANCEL
        ENDIF

        *  We've downloaded the photo image bytes into memory.
        *  Save it to the cache AND save it to the output file.
        loFbCache.SaveToCacheNoExpire(lcImageUrl,"",loImageBytes)
        loFac.WriteEntireFile(loSbLocalFilePath.GetAsString(),loImageBytes)

        ? "Downloaded to " + loSbLocalFilePath.GetAsString()
    ENDIF

    i = i + 1
ENDDO

? "Finished downloading all Facebook photos!"

RELEASE loFbCache
RELEASE loOauth2
RELEASE loRest
RELEASE loPhotoJson
RELEASE loSaPhotoUrls
RELEASE loSbPhotoIdPath
RELEASE loJson
RELEASE loImgUrlJson
RELEASE loHttp
RELEASE loUrlJson
RELEASE loFac
RELEASE loSbImageUrl
RELEASE loSbLocalFilePath


 

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