Ejemplo n.º 1
0
def shorten(url, server, password, amazon_tag=None):
    if url.startswith("mailto"):
        return url

    logger.debug("Shortening %r" % url)
    url = url.rstrip("/")
    # XXX should use urlparse
    if amazon_tag and "amazon.fr" in url:
        # XXX crappy
        if "?" in url:
            url += "&tag=" + amazon_tag
        else:
            url += "?tag=" + amazon_tag

    req = urllib2.Request(server, headers={"X-Short": password})
    req.get_method = lambda: "POST"
    req.add_data(url)
    try:
        res = urllib2.urlopen(req).read()
    except (socket.error, urllib2.URLError), e:
        logger.debug("Error on the call %r" % url)
        return url
Ejemplo n.º 2
0
def _notag(text):
    return cgi.escape(text)


def shorten(url, server, password, amazon_tag=None):
    if url.startswith("mailto"):
        return url

    logger.debug("Shortening %r" % url)
    url = url.rstrip("/")
    # XXX should use urlparse
    if amazon_tag and "amazon.fr" in url:
        # XXX crappy
        if "?" in url:
            url += "&tag=" + amazon_tag
        else:
            url += "?tag=" + amazon_tag

    req = urllib2.Request(server, headers={"X-Short": password})
    req.get_method = lambda: "POST"
    req.add_data(url)
    try:
        res = urllib2.urlopen(req).read()
    except (socket.error, urllib2.URLError), e:
        logger.debug("Error on the call %r" % url)
        return url
    res = json.loads(res)
    res = server + "/" + res["short"]
    logger.debug("Shortened to %r" % res)
    return res
Ejemplo n.º 3
0
    logger.info('Shortening %r' % url)
    url = url.rstrip('/')
    # XXX should use urlparse
    if amazon_tag and 'amazon.fr' in url:
        # XXX crappy
        if '?' in url:
            url += '&tag=' + amazon_tag
        else:
            url += '?tag=' + amazon_tag

    req = urllib2.Request(server, headers={'X-Short': password})
    req.get_method = lambda: 'POST'
    req.add_data(url)
    old_timeout = socket.getdefaulttimeout()
    socket.setdefaulttimeout(20)
    try:
        res = urllib2.urlopen(req).read()
    except (socket.error, urllib2.URLError), e:
        #if isinstance(e, urllib2.HTTPError):
        #    raise ValueError("Error %s on %s" % (e.code, url))

        logger.info('Error on the call %r' % url)
        return url
    finally:
        socket.setdefaulttimeout(old_timeout)
    res = json.loads(res)
    res = server + '/' + res['short']
    logger.debug('Shortened to %r' % res)
    return res