コード例 #1
0
def update_document_share_url(pk):
    """
    Get the shortened URL for the given Document.

    :arg pk: Primary key of the `Document`.
    """
    if settings.BITLY_API_KEY is None or settings.BITLY_USERNAME is None:
        return

    try:
        doc = Document.objects.get(pk=pk)
    except Document.DoesNotExist:
        log.error('Document not found (pk:%s)' % pk)
        return

    doc_url = absolutify(doc.get_absolute_url())
    try:
        share_url = bitly.shorten(doc_url)['url']
    except (bitly_api.BitlyError, KeyError):
        # If bit.ly service fails or the API key isn't configured.
        log.exception('Bitly URL shortening failed.')
        return

    doc.share_url = share_url
    doc.save(update_fields=['share_url'])
コード例 #2
0
ファイル: jinja_helpers.py プロジェクト: fritexvz/kuma
def bitly_shorten(url):
    """Attempt to shorten a given URL through bit.ly / mzl.la"""
    cache_key = "bitly:%s" % hashlib.md5(smart_str(url)).hexdigest()
    short_url = memcache.get(cache_key)
    if short_url is None:
        try:
            short_url = bitly.shorten(url)["url"]
            memcache.set(cache_key, short_url, 60 * 60 * 24 * 30 * 12)
        except (bitly_api.BitlyError, KeyError):
            # Just in case the bit.ly service fails or the API key isn't
            # configured, fall back to using the original URL.
            return url
    return short_url
コード例 #3
0
def bitly_shorten(url):
    """Attempt to shorten a given URL through bit.ly / mzl.la"""
    cache_key = 'bitly:%s' % hashlib.md5(smart_str(url)).hexdigest()
    short_url = memcache.get(cache_key)
    if short_url is None:
        try:
            short_url = bitly.shorten(url)['url']
            memcache.set(cache_key, short_url, 60 * 60 * 24 * 30 * 12)
        except (bitly_api.BitlyError, KeyError):
            # Just in case the bit.ly service fails or the API key isn't
            # configured, fall back to using the original URL.
            return url
    return short_url