Exemplo n.º 1
0
def fetch(site, bodyvec, encodings):
    """ Fetch the RSS feeds of a website """

    for path in PATHS:
        logging.info("subr_rss: try with %s for %s", path, site)

        del bodyvec[:]
        del encodings[:]
        headers = {}

        result = subr_http.retrieve("GET", "http", site, path, bodyvec,
                                    [], headers)
        if result != 200:
            continue

        ctype = headers.get("content-type")
        if not ctype:
            logging.warning("subr_rss: no content-type")
            continue

        ctype, encoding = subr_http.parse_content_type(ctype)
        if (
            ctype != "application/atom+xml" and
            ctype != "application/rss+xml" and
            ctype != "text/xml" and
            ctype != "application/xml"
           ):
            logging.warning("subr_rss: bad content type: %s", ctype)
            continue

        encodings.append(encoding)
        return 0

    logging.error("subr_rss: can't fetch RSS for %s", site)
    return -1
Exemplo n.º 2
0
def fetch(site, noisy=0):
    """ Fetch RSS feeds of a website """

    for path in PATHS:
        logging.info("subr_rss: try with %s for %s", path, site)

        result = subr_http.fetch(site, path, noisy=noisy)
        if not result:
            continue
        response, body = result

        ctype = response.getheader("Content-Type")
        if not ctype:
            logging.warning("subr_rss: no content-type")
            continue

        ctype, encoding = subr_http.parse_content_type(ctype)
        if (
            ctype != "application/atom+xml" and
            ctype != "application/rss+xml" and
            ctype != "text/xml" and
            ctype != "application/xml"
           ):
            logging.warning("subr_rss: bad content type: %s", ctype)
            continue

        return body, encoding

    logging.error("subr_rss: can't fetch RSS for %s", site)
def shorten(url):
    """ Shorten URLs using bit.ly """

    authdata = readconf()
    if not authdata:
        return

    orig_url = url

    bodyvec = []
    headers = {}

    url = urllib.quote(url, safe="")
    path = "/v3/shorten?login=%s&apiKey=%s&longUrl=%s" % (
      authdata["login"], authdata["api_key"], url)
    result = subr_http.retrieve("GET", "https", "api-ssl.bitly.com",
                                path, bodyvec, [], headers)
    if result != 200:
        logging.warning("subr_bitly.py: can't shorten %s", orig_url)
        return

    body = "".join(bodyvec)

    ctype = headers.get("content-type")
    if not ctype:
        logging.warning("subr_bitly.py: no content type")
        return
    ctype, encoding = subr_http.parse_content_type(ctype)
    if ctype != "application/json":
        logging.warning("subr_bitly.py: bad content type")
        return

    if encoding:
        body = body.decode(encoding)

    dictionary = json.loads(body)
    if not "data" in dictionary or not "url" in dictionary["data"]:
        logging.warning("subr_bitly.py: invalid dictionary")
        return

    return dictionary["data"]["url"]
Exemplo n.º 4
0
def shorten(url, noisy=0):
    """ Shorten URLs using bit.ly """

    authdata = readconf()
    if not authdata:
        return

    orig_url = url

    url = urllib.quote(url, safe="")
    path = "/v3/shorten?login=%s&apiKey=%s&longUrl=%s" % (
      authdata["login"], authdata["api_key"], url)
    result = subr_http.fetch("api-ssl.bitly.com", path, noisy=noisy)
    if not result:
        logging.warning("subr_bitly.py: can't shorten %s", orig_url)
        return

    response, body = result

    ctype = response.getheader("Content-Type")
    if not ctype:
        logging.warning("subr_bitly.py: no content type")
        return
    ctype, encoding = subr_http.parse_content_type(ctype)
    if ctype != "application/json":
        logging.warning("subr_bitly.py: bad content type")
        return

    if encoding:
        body = body.decode(encoding)

    dictionary = json.loads(body)
    if not "data" in dictionary or not "url" in dictionary["data"]:
        logging.warning("subr_bitly.py: invalid dictionary")
        return

    return dictionary["data"]["url"]
Exemplo n.º 5
0
def shorten(url, noisy=0):
    """ Shorten URLs using bit.ly """

    authdata = readconf()
    if not authdata:
        return

    orig_url = url

    url = urllib.quote(url, safe="")
    path = "/v3/shorten?login=%s&apiKey=%s&longUrl=%s" % (
        authdata["login"], authdata["api_key"], url)
    result = subr_http.fetch("api-ssl.bitly.com", path, noisy=noisy)
    if not result:
        logging.warning("subr_bitly.py: can't shorten %s", orig_url)
        return

    response, body = result

    ctype = response.getheader("Content-Type")
    if not ctype:
        logging.warning("subr_bitly.py: no content type")
        return
    ctype, encoding = subr_http.parse_content_type(ctype)
    if ctype != "application/json":
        logging.warning("subr_bitly.py: bad content type")
        return

    if encoding:
        body = body.decode(encoding)

    dictionary = json.loads(body)
    if not "data" in dictionary or not "url" in dictionary["data"]:
        logging.warning("subr_bitly.py: invalid dictionary")
        return

    return dictionary["data"]["url"]