Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function # Adding also this to the module top.
- import sys
- # I would do it like this, for example:
- '''
- Error handling code:
- Do you think compiling it is excessive? :)
- '''
- # error = compile("print('[!] Error: {0}'.format(e), file=sys.stderr)", '<string>', 'eval')
- error = "print('[!] Error: {0}'.format(e), file=sys.stderr)"
- class GoogleShortener(object):
- """
- Based on:
- https://github.com/avelino/django-googl/blob/master/googl/short.py
- Googl Shortener Implementation
- Doesn't need anything from the app
- """
- api_url = "https://www.googleapis.com/urlshortener/v1/url"
- def short(self, url):
- params = json.dumps({'longUrl': url})
- headers = {'content-type': 'application/json'}
- response = requests.post(self.api_url, data=params,
- headers=headers)
- if response.ok:
- try:
- data = response.json()
- self.shorten = data['id']
- return data['id']
- except Exception, e:
- eval(error)
- def expand(self, url):
- params = {'shortUrl': url}
- response = requests.get(self.api_url, params=params)
- if response.ok:
- try:
- data = response.json()
- return data['longUrl']
- except Exception, e:
- eval(error)
- # Of course if you know what kind of exceptions you get, you can be more explicit.
Advertisement
Add Comment
Please, Sign In to add comment