还可以给自己定时发送天气预报~或者发送你订阅的RSS也行哦~~~
天气预报短信一直是移动通信公司提供的一种收费服务,Google 免费天气预报服务打破了这个僵局。但是Google 的服务很不稳定,经常收不到短信,而且天气预报内容的定制性差。
1. 发送飞信的命令行程序[1, 2, 3, 4, 5, 6, 7]
这个程序主要基于邓东东开发的 libfetion 库。这个库不是开源的,但是作者提供了头文件和库文件(在GUI源代码中),所以我们可以使用它的 API 来写一些自己的程序。下面的程序内容很简单,注释也不少,我就只贴源码,不再解释了(注意,编译时需要 curl 的 dev 库)。你可以在这里下载到我的 sendsms 小程序的源代码。
代码: 全选
sendsms
|-- Makefile
|-- include
| |-- common.h
| |-- datastruct.h
| |-- event.h
| |-- fxconfig.h
| `-- libfetion.h
|-- lib
| |-- libfetion_32.a
| `-- libfetion_64.a
|-- sendsms
`-- sendsms.cpp
你可以从这里下载到下面的 bash 脚本,或者到这里下载几乎同样功能的 python 脚本。脚本就不多做解释了,没几行代码,相信稍微研究一下就能看懂。
代码: 全选
$ more weatherman.sh
#!/bin/bash
# This script fetch user specified citys' weather forecast from
# http://weather.com.cn, and send them using a CLI SMS sender "sendsms"
# which you can get from http://share.solrex.cn/dcount/click.php?id=5.
#
# You can look for new or bug fix version
# @ http://share.solrex.cn/scripts/weatherman.sh.
# Copyright (C) Solrex Yang <http://solrex.cn> with GPL license.
#
# Usage: You should add it to crontab by "crontab -e", and then add a line
# such as:
# 00 20 * * * /usr/bin/weatherman.sh >> ~/bin/log/weatherman.log 2>&1
# which will send weather forecast to your fetion friends at every 8pm.
CITY_LIST=("南京" "北京" "郑州")
URL_LIST=("101190101" "101010100" "101180101")
SMS_USER=("135xxxxxxxx" "136xxxxxxxx,137xxxxxxxx")
SMS_CITY=("郑州" "北京")
URLBASE="http://www.weather.com.cn/html/weather/"
get_html()
{
i=0
for city in ${CITY_LIST[*]}; do
url=$URLBASE${URL_LIST[i]}.shtml
#wget -e "http_proxy=http://user:[email protected]:8080" -O $city.txt $url
wget -nv -O $city.txt $url 2> /dev/null
i=$(($i+1))
done
}
parse_html()
{
for city in ${CITY_LIST[*]}; do
grep -q "18:00" $city.txt
# Select useful part.
NOT18=$?
if [ $NOT18 -eq 0 ]; then
sed -i -e '1,/c_1_1/d;/surf/,$d;' $city.txt
sed -i -e '/dl class="right"/,/dd_0/d;' $city.txt
else
sed -i -e '1,/c_1_1/d;/box_hist/,$d;' $city.txt
sed -i -e '/dl class="right"/,/c_1_2/d;s/<br \/>.*<\/dd>//g;' $city.txt
fi
# Remove HTML tags.
sed -i -e 's/<[^>]*>//g;/<!--/d' $city.txt
# Remove empty lines.
sed -i -e 's/ //g;s/°C//g;s/^\s*//g;/^$/d' $city.txt
sed -i -e '14,$d;' $city.txt
# Cut verbose words.
sed -i -e 's/℃//g;s/高温://g;s/低温://g;s/指数//g;' $city.txt
sed -i -e 's/星期/周/g;s@/@\n@g;s/[ \t\r]*//g;s/:/:/g;' $city.txt
# Format file content to SMS.
LANG=zh_CN.UTF-8
if [ ${NOT18} -eq 0 ]; then
MES="${city}(18:00发布)\n"
MES=$MES`date -d tomorrow +%-d`日周`date -d tomorrow +%a`:
MES=$MES`sed -n -e '1p' $city.txt`,
MES=$MES`sed -n -e '2p' $city.txt`到`sed -n -e '3p' $city.txt`度,
MES=$MES`sed -n -e '4p' $city.txt`'\n'
MES=$MES`sed -n -e '5p' $city.txt`:`sed -n -e '6p' $city.txt`,
MES=$MES`sed -n -e '8p' $city.txt`到`sed -n -e '7p' $city.txt`度,
MES=$MES`sed -n -e '9p' $city.txt`'\n'
MES=$MES`sed -n -e '10p' $city.txt`:`sed -n -e '11p' $city.txt`,
MES=$MES`sed -n -e '13p' $city.txt`到`sed -n -e '12p' $city.txt`度,
MES=$MES`sed -n -e '14p' $city.txt`
else
MES="${city}(8:00发布)\n"
MES=$MES今天白天:
MES=$MES`sed -n -e '1p' $city.txt`,
MES=$MES`sed -n -e '2p' $city.txt`到`sed -n -e '3p' $city.txt`度,
MES=$MES`sed -n -e '4p' $city.txt`'\n'
MES=$MES`sed -n -e '5p' $city.txt`'\n'
MES=$MES`sed -n -e '6p' $city.txt`'\n'
MES=$MES`sed -n -e '7p' $city.txt`'\n'
MES=$MES`sed -n -e '8p' $city.txt`'\n'
MES=$MES`sed -n -e '10p' $city.txt`'\n'
MES=$MES`sed -n -e '11p' $city.txt`
fi
echo -ne $MES > $city.txt
done
}
send_forcast()
{
i=0
for user in ${SMS_USER[*]}; do
sendsms -vlf 13xxxxxxxxx -p **** -t ${SMS_USER[$i]} < ${SMS_CITY[$i]}.txt
sleep 1
i=$(($i+1))
done
}
clear_html()
{
for city in ${CITY_LIST[*]}; do
rm -f $city.txt
done
}
get_html
parse_html
send_forcast
#clear_html
安装好 sendsms 到 /usr/bin 之后,将上面脚本放到 YOURPATH 下,然后在命令行执行:crontab -e,将下面一行添加进去:
代码: 全选
50 19 * * * /YOURPATH/weatherman.sh 1> /tmp/weatherman.out 2> /tmp/weatherman.err
[1] 应大家要求,在程序中加入了读取 http_proxy 代理服务器环境变量的部分,其它类型的代理服务器可以自行添加(毕竟源代码给你了,随便改)。
[2] 应读者要求,增加了重试登录和发送的代码。
[3] 2008 年 11 月 30 日:增加了群发短信功能(多个接收者用','分隔);根据天气网的升级更新了 weatherman.sh(.py 不再维护了)。
[4] 2008 年 12 月 21 日:用新版本的飞信库更新了源代码包中的库,减少了程序运行时库函数产生的调试输出。
[5] 2009 年 01 月 11 日:增加从标准输入读入信息支持,可使用管道和输入重定向。这篇博客中的代码就不更新了,请到给出的链接去下载新版本。
[6] 2009 年 2 月 21 日:用新版本的飞信库(0.9.2)更新了源代码包中的库。
[7] 2009 年 4 月 17 日:添加了"-l"选项,支持长短信发送,最长可到 1024 字节。解决了一个从标准输入读取短信的 bug。
OVER
大家需要的话就去下面的网址~
这个是Linux版下载界面的网址~
http://www.libfetion.cn/Linux_demoapp_download.html[/size]