z0r-script, accorciare links da terminale (YOURLS API based)

Pubblicità

Nhoya

Nuovo Utente
Messaggi
45
Reazioni
7
Punteggio
26
Ultimamente ho terminato uno script per un URL shortner che permette di accorciare inserire un link lungo ed ottenerne uno corto,
Dato che utilizza le API di YOURLS ho pensato potesse essere utile anche a chi ha un proprio URL Shortner. (Liberissimi di usarlo così com'è eh :sisilui:)

Link al repo: https://github.com/InforgeNet/z0r-script

v1.7.3 (per le altre versioni fate riferimento al repo)
Codice:
#!/bin/bash

#		Copyright (C) 2015  Nhoya.
#
#   z0r 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 3 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, see <http://www.gnu.org/licenses/>

#text clours
readonly RED="\033[01;31m"
readonly GREEN="\033[01;32m"
readonly BLUE="\033[01;34m"
readonly YELLOW="\033[00;33m"
readonly BOLD="\033[01m"
readonly FINE="\033[0m"

ver='v1.7.3'
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*\.[A-Za-z0-9\+&@%#/=~_|():?]+'
is_z0r='http://z0r.it/[^ \n]'
url=(http://$1)
mode=-s
dep=(curl awk xclip wget)
apikey=4e4b657a91
clip=$(xclip -o 2>/dev/null)
LOGDIR=/$HOME/.z0r_logs
#upload function (long -> short)
shr(){
	url_e=$(echo $1| sed 's/?/%3F/')
	short=$(curl $mode -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&keyword=$2&format=simply&url=$url_e"|awk -F '/' '{print$NF}')
	uc="http://z0r.it/$short"
	if  [ -n "$short" ]; then
		if curl $mode -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&keyword=$2&format=simply&url=$url_e" |grep -q "Errore 406"; then
                	echo -e $RED"Error 406 - URL not valid(try to use https instead of http or try encoding the URL)"$FINE
        	else
			echo "http://z0r.it/$short" && echo "$(date +"%d-%h-%Y %H.%M.%S")	-	 $uc	-	$1">>$LOGDIR
        		echo "$uc" |xclip -selection clipboard
		fi
	else
	if [ -n "$2" ]; then
	echo -e $RED"custom URL already in use"$FINE
	fi
	fi
	}
#expand function (short -> long)
expand(){
	avi=$(echo ${@:2} |awk -F '/' '{print$NF}')
	exp=$(curl $mode -L  "http://z0r.it/yourls-api.php?signature=$apikey&action=expand&title=check_with_z0r_scipt&format=simply&shorturl=$avi")
	echo $exp
	echo "$exp" |xclip -selection clipboard
	}
#help screen
help(){
	echo -e "$YELLOW$BOLD$0$FINE $GREEN$ver$FINE, faster uploader for z0r.it shortner. Based on YOURL API"
	echo "Copyright(C) 2015 Nhoya"
        echo ""
        echo -e $BOLD"  Usage: $0 [long_URL] [custom_name]"$FINE
	echo  "	keep empty custom_name field for normal URL format"
	echo ""
	echo -e $GREEN"======================================================================================"$FINE
	echo " Available Options:"
	echo "		-e <short_URL>				expand URL (short -> long)"
	echo "		-h 					display this page"
	echo "		-c <short_URL>				display numbers of clicks"
	echo "		-d					check dependencies"
	echo "		-l					show shorting history"
	echo "		-m <long_URL1> <long_URL2> ...		mass shorting"
	echo "		-r					reset history"
	echo "		-u					show updates"
	echo -e $GREEN"======================================================================================"$FINE

	}
#stats
stats(){
	lil=$(echo ${@:2} |awk -F '/' '{print$NF}')
	click=$(curl -s -L "http://z0r.it/yourls-api.php?signature=$apikey&action=url-stats&format=json&shorturl=$lil"|grep -Po '"clicks":.*?[^\\]"')
	echo $click
}
mss(){
	for i in ${@:2}; do
		mass=$(curl -s -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&format=simply&url=$(echo $i| sed 's/?/%3F/')"|awk -F '/' '{print$NF}')
	if wget --spider "$i" 2>/dev/null; then
 		if  [ -n "$mass" ] ; then
			if curl $mode -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&format=simply&url=$(echo $i| sed 's/?/%3F/')" |grep -q "Errore 406"; then
                        	echo -e $RED"Error 406 - URL not valid(try to use https instead of http)"$FINE
                	else

				echo "http://z0r.it/$mass" && echo "$(date +"%d-%h-%Y %H.%M.%S")       -        http://z0r.it/$mass   -       $i">>$LOGDIR
			fi
		else
			echo -e $RED"Bad Request"$FINE
		fi
	else
	echo -e $RED"$i is not a valir URL"$FINE
	fi
	done
}
#control over argument
is_url(){
	if [[ $1 =~ $regex ]];then
		if wget --spider "$1" 2>/dev/null; then
		shr $1 $2
		else
		echo -e $RED"Page does not exist"$FINE
		fi
	else
	if [[ $url =~ $regex ]];then
		if wget --spider "$1" 2>/dev/null; then
			shr $1 $2
		else
		echo -e  $RED"Page does not exist"$FINE
		fi
	else
		echo -e  $RED""$1" is not a valid url"$FINE
	fi
	fi
}
#check dipendencies
checkdep(){
	for i in "${dep[@]}"; do
        if  ( ! which $i &>/dev/null ); then
        	echo -e $RED"$i not found, solve the dipendencies before running the script"$FINE
		missing="1"
	fi
	done
}
del_history(){
	echo -e $RED"Are you sure? History can not be restored [y/n]"$FINE
	read input
	case $input in
		[Yy]) echo "" > $LOGDIR
		;;
		*) exit 0
		;;
	esac
}
check_ver(){
	r_ver=$(curl -s "https://api.github.com/repos/InforgeNet/z0r-script/releases" | egrep -m 1 --color 'tag_name":\s*".*"' | cut -d '"' -f 4)
	if [ ! "$r_ver" = "$ver" ]; then
        	echo -e $BLUE"$r_ver"$FINE $GREEN"update found(https://github.com/InforgeNet/z0r-script)"$FINE
		echo -e $GREEN"Want to download the $r_ver release(zipball)?[y/n]"$FINE
		read input
		case $input in
			[Yy]) wget -q --show-progress https://github.com/InforgeNet/z0r-script/zipball/master -O $HOME/z0r-$r_ver.zip
			;;
			*) exit 0
			;;
		esac
	else
		echo -e $RED"No update found"$FINE
fi
}
#option parser
opsparser(){
while getopts ":hvulre:c:m:" opt; do
		case "$opt" in
		h)	help
		exit 0
		;;
		e)	if [[ ${@:2} =~ $is_z0r ]];then
				expand $@
			else
				echo -e  $RED"Enter a valid z0r url(http://z0r.it/...)"$FINE
			fi
			exit 0
		;;
		c)	stats $@
			exit 0
		;;
		m)
			mss $@
			exit 0
		;;
		u)	check_ver
			exit 0
		;;
		v)	echo $ver
			exit 0
		;;
		l)	echo -e $GREEN"====================================================LOGs==============================================="$FINE
			cat $LOGDIR
			echo -e $GREEN"======================================================================================================="$FINE
			exit 0
		;;
		r)	del_history
			exit 0
		;;
		\?) 	echo -e $RED"enter a valid option, use -h for help"$FINE
		exit 0
		;;
		esac
done
}

checkdep
if [ "$missing" == "1" ]; then
        exit 0
fi
opsparser $@
if [ "$#" == "0" ]; then
	is_url $clip
else
	is_url $1 $2
fi

Se trovate problemi per piacere scrivete qui sotto o aprite direttamente un issue su github
 
Pubblicità
Pubblicità
Indietro
Top