2012-04-02 18:17:55 +02:00
|
|
|
# Plugin by neersighted and Lukeroge
|
2012-02-28 05:22:57 +01:00
|
|
|
from util import hook
|
|
|
|
import urllib2
|
|
|
|
|
2012-03-12 19:13:32 +01:00
|
|
|
|
2012-02-28 05:22:57 +01:00
|
|
|
@hook.command
|
|
|
|
def gitio(inp):
|
2012-05-16 05:07:27 +02:00
|
|
|
"gitio <url> [code] -- Shorten Github URLs with git.io. [code] is" \
|
2012-03-23 01:32:48 +01:00
|
|
|
" a optional custom short code."
|
2012-02-28 05:22:57 +01:00
|
|
|
split = inp.split(" ")
|
|
|
|
url = split[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
code = split[1]
|
|
|
|
except:
|
|
|
|
code = None
|
|
|
|
|
2012-03-12 19:13:32 +01:00
|
|
|
# if the first 8 chars of "url" are not "https://" then append
|
|
|
|
# "https://" to the url, also convert "http://" to "https://"
|
2012-02-28 05:22:57 +01:00
|
|
|
if url[:8] != "https://":
|
2012-02-29 09:29:53 +01:00
|
|
|
if url[:7] != "http://":
|
|
|
|
url = "https://" + url
|
|
|
|
else:
|
|
|
|
url = "https://" + url[7:]
|
2012-03-12 19:13:32 +01:00
|
|
|
url = 'url=' + str(url)
|
2012-02-28 05:22:57 +01:00
|
|
|
if code:
|
|
|
|
url = url + '&code=' + str(code)
|
2012-02-29 09:29:53 +01:00
|
|
|
req = urllib2.Request(url='http://git.io', data=url)
|
2012-02-28 05:22:57 +01:00
|
|
|
|
|
|
|
# try getting url, catch http error
|
|
|
|
try:
|
|
|
|
f = urllib2.urlopen(req)
|
|
|
|
except urllib2.HTTPError:
|
|
|
|
return "Failed to get URL!"
|
|
|
|
urlinfo = str(f.info())
|
|
|
|
|
2012-03-12 19:13:32 +01:00
|
|
|
# loop over the rows in urlinfo and pick out location and
|
|
|
|
# status (this is pretty odd code, but urllib2.Request is weird)
|
2012-02-28 05:22:57 +01:00
|
|
|
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
|
2012-02-29 09:29:53 +01:00
|
|
|
return location[10:]
|