def fetch(self, code): resp = self._http_fetch(code) if resp.status == 301: location = resp.getheader("Location") if not location: raise exceptions.ServiceException( "No Location header after HTTP status 301") if resp.reason == "Moved": # Normal bit.ly redirect return location elif resp.reason == "Moved Permanently": # Weird "bundles" redirect, forces connection close despite # sending Keep-Alive header self._conn.close() raise exceptions.CodeBlockedException() else: raise exceptions.ServiceException( "Unknown HTTP reason %s after HTTP status 301" % resp.reason) elif resp.status == 302: location = resp.getheader("Location") if not location: raise exceptions.ServiceException( "No Location header after HTTP status 302") return self._parse_warning_url(code, location) elif resp.status == 403: raise exceptions.BlockedException() elif resp.status == 404: raise exceptions.NoRedirectException() elif resp.status == 410: raise exceptions.CodeBlockedException() else: raise exceptions.ServiceException("Unknown HTTP status %i" % resp.status)
def fetch(self, code): resp = self._http_fetch(code) if resp.status == 200: return self._fetch_200(code) elif resp.status == 301: location = resp.getheader("Location") if not location: raise exceptions.CodeBlockedException( "No Location header after HTTP status 301") tiny = resp.getheader("X-tiny") if tiny and tiny[:3] == "aff": return self._preview(code) return location elif resp.status == 302: raise exceptions.CodeBlockedException() elif resp.status == 404: raise exceptions.NoRedirectException() elif resp.status == 500: # Some "errorhelp" URLs result in HTTP status 500, which goes away when trying a different server self._conn.close() raise exceptions.ServiceException("HTTP status 500") else: raise exceptions.ServiceException("Unknown HTTP status %i" % resp.status) return resp.status
def unexpected_http_status(self, code, resp): if resp.status == 302: location = resp.getheader("Location") if location and ("sharedby" in location or "visibli" in location): raise exceptions.NoRedirectException() elif location and location.startswith("http://yahoo.com"): raise exceptions.BlockedException("Banned (location=%s)" % location) # Guess it be an override for site that busts out iframes return location if resp.status != 200: return super(BaseVisbliService, self).unexpected_http_status(code, resp) resp, data = self._http_get(code) if resp.status != 200: raise exceptions.ServiceException( "HTTP status changed from 200 to %i on second request" % resp.status) match = re.search(r'<iframe id="[^"]+" src="([^"]+)">', data) if not match: if 'Undefined index: HTTP_USER_AGENT' in data: raise exceptions.ServiceException( "Website broken about user-agent") raise exceptions.ServiceException("No iframe url found") url = match.group(1).decode("utf-8") url = HTMLParser.HTMLParser().unescape(url).encode("utf-8") return url
def fetch(self, code): if code == "500": raise exceptions.CodeBlockedException() url = super(Trimnew, self).fetch(code) if url == "http://tr.im/404": raise exceptions.NoRedirectException() return url
def fetch(self, code): resp = self._http_fetch(code) if resp.status == 200: return self._fetch_blocked(code) elif resp.status == 301: location = resp.getheader("Location") if not location: raise exceptions.CodeBlockedException( "No Location header after HTTP status 301") return location elif resp.status == 404: raise exceptions.NoRedirectException() else: raise exceptions.ServiceException("Unknown HTTP status %i" % resp.status)
def fetch(self, code): resp = self._http_head(code) if resp.status in self.http_status_redirect: location = resp.getheader("Location") if not location: raise exceptions.ServiceException( "No Location header after HTTP status 301") return location elif resp.status in self.http_status_no_redirect: raise exceptions.NoRedirectException() elif resp.status in self.http_status_code_blocked: raise exceptions.CodeBlockedException() elif resp.status in self.http_status_blocked: raise exceptions.BlockedException() else: return self.unexpected_http_status(code, resp)
params = {"action": "expand", "shorturl": code, "format": "simple"} try: self._conn.request("GET", self._path + "?" + urllib.urlencode(params)) resp = self._conn.getresponse() data = resp.read() except httplib.HTTPException, e: self._conn.close() raise exceptions.ServiceException("HTTP exception: %s" % e) except socket.error, e: self._conn.close() raise exceptions.ServiceException("Socket error: %s" % e) if resp.status == 200: if data == "not found": raise exceptions.NoRedirectException() return data raise exceptions.ServiceException("Unexpected HTTP status %i" % resp.status) class Bitly(HTTPService): """ http://bit.ly/ """ @property def charset(self): return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_" @property def url(self):