From 0b182c0be2b86ae91722125166de30b92d96154a Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Mon, 17 Jan 2011 18:28:28 +0100 Subject: [PATCH] Import first version of mtinyurl.tcl --- home/mtinyurl.tcl | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 home/mtinyurl.tcl diff --git a/home/mtinyurl.tcl b/home/mtinyurl.tcl new file mode 100755 index 0000000..f05fa10 --- /dev/null +++ b/home/mtinyurl.tcl @@ -0,0 +1,78 @@ +#! /bin/sh +# -*- tcl -*- \ +exec tclsh "$0" ${1+"$@"} + +# Script to create a short url via http://tinyurl.com +# right away from the command line. + +# Copyright (c) 2007 Sven Hoexter +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA + +# For updates see http://sven.stormbind.net/misc/ + +### Changelog +# Version: 0.1 2007-04-15 +# - initial release + +### required external (Debian) packages are: +# - tclcurl for TclCurl +# - tcllib for htmlparse etc. + +package require TclCurl +package require http +package require htmlparse +package require struct + +proc parse {rpage} { + ::struct::tree t + htmlparse::2tree $rpage t + htmlparse::removeVisualFluff t + htmlparse::removeFormDefs t + foreach nodeakt [t children -all [t rootname]] { + #puts "DEBUG: [t getall $nodeakt ] NODE: $nodeakt" + if {[t get $nodeakt type] == "PCDATA"} { + set nodedata [t get $nodeakt data] + if {[string match -nocase "*http://tinyurl.com*" $nodedata]} { + puts $nodedata + puts "" + } + } + } +} + + +proc main {argv} { + #puts "DEBUG: $argv" + regsub -all "(http://)" $argv "" saneargv + #puts "DEBUG: $saneargv" + set useragent "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922" + set uri http://tinyurl.com/create.php? + append uri [http::formatQuery url $saneargv] + #puts "DEBUG: $uri" + + set chandle [curl::init] + $chandle configure -url $uri \ + -useragent $useragent \ + -autoreferer 1 \ + -followlocation 1 \ + -bodyvar rpage + $chandle perform + $chandle cleanup + + parse $rpage +} + +main $argv \ No newline at end of file -- 2.39.5