免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1647 | 回复: 0

利用飞信发送天气预报短信 [复制链接]

论坛徽章:
0
发表于 2009-04-23 23:29 |显示全部楼层

                                                                                                在实现
Tinker-Bot
的天气预报功能时,google 偶然发现了这么一个有趣的工具:命令行下,从电脑发送短信到手机的飞信软件。这个程序是
solrex
写的。原文链接如下:
http://blog.solrex.cn/articles/diy-free-weather-forecast-sms.html

solrex
的这篇文章里,已经把原理讲得很明白了。我就不再赘述。
我用了他的 sendsms 程序(见附件,也可以去 solrex 的博客下载),不过自己写了抓天气的 Perl 脚本。代码如下:
$ cat weather.pl
#!/usr/bin/perl
use strict;
my %weather;
my @date = ("24", "48", "72");
my %user_list = (
    # 各个城市用户的手机号或飞信号,多个用户以逗号相隔(需事先加为飞信好友)
    "南京" => "159*,139*,158*,138*",
    "杭州" => "158*",
    "上海" => "137*,139*",
    "宁波" => "150*",
    "北京" => "134*",
);
my %code_of_city = (
    # http://wap.weather.com.cn 各城市代码
    "南京" => 58238,
    "杭州" => 58457,
    "上海" => 58367,
    "宁波" => 58563,
    "北京" => 54511,
);
foreach my $city (keys %user_list) {
    foreach my $date (@date) {
        my $result = "";
        my @info = `w3m -dump -no-cookie http://wap.weather.com.cn/wap/$code_of_city{$city}/h$date/`;
        if ($date eq "24") {
            $result = "${city}天气\n今天" . join('', @info[4,5,6]);
        } elsif ($date eq "48") {
            $result = "\n明天" . join('', @info[4,5,6,7,8]);
        } elsif ($date eq "72") {
            $result = "\n后天" . join('', @info[4,5,6]);
        }
        $result =~ s/度/°C/g;
        $result =~ s/°C到/-/g;
        $result =~ s/(今天|明天|后天)天气/$1/g;
        $weather{$city} .= $result;
    }
}
open LOG, ">>", "/home/sapiens/log/sendsms.log" or die "Can't write to sendsms.log: $!";
print LOG `date`;
close LOG;
foreach my $city (keys %user_list) {
    `sendsms -v -l -f 159* -p passwd -t $user_list{$city} "$weather{$city}"`;
#    print "To $user_list{$city} in $city:\n$weather{$city}\n";
    sleep 3;
}
此脚本获取的天气信息示例:
南京天气
今天:阵雨转多云
气温:20-12°C
风向风力:东南风4-5级转北风4-5级
明天:晴
气温:20-10°C
风向风力:北风4-5级转3-4级
紫外线指数:中等
穿衣指数:温凉
后天:晴转多云
气温:21-11°C
风向风力:北风3-4级转东风小于3级
先编译安装 sendsms ,然后把这个脚本扔到 crontab 里,就可以每天定时的给一群人发送天气预报啦!
$ crontab -l
0 19 * * * /home/sapiens/bin/weather.pl >> /home/sapiens/log/sendsms.log 2>&1
附: sendsms 程序

       
        文件:sendsms.tar.bz2
        大小:739KB
        下载:
下载
       


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93312/showart_1908632.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP