-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoo.gl.py
More file actions
executable file
·55 lines (39 loc) · 1.17 KB
/
goo.gl.py
File metadata and controls
executable file
·55 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf8 -*-
import json
import sys
import requests
import six
goo_gl = 'https://www.googleapis.com/urlshortener/v1/url'
__doc__ = 'https://developers.google.com/url-shortener/'
def shorten(url):
data = dict(longUrl=url)
headers = {'content-type': 'application/json'}
res = requests.post(goo_gl, data=json.dumps(data), headers=headers)
if res.ok:
return res.json()
else:
res.raise_for_status(res.content)
def expand(shortUrl):
# https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS
params = {'shortUrl': shortUrl}
return _get(params)
def stat(shortUrl):
params = {'shortUrl': shortUrl, 'projection': 'FULL'}
return _get(params)
def _get(params):
res = requests.get(goo_gl, params=params)
if res.ok:
return res.json()
else:
res.raise_for_status(res.content)
def history(shortUrl):
raise NotImplementedError()
if __name__ == '__main__':
if(len(sys.argv) > 1):
url = sys.argv[1]
if 'goo.gl' in url:
six.print_(expand(url)['longUrl'])
else:
six.print_(shorten(url)['id'])
# vim: et sw=4 ts=4