How to convert links to TinyURLs
How to filter your links and convert them to TinyURLs:
import re
import urllib
links_re = re.compile('http://[^\s]+')
def convert_links(text):
def converter(m):
link = m.group(0)
if len(link) <= 30:
return link
else:
params = urllib.urlencode({'url': link})
fp = urllib.urlopen("http://tinyurl.com/api-create.php", params)
return fp.read()
return re.sub(links_re, converter, text)
And an example: print convert_links("This is a test http://amix.dk/blog/viewEntry/19398")
#prints "This is a test http://tinyurl.com/djdy24"
Pretty stupid, but could be useful for some :p |
|