Return to Snippet

Revision: 31673
at March 20, 2011 18:15 by ksaver


Updated Code
#!/usr/bin/env python
#
#   shorturl.py
#   Shorts a long URL from command line, using ur1.ca.
#   Shows original page's title and short url.
#
#   ksaver, (at identi.ca); Sep, 2010.
#   version 0.2, Mar 2011.
#   Requieres BeautifulSoup library in order to work properly:
#   http://www.crummy.com/software/BeautifulSoup/
#
#   Public Domain Code.

import sys
import urllib2

from   BeautifulSoup import BeautifulSoup
from   urllib  import urlencode

def shorten(longurl):
    shortener = 'http://ur1.ca/'
    urlparam = {'longurl': longurl}
    encparam = urlencode(urlparam)
    request  = urllib2.Request(shortener, encparam)
    htmlpage = urllib2.urlopen(request).read()
    htmlsoup = BeautifulSoup(htmlpage)
    txtmatch = htmlsoup.p.text.find('http')
    shorturl = htmlsoup.p.text[txtmatch:]
    return shorturl

def main(url):
    htmldoc = urllib2.urlopen(url).read()
    mysoup  = BeautifulSoup(htmldoc)
    title   = mysoup.title.text
    shorturl = shorten(url)
 
    print "'%s': %s\n" % (title, shorturl)
 
if __name__ == '__main__':
    if len(sys.argv) > 1:
        url = sys.argv[1]
    else:
        url = raw_input("URL to short: ")
    main(url)

Revision: 31672
at March 19, 2011 11:35 by ksaver


Updated Code
#!/usr/bin/env python
#
#   shorturl.py
#   Shorts a long URL from command line, using ur1.ca.
#   Shows original page's title and short url.
#   Example:
#   $ ./shorturl.py  http://python.org
#   'Python Programming Language – Official Website': http://ur1.ca/3kkr4
#
#   ksaver, (at identi.ca); Sep, 2010.
#   version 0.2, Mar 2011.
#   Requieres BeautifulSoup library in order to work properly:
#   http://www.crummy.com/software/BeautifulSoup/
#
#   Public Domain Code.

import string
import sys
import urllib2

from   BeautifulSoup import BeautifulSoup
from   urllib  import urlencode

def shorten(longurl):
    shortener =  'http://ur1.ca/'
    urlparam  =  {'longurl': longurl}
    encparam  =  urlencode(urlparam)
    request   =  urllib2.Request(shortener, encparam)
    htmlpage  =  urllib2.urlopen(request).read()
    htmlsoup  =  BeautifulSoup(htmlpage)
    txtmatch  =  string.find(htmlsoup.p.text, 'http')
    shorturl  =  htmlsoup.p.text[txtmatch:]
    return shorturl

def main(url):
    htmldoc  =  urllib2.urlopen(url).read()
    mysoup   =  BeautifulSoup(htmldoc)
    title    =  mysoup.title.text
    shorturl =  shorten(url)
 
    print "'%s': %s\n" % (title, shorturl)
 
if __name__ == '__main__':
    if len(sys.argv) > 1:
        url = sys.argv[1]
    else:
        url = raw_input("URL to short: ")
    main(url)

Revision: 31671
at September 10, 2010 10:52 by ksaver


Updated Code
#!/usr/bin/env python
#
#   shorturl.py
#   Short a long URL from comand line, using ur1.ca.
#
#   ksaver, (at identi.ca); Sep, 2010.
#   Requieres BeautifulSoup library in order to work properly.
#   http://www.crummy.com/software/BeautifulSoup/
#
#   Public Domain Code.

import urllib2
import string
import sys
from   BeautifulSoup import BeautifulSoup
from   urllib import urlencode

def cooksoup(htmlpage):
    return BeautifulSoup(htmlpage)
    
def shorten(longurl):
    shortener = 'http://ur1.ca/'
    webparams = {'longurl': longurl}
    encparams = urlencode(webparams)
    urlreqst  = urllib2.Request(shortener, encparams)
    htmlpage  = urllib2.urlopen(urlreqst).read()
    soup      = cooksoup(htmlpage)
    shorturl  = soup.p.text[string.find(soup.p.text, ':')+1:] #:-S
    return shorturl    
    
def main(argv):
    if len(argv) > 1:
        url = argv[1]
    else:
        url = raw_input("URL to short: ")
    
    shorturl = shorten(url)
    
    print "Short URL: %s" % shorturl
    print

if __name__ == '__main__':
    main(sys.argv)

Revision: 31670
at September 10, 2010 10:46 by ksaver


Initial Code
#!/usr/bin/env python
#
#   shorturl.py
#   Short a long URL from comand line, using ur1.ca.
#
#   ksaver, (at identi.ca); Sep, 2010.
#   Requieres BeautifulSoup library in order to work properly.
#   http://www.crummy.com/software/BeautifulSoup/
#
#   Public Domain Code.
#
import urllib2
import string
import sys
from   BeautifulSoup import BeautifulSoup
from   urllib import urlencode

def cooksoup(htmlpage):
    return BeautifulSoup(htmlpage)
    
def shorten(longurl):
    shortener = 'http://ur1.ca/'
    webparams = {'longurl': longurl}
    encparams = urlencode(webparams)
    urlreqst  = urllib2.Request(shortener, encparams)
    webpage   = urllib2.urlopen(urlreqst).read()
    soup      = cooksoup(webpage)
    shorturl  = soup.p.text[string.find(soup.p.text, ':')+1:] #:-S
    return shorturl    
    
def main(argv):
    if len(argv) > 1:
        url = argv[1]
    else:
        url = raw_input("URL to short: ")
    
    shorturl = shorten(url)
    
    print "Short URL: %s" % shorturl
    print

if __name__ == '__main__':
    main(sys.argv)

Initial URL

                                

Initial Description

                                

Initial Title
shorturl.py: Short a long URL from comand line, in Python

Initial Tags
python

Initial Language
Python