Skip to content

Instantly share code, notes, and snippets.

@DrSkippy
Created March 20, 2012 00:22
Show Gist options
  • Save DrSkippy/2128919 to your computer and use it in GitHub Desktop.
Save DrSkippy/2128919 to your computer and use it in GitHub Desktop.
Lengthen Short URL with service longurl.org
#!/usr/bin/env python
#
# Scott Hendrickson
# 2012-03-18
#
import sys
import re
import urllib
import httplib2
import json
# URLS
r1 = r"(\b(http|https)://([-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]))"
urlRE = re.compile(r1)
#########################
# http://longurl.org/api
http = httplib2.Http()
header = { 'User-Agent' : 'Mozilla/5.0' }
urlsUnrolled = 'http://api.longurl.org/v2/services?format=json'
response, content = http.request(urlsUnrolled, 'GET', headers=header)
# list contains all valid domains - don't try to process invalid domains
domainList = json.loads(content)
def unroll(x):
# x e.g. "http://bit.ly/asdfg"
domain = x.split('/')[2]
if domain in domainList:
u = 'http://api.longurl.org/v2/expand?url='+urllib.quote(x)
response, content = http.request(u, 'GET', headers=header)
# response verification here some day?
tmp = urlRE.search(content)
if tmp is not None:
return tmp.group(0)
else:
return x
else:
return x
##########################
for r in sys.stdin:
# r is some text containing 1 or more urls
for x in urlRE.findall(r.strip()):
print unroll(x[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment