I want all albums from facebook and get the src property of the cover photo. This is where it became tricky. I wrote the following code to do this, but i'm wondering if this can't be done in a more efficient way?
var facebookClient = new FacebookClient(accessToken);
var sw = Stopwatch.StartNew();
dynamic facebookAlbums = facebookClient.Get("/fql",
new
{
q = new
{
coverPids = "select name, link,aid, cover_pid from album where owner = 56615007038 AND photo_count > 0 ORDER BY created desc",
coverSrcs = "select src, src_big,aid from photo where pid in (select cover_pid from #coverPids)"
}
});
sw.Stop();
Console.WriteLine("fql query took: " + sw.ElapsedMilliseconds);
sw.Restart();
var coverPids = ((Facebook.JsonArray)facebookAlbums.data).Cast<dynamic>().FirstOrDefault(i => i.name == "coverPids");
var coverSrcs = ((Facebook.JsonArray)facebookAlbums.data).Cast<dynamic>().FirstOrDefault(i => i.name == "coverSrcs");
if (coverPids != null && coverSrcs != null && coverPids.fql_result_set != null && coverSrcs.fql_result_set != null)
{
var coverSrcsFqlResultSet = coverSrcs.fql_result_set as IEnumerable<dynamic>;
var coverPidsFqlResultSet = coverPids.fql_result_set as IEnumerable<dynamic>;
var test = coverPidsFqlResultSet.Join(coverSrcsFqlResultSet, album => album.aid, photo => photo.aid, (album, photo) => new
{
name = album.name,
link = album.link,
aid = album.aid,
cover_pid = album.cover_pid,
src = photo.src,
src_big = photo.src_big
});
}
sw.Stop();
Console.WriteLine("processing took: " + sw.ElapsedMilliseconds);
Console.ReadLine();
The times are now as follows when performing this 10 times in a loop:
- fql query took: 717
- processing took: 204
- fql query took: 304
- processing took: 0
- fql query took: 265
- processing took: 0
- fql query took: 251
- processing took: 0
- fql query took: 220
- processing took: 0
- fql query took: 237
- processing took: 0
- fql query took: 225
- processing took: 0
- fql query took: 224
- processing took: 0
- fql query took: 225
- processing took: 0
- fql query took: 219
- processing took: 0