def process_request(self, request): host = request.META['HTTP_HOST'] if host in settings.EXCLUDED_HOSTS: return None request_uri = request.build_absolute_uri() if '/admin/' in request_uri: return None ## do a single check to see if there's anything alphabetical has_alpha = re.compile('[a-zA-Z]+') if has_alpha.search(host) is not None: ## check if someone is hitting their application via a private IP rather than hostname for private in PRIVATES: p = re.compile(private) if p.match(host) is not None: return None ## okay, looks like all those tests were passed, let's go shorten... if BitlyURL.objects.filter(unshorted_url=request_uri).count() < 1: try: newbitly = BitlyURL() newbitly.unshorted_url = request_uri a=Api(login=settings.BITLY_LOGIN,apikey=settings.BITLY_API_KEY) short=a.shorten(request_uri,{'history':1}) newbitly.shortened_url=short newbitly.save() except: pass return None
# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """A wrapper library for Bitly http://bit.ly""" __author__ = "Jair Gaxiola" __email__ = "*****@*****.**" __license__ = "MIT License" __version__ = "0.0.1" from bitly import Api import sys if len(sys.argv) != 5: print "Usage: \n\t python shorten.py 'version' 'longUrl' 'login' 'apiKey'" sys.exit(1) version = sys.argv[1] longUrl = sys.argv[2] login = sys.argv[3] apiKey = sys.argv[4] api = Api(version, login, apiKey) api.shorten(longUrl)