Skip to content

Instantly share code, notes, and snippets.

@alivepea
Created June 20, 2017 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alivepea/10c87b93a308990c05ab0f0744d6d868 to your computer and use it in GitHub Desktop.
Save alivepea/10c87b93a308990c05ab0f0744d6d868 to your computer and use it in GitHub Desktop.
pastebin.com 的命令行脚本
#!/bin/bash
# API Detail: http://pastebin.com/api
url_post='http://pastebin.com/api/api_post.php'
url_login='http://pastebin.com/api/api_login.php'
url_get='http://pastebin.com/raw.php?i='
rc_file=~/.pastebin.rc
data=(
# required parameters
api_dev_key=
api_option="paste" # paste list delete trends userdetails
api_paste_code="frommypastebin"
# optional parameters
api_user_key=
api_paste_name="Watson"
api_paste_format="text"
api_paste_private=1 # 0=public 1=unlisted 2=private
api_paste_expire_date=N # N=Never 10M=10 Minutes 1H=1 Hour 1D=1 Day 1M =1 Month
)
data[0]=$(grep -F "${data[0]}" $rc_file)
data[3]=$(grep -F "${data[3]}" $rc_file)
[[ -z ${data[0]} ]] && echo "Invalide api_dev_key" && exit 1
[[ -z ${data[3]} ]] && data[3]=""
function help()
{
cat << 'EOF'
pastebin - access pastebin.com through CLI
EOF
exit 1;
}
function post_paste()
{
for opt in ${data[*]}; do
opts+=" --data-urlencode $opt"
done
curl -s $opts $url_post
echo
}
function get_paste()
{
paste_key="${1##*/}"
curl -s ${url_get}${paste_key}
echo
}
function list_paste()
{
opts="-d api_option=list"
for opt in "$@"; do
opts+=" -d $opt"
done
curl -s $opts $url_post
echo
}
function delete_paste()
{
api_paste_key=${1##*/}
opts="-d api_option=delete"
opts+=" -d api_paste_key=$api_paste_key"
shift 1
for opt in "$@"; do
opts+=" -d $opt"
done
curl $opts $url_post
echo
}
function gen_user_key()
{
read -p "USERNAME: " user_name
read -p "PASSWD: " -s user_password
opts="-d $1"
opts+=" -d api_user_name=$user_name"
opts+=" -d api_user_password=$user_password"
curl -s $opts $url_login && echo -e "\nadd api_user_key= to $rc_file"
}
while getopts "n:f:p:e:ld:g:u" options; do
case $options in
n)
data[4]="api_paste_name=$OPTARG"
;;
f)
data[5]="api_paste_format=$OPTARG"
;;
p)
data[6]="api_paste_private=$OPTARG"
;;
e)
data[7]="api_paste_expire_date=$OPTARG"
;;
l)
list_flag=1
;;
d)
delete_flag=1
url="$OPTARG"
;;
g)
get_flag=1
url="$OPTARG"
;;
u)
login_flag=1
;;
*)
help ;;
esac
done
shift $(($OPTIND-1))
if [[ -n $get_flag ]]; then
get_paste ${url}
elif [[ -n $list_flag ]]; then
list_paste ${data[0]} ${data[3]}
elif [[ -n $delete_flag ]]; then
delete_paste ${url} ${data[0]} ${data[3]}
elif [[ -n $login_flag ]]; then
gen_user_key ${data[0]}
else
max_size=$((500*1024))
file=$1
if [[ -f $file ]]; then # file
size=$(stat $file | awk '/Size/{print $2}')
if [[ $size -gt $max_size ]]; then
echo "$file too LARGE"
exit 1
fi
data[2]=api_paste_code@"$1"
else # stdin
read -N $((500*1024)) input
data[2]=api_paste_code=$input
fi
post_paste
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment