Created
January 12, 2011 13:20
-
-
Save alice1017/776157 to your computer and use it in GitHub Desktop.
using google API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#coding:utf-8 | |
import json | |
import usable # <- it's my module. Please look git:770826 | |
import commands | |
import sys | |
import urllib | |
class Getlink: | |
def get(self,url): | |
cmd = """curl -s https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{"longUrl": "%s"}'""" % url | |
self.res = commands.getoutput(cmd) | |
return json.loads(self.res) | |
def expand(self,shorten): | |
url = "https://www.googleapis.com/urlshortener/v1/url" | |
self.key = usable.File("key").readonly().read() | |
param = urllib.urlencode({"key":self.key,"shortUrl":shorten}) | |
res = urllib.urlopen(url+"?"+param).read() | |
return json.loads(res) | |
if __name__ == "__main__": | |
link = Getlink() | |
try: | |
url = sys.argv[1] | |
if url[0:14] == "http://goo.gl/": | |
print link.expand(url)["longUrl"] | |
else: | |
print link.get(url)["id"] | |
except: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment