Django Facebook Utils
License: MIT
The intent of this project is to provide some very basic utilities related to Facebook.
At the moment there are only two features:
- An utility that forces the update of an URL in the Facebook's share cache.
- A Context Processor that allows you to hide Facebook Open Graph Protocol <meta> tags from other User Agents.
Test it
# Download and install example
cd /tmp
virtualenv testfacebookutils
cd testfacebookutils
git clone https://github.com/caioariede/django-facebook-utils.git
source bin/activate
cd django-facebook-utils/example
pip install -r requirements.txt
# Test
./manage.py ping_facebook http://example.com --verbosity=2
Installation
pip install django-facebook-utils
or install the master branch:
pip install git+http://github.com/caioariede/django-facebook-utils.git#egg=facebook_utils
How to force an URL to be updated from Facebook's cache
-
The
ping_facebook
command:python manage.py ping_facebook http://example.com
See below How to extend the ping_facebook command to fit your needs.
-
Calling the
ping_facebook
shortcut:from facebook_utils.shortcuts import ping_facebook if ping_facebook('http://example.com'): print('success') else: print('fail')
This shortcut will only return
True
orFalse
. If you need more information to debug, you can callfacebook_utils.utils.ping
or use theping_facebook
command with--verbosity=2
(more verbose).
Detecting Facebook requests
-
The
facebookexternalhit
context processor (for templates):Add
facebook_utils.context_processors.facebookexternalhit
to the TEMPLATE_CONTEXT_PROCESSOR setting insettings.py
:TEMPLATE_CONTEXT_PROCESSORS = ( # .. other stuff 'facebook_utils.context_processors.facebookexternalhit', )
In the template:
{% if facebookexternalhit %} <meta property="og:title" content="The Rock"/> <meta property="og:type" content="movie"/> <meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/> <meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/> {% endif %}
-
The
is_facebookexternalhit
shortcut:from facebook_utils.shortcuts import is_facebookexternalhit def some_view(request): if is_facebookexternalhit(request): return HttpResponse('Hello, Facebook!') return HttpResponse('Hello visitor!')
How to extend the ping_facebook command
You can easily extend the ping_facebook
command to fit your needs.
- First of all, start writing your own custom command.
-
Now extends the
ping_facebook
command:from facebook_utils.management.commands import BasePingCommand
-
Finally, extend it to fit your needs:
class Command(BasePingCommand): help = 'Ping some pages stored in database' def handle(self, **options): verbosity = options.get('verbosity') traceback = options.get('traceback') for page in Page.objects.all(): page_url = page.get_absolute_url() self.do_ping(page_url, verbosity, traceback)