分页: 1 / 5

Linux下的飞信~哦也~【可以定制天气短信发送给自己】

发表于 : 2009-01-08 3:47
zm.sonny
Linux FOR fetion~ 现在最新的是到8.10 但是9.04也能装上去~跟Win下的没有什么差别了~包括可以给自己发短信阿 定时短信阿等等~非常的不错哟

还可以给自己定时发送天气预报~或者发送你订阅的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
2. 到中国气象网抓取、过滤天气信息并发送短信的 bash 脚本

你可以从这里下载到下面的 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/&nbsp;//g;s/&deg;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 

3. 将脚本设置为定时执行

安装好 sendsms 到 /usr/bin 之后,将上面脚本放到 YOURPATH 下,然后在命令行执行:crontab -e,将下面一行添加进去:

代码: 全选

50 19 * * * /YOURPATH/weatherman.sh 1> /tmp/weatherman.out 2> /tmp/weatherman.err 

就设置为每天下午 7 点 50 发送天气预报短信。

[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]

Re: Linux下的飞信~哦也~

发表于 : 2009-01-08 8:42
sammysun
这个早就有了阿

Re: Linux下的飞信~哦也~

发表于 : 2009-01-08 8:54
youngerlak
鼓励下小白菜!热情可嘉!

Re: Linux下的飞信~哦也~

发表于 : 2009-01-08 12:16
yinliang108
我还以为我是火星人呢,赶紧进来一看,阿,原来楼主来自火星!!

Re: Linux下的飞信~哦也~

发表于 : 2009-01-08 13:13
tlhl28
恩恩。论坛里之前有人发布过了。
一般我有什么想要的都是先在论坛搜的,这是聚集地嘛,哈哈。

不过 恭喜楼主!!!感谢发布消息。

有火星帖子也说明不断有新人加入阿,也是好事阿! :em01

Re: Linux下的飞信~哦也~

发表于 : 2009-01-08 20:39
leon3
顶火星人,多么积极亚,我就老潜水,

I came across a great view here!

发表于 : 2009-01-09 10:47
vg471448
I came across a great view here!







___________________________________________________________________
Maple Story Mesos
Maple Story Europe Mesos
Maple Story Power Leveling

Re: Linux下的飞信~哦也~

发表于 : 2009-01-09 10:53
rainstone
:em04 :em04

Re: Linux下的飞信~哦也~

发表于 : 2009-01-17 20:20
nsby494
:em06 大家不要伤害年青人的积极性

Re: Linux下的飞信~哦也~

发表于 : 2009-01-17 21:06
lerosua
老婆,出来看上帝啊

Re: Linux下的飞信~哦也~

发表于 : 2009-01-17 22:01
lzw07
谢谢你发布的飞信,我在官方都没 找到

Re: Linux下的飞信~哦也~

发表于 : 2009-01-17 23:57
safiros
真的很强大阿,我很喜欢这个飞信 跟官方的一样的

Re: Linux下的飞信~哦也~

发表于 : 2009-01-18 13:16
080220315
真是太好了,还一直在想用Ubuntu就不能免费发短信了呢,这回好了。

Re: Linux下的飞信~哦也~

发表于 : 2009-01-23 17:46
stephenven
不错,呵呵, 我是地球人,第一次看到这个``` 谢谢分享啦

Re: Linux下的飞信~哦也~

发表于 : 2009-01-30 20:09
njuptsoz
呵呵,搂住的精神还是可嘉的