Utils

open_facebook.utils.base64_url_decode_php_style(inp)[source]

PHP follows a slightly different protocol for base64 url decode. For a full explanation see: http://stackoverflow.com/questions/3302946/how-to-base64-url-decode-in-python and http://sunilarora.org/parsing-signedrequest-parameter-in-python-bas

open_facebook.utils.camel_to_underscore(name)[source]

Convert camelcase style naming to underscore style naming

e.g. SpamEggs -> spam_eggs

open_facebook.utils.encode_params(params_dict)[source]

Take the dictionary of params and encode keys and values to ascii if it’s unicode

open_facebook.utils.import_statsd()[source]

Import only the statd by wolph not the mozilla statsd TODO: Move to mozilla statds which is more widely used

open_facebook.utils.is_json(content)[source]

Unfortunately facebook returns 500s which mean they are down Or 500s with a nice error message because you use open graph wrong

So we have to figure out which is which :)

class open_facebook.utils.memoized(func)[source]

Decorator. Caches a function’s return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated).

open_facebook.utils.merge_urls(generated_url, human_url)[source]

merge the generated_url with the human_url following this rules: params introduced by generated_url are kept final params order comes from generated_url there’s an hack to support things like this http://url?param&param=value

>>> gen = "http://mysite.com?p1=a&p2=b&p3=c&p4=d"
>>> hum = "http://mysite.com?p4=D&p3=C&p2=B"
>>> merge_urls(gen, hum)
u'http://mysite.com?p1=a&p2=B&p3=C&p4=D'
>>> gen = "http://mysite.com?id=a&id_s=b&p_id=d"
>>> hum = "http://mysite.com?id=A&id_s=B&p_id=D"
>>> merge_urls(gen, hum)
u'http://mysite.com?id=A&id_s=B&p_id=D'
>>> gen = "http://mysite.com?p1=a&p2=b&p3=c&p4=d"
>>> hum = "http://mysite.com"
>>> merge_urls(gen, hum)
u'http://mysite.com'
>>> gen = "http://ad.zanox.com/ppc/?18595160C2000463397T&zpar4=scrapbook&zpar0=e2494344_c4385641&zpar1=not_authenticated&zpar2=unknown_campaign&zpar3=unknown_ref&ULP=http://www.asos.com/ASOS/ASOS-MARS-Loafer-Shoes/Prod/pgeproduct.aspx?iid=1703516&cid=4172&sh=0&pge=2&pgesize=20&sort=-1&clr=Black&affId=2441"
>>> hum = "http://ad.zanox.com/ppc/?18595160C2000463397T&zpar3=scrapbook&ULP=http://www.asos.com/ASOS/ASOS-MARS-Loafer-Shoes/Prod/pgeproduct.aspx?iid=1703516&cid=4172&sh=0&pge=2&pgesize=20&sort=-1&clr=Black&affId=2441"
>>> merge_urls(gen, hum)
u'http://ad.zanox.com/ppc/?18595160C2000463397T&zpar4=scrapbook&zpar0=e2494344_c4385641&zpar1=not_authenticated&zpar2=unknown_campaign&zpar3=scrapbook&ULP=http://www.asos.com/ASOS/ASOS-MARS-Loafer-Shoes/Prod/pgeproduct.aspx?iid=1703516&cid=4172&sh=0&pge=2&pgesize=20&sort=-1&clr=Black&affId=2441'
>>> gen = "http://mysite.com?invalidparam&p=2"
>>> hum = "http://mysite.com?p=1"
>>> merge_urls(gen, hum)
u'http://mysite.com?invalidparam&p=1'
open_facebook.utils.send_warning(message, request=None, e=None, **extra_data)[source]

Uses the logging system to send a message to logging and sentry

open_facebook.utils.smart_str(s, encoding='utf-8', strings_only=False, errors='strict')[source]

Adapted from django, needed for urlencoding Returns a bytestring version of ‘s’, encoded as specified in ‘encoding’. If strings_only is True, don’t convert (some) non-string-like objects.

open_facebook.utils.start_statsd(path)[source]

Simple wrapper to save some typing

open_facebook.utils.validate_is_instance(instance, classes)[source]

Usage validate_is_instance(10, int) validate_is_instance(‘a’, (str, unicode))