]> git.sven.stormbind.net Git - sven/scripts.git/blob - home/mtinyurl.tcl
Remove nd.gd service - dead
[sven/scripts.git] / home / mtinyurl.tcl
1 #! /bin/sh
2 # -*- tcl -*- \
3 exec tclsh "$0" ${1+"$@"}
4
5 # Script to create a short url via http://tinyurl.com
6 # and similar services right away from the command line.
7 # Default is http://jbot.de provided by my friend Ralf.
8
9 #   Copyright (c) 2007-2010  Sven <sven@timegate.de>
10 #
11 #   This program is free software; you can redistribute it and/or modify
12 #   it under the terms of the GNU General Public License as published by
13 #   the Free Software Foundation; either version 2 of the License, or
14 #   (at your option) any later version.
15 #
16 #   This program is distributed in the hope that it will be useful,
17 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 #   GNU General Public License for more details.
20 #
21 #   You should have received a copy of the GNU General Public License
22 #   along with this program; if not, write to the Free Software
23 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
24
25 # For updates see http://sven.stormbind.net/misc/
26
27 ### Changelog
28 # Version: 0.4 2014-06-22
29 # Remove nd.gd support - dead
30 #
31 # Version: 0.3 2010-08-27
32 # - Added http://nd.gd as the -n option, currently a pretty stupid
33 #   service which just started linear counting at /01.
34 #
35 # Version: 0.2 2007-04-17
36 #  - made the service chooseable via cmdline options
37 #  - added support for http://jbot.de and made it default
38 #
39 # Version: 0.1 2007-04-15
40 #  - initial release
41
42 ### required external (Debian) packages are:
43 # - tclcurl for TclCurl
44 # - tcllib for htmlparse etc.
45
46 package require TclCurl
47 package require http
48 package require htmlparse
49 package require struct
50 package require cmdline
51
52 #GLOBAL VALUES
53 set VERSION "0.4"
54 set USERAGENT "mtinyurl.tcl version $VERSION by http://sven.stormbind.net/misc"
55
56
57 proc use_t {args_t} {
58     set uri http://tinyurl.com/create.php?      
59     append uri [http::formatQuery url $args_t]
60     set curlopts [http::formatQuery url $args_t]
61     set shortname "http://tinyurl.com"
62     maketiny $uri $curlopts $shortname
63 }
64
65 proc use_j {args_j} {
66     set uri http://jbot.de/create.php
67     set curlopts [http::formatQuery url $args_j]
68     set shortname "http://jbot.de"
69     maketiny $uri $curlopts $shortname
70 }
71
72 proc maketiny {uri curlopts shortname} {
73     global USERAGENT
74     set chandle [curl::init]
75     $chandle configure -url $uri \
76         -useragent $USERAGENT \
77         -autoreferer 1 \
78         -followlocation 1 \
79         -postfields $curlopts \
80         -bodyvar rpage
81     $chandle perform
82     $chandle cleanup
83
84     ::struct::tree t
85     htmlparse::2tree $rpage t
86     htmlparse::removeVisualFluff t
87     htmlparse::removeFormDefs t
88     foreach nodeakt [t children -all [t rootname]] {
89         #puts "DEBUG: [t getall $nodeakt ] NODE: $nodeakt"
90         if {[t get $nodeakt type] == "PCDATA"} {
91             set nodedata [t get $nodeakt data]
92             if {[string match -nocase "*$shortname*" $nodedata]} {
93                 puts $nodedata
94             }
95         }
96     }
97     t destroy
98 }
99
100 proc main {argv0 argv} {
101     regsub -all "(http://)" $argv "" saneargv
102     #puts "DEBUG: $argv0 -- $argv -- $saneargv"
103     set options {
104         {t.arg -1 "Use http://tinyurl.com service to create a short URL"}
105         {j.arg -1 "Use http://jbot.de service to create a short URL (default)"}
106     }
107     set saneargv0 [::cmdline::getArgv0]
108     set usage "$saneargv0 \[options]  URL"
109     if { [catch {array set args [::cmdline::getoptions saneargv $options $usage]}] } {
110         puts $usage
111         puts "-t Use http://tinyurl.com service to create a short URL"
112         puts "-j Use http://jbot.de service to create a short URL (default)"
113         exit 1
114     }
115
116     # Handle -t for http://tinyurl.com
117     if {$args(t) != -1} {
118         use_t $args(t)
119     }
120
121     # Handle -j for http://jbot.de (default)
122     if {$args(j) != -1} {
123         use_j $args(j)
124     }
125
126     # No option given? Fall back to a default service (jbot.de)
127     if {$args(j) == -1 && $args(t) == -1 } {
128         use_j $saneargv
129     }
130
131 }
132
133 main $argv0 $argv