Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Lianja Web API Examples

Primary Categories

ABN AMRO
AWS Secrets Manager
AWS Security Token Service
AWS Translate
Activix CRM
Adyen
Alibaba Cloud OSS
Amazon Cognito
Amazon DynamoDB
Amazon MWS
Amazon Pay
Amazon Rekognition
Amazon SP-API
Amazon Voice ID
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Backblaze S3
Banco Inter
Belgian eHealth Platform
Bitfinex v2 REST
Bluzone
BrickLink
Bunny CDN
CallRail
CardConnect
Cerved
ClickBank
Clickatell
Cloudfare
Constant Contact
DocuSign
Duo Auth MFA
ETrade
Ecwid
Egypt ITIDA
Egypt eReceipt
Etsy
Facebook
Faire
Frame.io
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Google Translate
Google Vision
Hungary NAV Invoicing
IBM Text to Speech
Ibanity
IntakeQ
JCC Cyprus
Jira
Lightspeed
MYOB
Magento
Mailgun
Malaysia MyInvois

Mastercard
MedTunnel
MercadoLibre
MessageMedia
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Microsoft Teams
Moody's
Okta OAuth/OIDC
OneLogin OIDC
OneNote
OpenAI ChatGPT
PRODA
PayPal
Paynow.pl
Peoplevox
Populi
QuickBooks
Rabobank
Refinitiv
Royal Mail OBA
SCiS Schools Catalogue
SII Chile
SMSAPI
SOAP finkok.com
Salesforce
SendGrid
Shippo
Shopify
Shopware
Shopware 6
SimpleTexting
Square
Stripe
SugarCRM
TicketBAI
TikTok Shop
Trello
Twilio
Twitter API v2
Twitter v1
UPS
UniPin
VoiceBase
Vonage
WaTrend
Walmart v3
Wasabi
WhatsApp
WiX
WooCommerce
WordPress
Xero
Yahoo Mail
Yapily
Yousign
ZATCA
Zendesk
Zoom
_Miscellaneous_
eBay
effectconnect
hacienda.go.cr

 

 

 

(Lianja) 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 Lianja Extension Download

Chilkat Lianja Extension

// 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("CkCache")
// 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("CkOAuth2")
loOauth2.AccessToken = "FACEBOOK-ACCESS-TOKEN"

loRest = createobject("CkRest")

// Connect to Facebook.
llSuccess = loRest.Connect("graph.facebook.com",443,.T.,.T.)
if (llSuccess <> .T.) then
    ? loRest.LastErrorText
    release loFbCache
    release loOauth2
    release loRest
    return
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 <> .T.) then
    ? loRest.LastErrorText
    release loFbCache
    release loOauth2
    release loRest
    return
endif

loPhotoJson = createobject("CkJsonObject")
loSaPhotoUrls = createobject("CkStringArray")
loSbPhotoIdPath = createobject("CkStringBuilder")

loJson = createobject("CkJsonObject")
loJson.EmitCompact = .F.
loJson.Load(lcResponseJson)

// Get the "after" cursor.
lcAfterCursor = loJson.StringOf("paging.cursors.after")
do while loJson.LastMethodSuccess = .T.

    ? "-------------------"
    ? "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 = .F.) 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 <> .T.) then
                ? loRest.LastErrorText
                release loFbCache
                release loOauth2
                release loRest
                release loPhotoJson
                release loSaPhotoUrls
                release loSbPhotoIdPath
                release loJson
                return
            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 = .T.) then

            // Actually, we'll add a small JSON document that contains both the image ID and the URL.
            loImgUrlJson = createobject("CkJsonObject")
            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 <> .T.) then
        ? loRest.LastErrorText
        release loFbCache
        release loOauth2
        release loRest
        release loPhotoJson
        release loSaPhotoUrls
        release loSbPhotoIdPath
        release loJson
        release loImgUrlJson
        return
    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("CkHttp")

// 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("CkJsonObject")

loFac = createobject("CkFileAccess")

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 = .F.) then
        //  This photo needs to be downloaded.

        loSbImageUrl = createobject("CkStringBuilder")
        loSbImageUrl.Append(lcImageUrl)

        // Let's form a filename..
        lcExtension = ".jpg"
        if (loSbImageUrl.Contains(".gif",.F.) = .T.) then
            lcExtension = ".gif"
        endif

        if (loSbImageUrl.Contains(".png",.F.) = .T.) then
            lcExtension = ".png"
        endif

        if (loSbImageUrl.Contains(".tiff",.F.) = .T.) then
            lcExtension = ".tiff"
        endif

        if (loSbImageUrl.Contains(".bmp",.F.) = .T.) then
            lcExtension = ".bmp"
        endif

        loSbLocalFilePath = createobject("CkStringBuilder")
        loSbLocalFilePath.Append("C:/Photos/facebook/uploaded/")
        loSbLocalFilePath.Append(lcPhotoId)
        loSbLocalFilePath.Append(lcExtension)

        loImageBytes = loHttp.QuickGet(lcImageUrl)
        if (loHttp.LastMethodSuccess <> .T.) 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
            return
        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-2024 Chilkat Software, Inc. All Rights Reserved.