Press "Enter" to skip to content

shell下发推脚本

Be Lazy, 懒得能不动鼠标, 就不愿动鼠标..
今天在推上看到有人的下面缀着"via bash", 好奇怎么做到, 找来推的API, 其实挺简单..
请参看最新的bTwitter.
直接上代码, 不解释了, 只是有几点解释下

1. 如果是VPN, 不用代理, 就取消-x选项
2. 脚本直接从STDIN读取内容,内容需要是UTF-8编码, 如果你的终端不是, 那请在脚本中添加iconv
3. 做到完全脚本浏览/发推也是可以的.
4. 新浪微薄也有REST API, 理论上也是可以这么搞的.

代码:

#!/bin/bash
#by laruence<http://www.laruence.com>
USERNAME="用户名"
PASSWORD="密码"
PROXY="代理地址"
function encodeurl() {
    echo "$*" | awk 'BEGIN {
        split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
        hextab [0] = 0
        for (i=1; i<=255; ++i) {
            ord [ sprintf ("%c", i) "" ] = i + 0
        }
    }
    {
        encoded = ""
        for (i=1; i<=length($0); ++i) {
            c = substr ($0, i, 1)
            if ( c ~ /[a-zA-Z0-9.-$]/ ) {
                encoded = encoded c             #不需要编码
            } else if ( c == " " ) {
                encoded = encoded "+"  #处理空格
            } else {
                #编码
                lo = ord [c] % 16
                hi = int (ord [c] / 16);
                encoded = encoded "%" hextab [hi] hextab [lo]
            }
        }
        print encoded
    }' 2>/dev/null
}
URL=https://api.twitter.com/1/statuses/update.xml
CONTENT=$*
REPLY=`echo $CONTENT | sed -n -e 's/^.*@\([^ ]\+\).*$/\1/p'`
DATA=`encodeurl $CONTENT`
POST="source=bash&status=$DATA"
#in_reply_to老是不能在下面显示出来, 不知何故
if [ -n "$REPLY" ]
then
    POST=$POST"&in_reply_to="$REPLY
fi
MESSAGE=`curl -3 -k --silent -x$PROXY -u$USERNAME:$PASSWORD -d"$POST" $URL`
exit 0

11 Comments

  1. Flo Rence
    Flo Rence July 17, 2019

    Have similar feeling about it. Guess I’m not the only one. It would be just great if you would write some more regarding this topic.

  2. 雪候鸟
    雪候鸟 July 31, 2010

    @mahone 不会, 还需要借助代理, 或者VPN.

  3. mahone
    mahone July 30, 2010

    这样就能绕过GFW了?如果是的话请告之原理,谢谢。
    还是只是一个发帖查看的简单工具?

  4. 雪候鸟
    雪候鸟 July 15, 2010

    @AWu 就是在一条twitter下面会显示 : about * hours ago via web in reply to somebody

  5. AWu
    AWu July 8, 2010

    #in_reply_to老是不能在下面显示出来, 不知何故?
    这个是什么意思?
    如果是转义的话,你可以考虑用单引号~

  6. AWu
    AWu July 8, 2010

    靠谱!
    mark了~

  7. Brilliance
    Brilliance May 24, 2010

    我的UTF8发出去的汉字是***= =!

  8. pityonline
    pityonline May 24, 2010

    贵博代码粘贴后每行都多了行号,还要手动去删除。最好换个能保留文件原来的编辑器……

  9. 雪候鸟
    雪候鸟 May 24, 2010

    @54chen Perl也是Linux标准配备, 顶.

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *