You are not logged in.

#1 2011-04-10 00:30:04

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

cush - command line url shortener

Hey guys,

Over the past few days I've been using nopaste more and more -- it's a great tool for pastebinning, but I always thought the URLs were a bit long. For my own needs I wrote a very simple script that would take the output of nopaste, put it into a URL shortener, and pass out the old and new URLs. A few people expressed to me that they might be interested in this, so I have been polishing it a bit today, and have made it fit for release.

cush accepts input in a few ways:

It can take input from stdin (newline delimiter for URLs):

lspci | nopaste | cush

It can also take input as arguments (space delimiter for URLs):

cush www.one.com www.two.com www.three.com

Cush is available on the AUR:

download

I'd appreciate any feedback you might have! smile

Last edited by neurolysis (2011-05-04 20:17:49)

Offline

#2 2011-04-10 00:35:13

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: cush - command line url shortener

Very useful! Thanks for your contribution! big_smile

Offline

#3 2011-04-10 02:11:55

hauzer
Member
From: Belgrade, Serbia
Registered: 2010-11-17
Posts: 279
Website

Re: cush - command line url shortener

Interesting.


Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
What profit hath a man of all his labour which he taketh under the sun?
All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

Offline

#4 2011-04-10 02:45:18

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: cush - command line url shortener

I've written such a tool for several url shortening services myself, including is.gd.
A little suggestion is that you should not assume URLs to be of http:// protocal. Actually, is.gd doesn't check for protocal at all. You can pass to it "http://google.com" "https://google.com" or "google.com", and it won't complain on any of them.


This silver ladybug at line 28...

Offline

#5 2011-04-10 02:50:23

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: cush - command line url shortener

You might be interested to know about '[[ -t', which will tell you if a given file descriptor is open on a tty, rather than checking for positional parameters -- your script won't just appear to hang without a prompt when a user doesn't supply any parameters, and you can print usage instead.

Example of checking for stdin being open...

checkstdin() {
  if [[ -t 0 ]]; then
    printf 'no data on stdin\n'
  else
    printf 'data found on stdin\n'
    while read -r line; do printf '%s\n' "$line"; done
  fi
}

checkstdin
printf 'foo\nbar\nbaz\n' | checkstdin

Offline

#6 2011-04-10 09:46:50

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

Thanks a lot for the feedback everyone! The following changes have been committed to git (and will be committed to AUR at the end of the day):

Features changed/added:
- Don't assume http protocol
- Better checking for presence of stdin

Bugs fixed:
- You can now specify arguments when taking input from stdin

There is one more bug I want to fix, but I'm trying to work out how best to approach it:

[[ "${_urls[@]: -1}" != "${_url}" ]] && sleep "${_sleeptime-1}"

That checks for the last argument, and doesn't sleep when it is detected to be the same as the one currently being shortened. I'm trying to work out how best to do the same for stdin.

I guess mapfile is the best option. Will try some things out.

Offline

#7 2011-04-10 20:25:17

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

Updated to 1.01, changes:

- No longer assumes HTTP
- Better stdin detection (thanks, falconindy)
- Addition of -- to specify everything following is not an operator
- Sleep detection now working for stdin content

Offline

#8 2011-04-11 23:44:10

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

1.02 removes curl as a dependency (not sure why I used it in the first place when wget works just fine and is in base).

Offline

#9 2011-04-18 00:24:47

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

1.03 shipped, only change is versioning errata.

Offline

#10 2011-04-18 21:35:39

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

1.04 shipped, now with j.mp support! Changelog:

- Add -j/--jmp switches to use j.mp instead of is.gd
- Some performance enhancements
- Now supports configs at /etc/cush.conf and $XDG_CONFIG_HOME/cush/cush.conf

There is a sample config at /usr/share/cush/config, which looks like this:

# Sample cush config file
# /usr/share/cush/config
#
# Global config: /etc/cush.conf
# User config: $XDG_CONFIG_HOME/cush/cush.conf

## Use j.mp shortening by default
#service=jmp

## j.mp username
#jmp_username=foo

## j.mp API key
#jmp_apikey=bar

service - The URL shortening service you want to use by default. Currently supported values are 'isgd' and 'jmp'.
jmp_username - Your username on j.mp
jmp_apikey - Your j.mp API key. Required to use j.mp URL shortening.

Offline

#11 2011-04-24 18:55:28

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

1.05 shipped, now removes perl as a dependency (uses printf for URL encoding).

Offline

#12 2011-04-25 06:38:12

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: cush - command line url shortener

You may want to check out the --data-urlencode option of curl.


This silver ladybug at line 28...

Offline

#13 2011-04-25 16:22:59

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

I'm aware of it -- I don't want to introduce curl as a dependency again.

Offline

#14 2011-04-26 16:04:04

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

1.06 shipped, changelog:

Features:
* Add URL expansion capability ( -e, --expand ), cush can expand URLs you give it with the same syntax you'd use to shorten them.
* Allows reading from the command line and STDIN at the same time

Other
* URL encoding efficiency changes (thanks, falconindy)
* Fixed a bug that would report the service available even if it wasn't

Last edited by neurolysis (2011-04-26 16:05:23)

Offline

#15 2011-05-03 09:14:41

neurolysis
Member
Registered: 2011-02-23
Posts: 112
Website

Re: cush - command line url shortener

1.07 shipped, changelog:

Features:
* RFC 3986 compliance

Offline

Board footer

Powered by FluxBB