Tag Archive for shorten

Fun.py: Short a url from command line, using fun.ly service

#!/ usr/bin/env python
#
#   fun.py
#   short a url using <a href="http://fun.ly" >http://fun.ly</a> shortener service.
#   ksaver (at identi.ca)
#   Public Domain Code.

import urllib
import urllib2
import sys

from BeautifulSoup import BeautifulSoup as cooksoup

def shorten(longurl):
    uagent    = 'Opera/9.80 (X11; FreeBSD 8.1-RELEASE i386; U; en)
    Presto/2.6.30 Version/10.62'
    headers   = {'User-Agent': uagent}
    shortener = '<a href="http://fun.ly/'" >http://fun.ly/'</a>
    webparams = {'funly': longurl}
    encparams = urllib.urlencode(webparams)
    urlreqst  = urllib2.Request(shortener, encparams, headers)
    htmlpage  = urllib2.urlopen(urlreqst).read()
    soup      = cooksoup(htmlpage)
    shorturl  = soup.findAll('p')[1].text
    return shorturl

def main(argv):
    if len(argv) > 1:
        shorturl = shorten(argv[1])
    else:
        longurl  = raw_input("Give me a URL: ")
        shorturl = shorten(longurl)
        
    print "%s" % shorturl
    
if __name__ == '__main__':
    main(sys.argv)

source

Shorten text class

public class CropText
{
    public CropText()
    {
    }

    public string ShortenText(string input, int MaxLenght, bool DoDots)
    {
        string result = input;
        int InputLength = input.Length;

        string lastChar = result.Substring(result.Length - 1);

        if (MaxLenght < InputLength)
        {
            result = input.Substring(0, MaxLenght);

            if (lastChar == " ")
            {
                result = result.Substring(0, result.Length - 1);
            }

            if (DoDots)
            {
                result += "...";
            }
        }
        return result;
    }
}

source

Simply Ellipse Long Strings

private function ellipsed(fullString:String, cutOff:Number):String
{
if(fullString.length < cutOff)
{
return fullString;
}
else
{

var ellipsedString:String = fullString.substr(0, (cutOff - 3));
ellipsedString += "...";
}

return ellipsedString;
}

source

Shorten text without braking words

/*
returns text limited by a specified length of characters but keeping words intact. the final character count will not be exact since it is affected by the possible removing of the a long word or by the addition of the ellipsis.

paramaters:
string - the input string
chars - the length of characters wanted
elli - the ellipsis to be used, defaults to '...'
*/

function shorten($string='', $chars=20, $elli='...'){
list($new_string, $elli)= explode("
", wordwrap($string, $chars, "
", false));
return  ( $elli ) ? $new_string.'...' : $new_string;
}

source

shortLink v2

#!/usr/bin/perl

package shortLink;

use LWP::UserAgent;
use strict;

my $url = readpipe('zenity --entry --title="shortLink" --text="Please enter a URL to shorten." --entry-text=""');

$shortLink::ua = LWP::UserAgent->new;
my $r = $shortLink::ua->post('http://metamark.net/api/rest/simple', { long_url => $url });
system('zenity --info --text="' . errorCheck($r) . '"');

sub errorCheck {
my $response = shift;
if ( $response->is_success ) {
return $response->content;
} else {
return "There was an error: " . $response->status_line . "
";
}
}

source

shortLink

#!/usr/bin/perl

package shortLink;

use Getopt::Std;
use LWP::UserAgent;
use strict;
use Switch;

our($opt_s, $opt_u);

getopts('s:u:');

help_message() unless $opt_u;
$opt_s = ( !$opt_s ) ? "tinyurl" : $opt_s;

switch ( $opt_s ) {
case "tinyurl" {
makeUA();
my $r = $shortLink::ua->get('http://tinyurl.com/api-create.php?url=' . $opt_u);
print errorCheck($r) . "
";
}
case "metamark" {
makeUA();
my $r = $shortLink::ua->post('http://metamark.net/api/rest/simple', { long_url => $opt_u });
print errorCheck($r) . "
";
}
else {
help_message();
}
}

sub errorCheck {
my $response = shift;
if ( $response->is_success ) {
return $response->content;
} else {
return "There was an error: " . $response->status_line . "
";
}
}

sub help_message {
print "Usage: shortlink [ -s service ] -u url
-s service	Specify URL shortener service to use. Possible values:
tinyurl
metamark
";
}

sub makeUA {
$shortLink::ua = LWP::UserAgent->new;
}

source