FacePy, Django, and GraphAPI Response Time

71 views Asked by At

I'm using FacePy to embed posts via a custom template tag in Django; however, the pages where this is used are taking considerably longer to load than those without this tag. I don't see a point in my code that could be causing the hangup, but I might (and probably am, if you review my question history. ;)) missing something totally. The code for the tag is

@register.simple_tag
def new_fb_post_embed(page_id, num_posts):
    try:
        graph = GraphAPI("<< fbKey >>")
        posts = graph.get("/" + page_id + "/feed", retry=1, limit=num_posts)
        new_posts = ""
        for i in range(0, num_posts):
            fb_post_info = posts['data'][i]['id'].split('_')
            new_posts += "<div class='fb-post' data-href='https://www.facebook.com/" + fb_post_info[0] + "/posts/" + fb_post_info[1] + "'></div><br/>"
    except:
        new_posts = "<div><b><h4>Facebook is currently Unavailable</h4></b></div>"

return new_posts    

The tag is used as follows; say grabbing the first three posts from the Intel page:

{% new_fb_post_embed 'Intel' 3 %}
1

There are 1 answers

0
slink On

The answer is:

Attaching cross-site API requests to a template tag is a bad idea.

I modified the above code and placed it in a custom context processor, and had an immediate improvement in page performance.