diff --git a/plugins/gitio.py b/plugins/gitio.py new file mode 100644 index 0000000..21dafd1 --- /dev/null +++ b/plugins/gitio.py @@ -0,0 +1,46 @@ +# plugin created by neersighted and lukeroge +from util import hook +import urllib2 + +@hook.command +def gitio(inp): + ".gitio [code] -- Shorten Github URLs with git.io. [code] is a optional custom short code." + split = inp.split(" ") + url = split[0] + + try: + code = split[1] + except: + code = None + + # if the first 8 chars of "url" are not "https://" then append "https://" to the url + if url[:8] != "https://": + url = "https://" + url + + url='url='+str(url) + if code: + url = url + '&code=' + str(code) + req = urllib2.Request(url='http://git.io', data = url) + + # try getting url, catch http error + try: + f = urllib2.urlopen(req) + except urllib2.HTTPError: + return "Failed to get URL!" + urlinfo = str(f.info()) + + # loop over the rows in urlinfo and pick out location and status (this is pretty odd code, but urllib2.Request is weird) + for row in urlinfo.split("\n"): + if row.find("Status") != -1: + status = row + if row.find("Location") != -1: + location = row + + print status + if not "201" in status: + return "Failed to get URL!" + + # this wont work for some reason, so lets ignore it ^ + + # return location, minus the first 10 chars + return location[10:] \ No newline at end of file