Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkCacheW.h>
#include <C_CkOAuth2W.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringArrayW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkByteData.h>
#include <C_CkFileAccessW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCacheW fbCache;
HCkOAuth2W oauth2;
HCkRestW rest;
const wchar_t *responseJson;
HCkJsonObjectW photoJson;
HCkStringArrayW saPhotoUrls;
HCkStringBuilderW sbPhotoIdPath;
HCkJsonObjectW json;
int i;
const wchar_t *photoId;
const wchar_t *imageUrl;
const wchar_t *afterCursor;
int numItems;
const wchar_t *photoJsonStr;
HCkJsonObjectW imgUrlJson;
HCkHttpW http;
int numUrls;
HCkJsonObjectW urlJson;
HCkByteData imageBytes;
HCkFileAccessW fac;
HCkStringBuilderW sbImageUrl;
const wchar_t *extension;
HCkStringBuilderW sbLocalFilePath;
success = 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.
fbCache = CkCacheW_Create();
// The cache will use 1 level of 256 sub-directories.
CkCacheW_putLevel(fbCache,1);
// Use a directory path that makes sense on your operating system..
CkCacheW_AddRoot(fbCache,L"C:/fbCache");
// This example assumes a previously obtained an access token
oauth2 = CkOAuth2W_Create();
CkOAuth2W_putAccessToken(oauth2,L"FACEBOOK-ACCESS-TOKEN");
rest = CkRestW_Create();
// Connect to Facebook.
success = CkRestW_Connect(rest,L"graph.facebook.com",443,TRUE,TRUE);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkCacheW_Dispose(fbCache);
CkOAuth2W_Dispose(oauth2);
CkRestW_Dispose(rest);
return;
}
// Provide the authentication credentials (i.e. the access key)
CkRestW_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".
CkRestW_AddQueryParam(rest,L"type",L"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.
CkRestW_AddQueryParam(rest,L"limit",L"100");
// Get the 1st page of photos ids.
// See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
responseJson = CkRestW_fullRequestNoBody(rest,L"GET",L"/v2.7/me/photos");
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkCacheW_Dispose(fbCache);
CkOAuth2W_Dispose(oauth2);
CkRestW_Dispose(rest);
return;
}
photoJson = CkJsonObjectW_Create();
saPhotoUrls = CkStringArrayW_Create();
sbPhotoIdPath = CkStringBuilderW_Create();
json = CkJsonObjectW_Create();
CkJsonObjectW_putEmitCompact(json,FALSE);
CkJsonObjectW_Load(json,responseJson);
// Get the "after" cursor.
afterCursor = CkJsonObjectW_stringOf(json,L"paging.cursors.after");
while (CkJsonObjectW_getLastMethodSuccess(json) == TRUE) {
wprintf(L"-------------------\n");
wprintf(L"afterCursor = %s\n",afterCursor);
// For each photo id in this page...
i = 0;
numItems = CkJsonObjectW_SizeOfArray(json,L"data");
while (i < numItems) {
CkJsonObjectW_putI(json,i);
photoId = CkJsonObjectW_stringOf(json,L"data[i].id");
wprintf(L"photoId = %s\n",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.
photoJsonStr = CkCacheW_fetchText(fbCache,photoId);
if (CkCacheW_getLastMethodSuccess(fbCache) == FALSE) {
// It's not locally available, so get it from Facebook..
CkStringBuilderW_Clear(sbPhotoIdPath);
CkStringBuilderW_Append(sbPhotoIdPath,L"/v2.7/");
CkStringBuilderW_Append(sbPhotoIdPath,photoId);
CkRestW_ClearAllQueryParams(rest);
CkRestW_AddQueryParam(rest,L"fields",L"id,album,images");
wprintf(L"Fetching photo node from Facebook...\n");
// This REST request will continue using the existing connection.
// If the connection was closed, it will automatically reconnect to send the request.
photoJsonStr = CkRestW_fullRequestNoBody(rest,L"GET",CkStringBuilderW_getAsString(sbPhotoIdPath));
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkCacheW_Dispose(fbCache);
CkOAuth2W_Dispose(oauth2);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(photoJson);
CkStringArrayW_Dispose(saPhotoUrls);
CkStringBuilderW_Dispose(sbPhotoIdPath);
CkJsonObjectW_Dispose(json);
return;
}
// Add the photo JSON to the local cache.
CkCacheW_SaveTextNoExpire(fbCache,photoId,L"",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.
CkJsonObjectW_Load(photoJson,photoJsonStr);
imageUrl = CkJsonObjectW_stringOf(photoJson,L"images[0].source");
if (CkJsonObjectW_getLastMethodSuccess(photoJson) == TRUE) {
// Actually, we'll add a small JSON document that contains both the image ID and the URL.
imgUrlJson = CkJsonObjectW_Create();
CkJsonObjectW_AppendString(imgUrlJson,L"id",photoId);
CkJsonObjectW_AppendString(imgUrlJson,L"url",imageUrl);
CkStringArrayW_Append(saPhotoUrls,CkJsonObjectW_emit(imgUrlJson));
wprintf(L"imageUrl = %s\n",imageUrl);
}
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.
CkRestW_ClearAllQueryParams(rest);
CkRestW_AddQueryParam(rest,L"type",L"uploaded");
CkRestW_AddQueryParam(rest,L"limit",L"20");
CkRestW_AddQueryParam(rest,L"after",afterCursor);
// Get the next page of photo ids.
responseJson = CkRestW_fullRequestNoBody(rest,L"GET",L"/v2.7/me/photos");
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkCacheW_Dispose(fbCache);
CkOAuth2W_Dispose(oauth2);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(photoJson);
CkStringArrayW_Dispose(saPhotoUrls);
CkStringBuilderW_Dispose(sbPhotoIdPath);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(imgUrlJson);
return;
}
CkJsonObjectW_Load(json,responseJson);
afterCursor = CkJsonObjectW_stringOf(json,L"paging.cursors.after");
}
wprintf(L"No more pages of photos.\n");
// 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.
http = CkHttpW_Create();
// We'll cache the image data so that if run again, we don't re-download the same image again.
numUrls = CkStringArrayW_getCount(saPhotoUrls);
i = 0;
urlJson = CkJsonObjectW_Create();
imageBytes = CkByteData_Create();
fac = CkFileAccessW_Create();
while (i < numUrls) {
CkJsonObjectW_Load(urlJson,CkStringArrayW_getString(saPhotoUrls,i));
photoId = CkJsonObjectW_stringOf(urlJson,L"id");
imageUrl = CkJsonObjectW_stringOf(urlJson,L"url");
// Check the local cache for the image data.
// Only download and save if not already cached.
success = CkCacheW_FetchFromCache(fbCache,imageUrl,imageBytes);
if (CkCacheW_getLastMethodSuccess(fbCache) == FALSE) {
// This photo needs to be downloaded.
sbImageUrl = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbImageUrl,imageUrl);
// Let's form a filename..
extension = L".jpg";
if (CkStringBuilderW_Contains(sbImageUrl,L".gif",FALSE) == TRUE) {
extension = L".gif";
}
if (CkStringBuilderW_Contains(sbImageUrl,L".png",FALSE) == TRUE) {
extension = L".png";
}
if (CkStringBuilderW_Contains(sbImageUrl,L".tiff",FALSE) == TRUE) {
extension = L".tiff";
}
if (CkStringBuilderW_Contains(sbImageUrl,L".bmp",FALSE) == TRUE) {
extension = L".bmp";
}
sbLocalFilePath = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbLocalFilePath,L"C:/Photos/facebook/uploaded/");
CkStringBuilderW_Append(sbLocalFilePath,photoId);
CkStringBuilderW_Append(sbLocalFilePath,extension);
success = CkHttpW_QuickGet(http,imageUrl,imageBytes);
if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkCacheW_Dispose(fbCache);
CkOAuth2W_Dispose(oauth2);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(photoJson);
CkStringArrayW_Dispose(saPhotoUrls);
CkStringBuilderW_Dispose(sbPhotoIdPath);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(imgUrlJson);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(urlJson);
CkByteData_Dispose(imageBytes);
CkFileAccessW_Dispose(fac);
CkStringBuilderW_Dispose(sbImageUrl);
CkStringBuilderW_Dispose(sbLocalFilePath);
return;
}
// We've downloaded the photo image bytes into memory.
// Save it to the cache AND save it to the output file.
CkCacheW_SaveToCacheNoExpire(fbCache,imageUrl,L"",imageBytes);
CkFileAccessW_WriteEntireFile(fac,CkStringBuilderW_getAsString(sbLocalFilePath),imageBytes);
wprintf(L"Downloaded to %s\n",CkStringBuilderW_getAsString(sbLocalFilePath));
}
i = i + 1;
}
wprintf(L"Finished downloading all Facebook photos!\n");
CkCacheW_Dispose(fbCache);
CkOAuth2W_Dispose(oauth2);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(photoJson);
CkStringArrayW_Dispose(saPhotoUrls);
CkStringBuilderW_Dispose(sbPhotoIdPath);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(imgUrlJson);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(urlJson);
CkByteData_Dispose(imageBytes);
CkFileAccessW_Dispose(fac);
CkStringBuilderW_Dispose(sbImageUrl);
CkStringBuilderW_Dispose(sbLocalFilePath);
}