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, 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"]