天气查询.pl

sh/bash/dash/ksh/zsh等Shell脚本
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

天气查询.pl

#1

帖子 eexpress » 2010-01-21 10:11

代码: 全选

● cat w-w天气查询.pl 
#!/usr/bin/perl

use Getopt::Long;
GetOptions('1' => \$oneline, 'n'=>\$notify);

#$w=$ARGV[0]?$ARGV[0]:"长沙";
#$_=`w3m -no-cookie -dump http://ipiciq.com/Weather/Default.aspx?q=$w`;
$_=`w3m -dump http://ipiciq.com/Weather/`;

s/.*?°C//s; s/•.*//s;           #去掉前后的无用信息
s/\n/ /g; s/(%|°C)/\1\n/sg;     #规整为每天1行
s/\ +/ /g; s/\ \/\ /\//g;       # 多余的空格
s/^$//mg;
s/^/ ►/mg,s/\n//g if($oneline);
if(/网络故障/){$_="拼音或中文城市名不正确。原提示:".$_;}

if(! $notify){print " ".$w.$_; exit 0;}
/当前:(.*?)\ /s; $str=$1;
my %pic=("晴"=>"32","阴"=>"26","雨"=>"40","雾"=>"19","雪"=>"15",);
foreach (keys %pic){$str=$_,last if($str=~/$_/);}
`notify-send -u critical -i '/home/exp/媒体/图标●/png/天气/$pic{$str}.png' '$w ' \"$_\"`;
32.png
32.png (9.66 KiB) 查看 1468 次
26.png
26.png (7.59 KiB) 查看 1468 次
40.png
40.png (9.22 KiB) 查看 1468 次
19.png
19.png (17.95 KiB) 查看 1468 次
15.png
15.png (9.01 KiB) 查看 1468 次
● 鸣学
头像
ibear
帖子: 787
注册时间: 2006-10-19 8:43
来自: 长江口

Re: 天气查询.pl

#2

帖子 ibear » 2010-01-21 10:37

为什么是32 26 40 19 15

准备翻译成python,perl太鸟语了 :em04
好好地做一个碌碌无为的人
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 天气查询.pl

#3

帖子 eexpress » 2010-01-21 10:53

图片名,就这样啊。我懒得改。lol
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 天气查询.pl

#4

帖子 eexpress » 2010-01-21 10:54

如果你实现相同的功能,欢迎用各种脚本。只是估计,没更简洁的写法了。
● 鸣学
头像
ibear
帖子: 787
注册时间: 2006-10-19 8:43
来自: 长江口

Re: 天气查询.pl

#5

帖子 ibear » 2010-01-21 11:24

想起以前一个python笑话

Q:如何取得一个随机序列
A:请读入一个perl脚本

阿姨别见怪 :em05
好好地做一个碌碌无为的人
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 天气查询.pl

#6

帖子 HuntXu » 2010-01-21 11:42

代码: 全选

#!/usr/bin/perl -w
use strict;

if ( !defined( $ARGV[0] ) ) {
    print "Usage: weather.pl cityname\n";
    exit 1;
}

my $curlcmd = "curl --connect-timeout 5 -s -S";
my $response =
  `$curlcmd -d cityinfo=$ARGV[0] http://search.weather.com.cn/static/url2.php`;

exit 2 if ($?);

$response =~ m[(\d{9})];
if ( $1 == "999999999" ) {
    print "no such a city.\n";
    exit;
}

$response = `$curlcmd http://www.weather.com.cn/html/weather_en/$1.shtml`;
exit 2 if ($?);

my $weather = "Today is ";

$response =~
m[<dt style="float:left; width:300px;">Welcome!&nbsp;&nbsp;&nbsp;&nbsp;(.+) </dt>];
$weather .= "<$1>*";

$response =~ m[<title>(\S+)];
$weather .= " <$1>*";

@_ = ( $response =~ m[<div class="fut_weatherbox">.*?</div>]sg );
if ( @_ < 3 ) {
    print "No forecast available.\n";
    exit;
}
my %match;
for ( 1 .. 3 ) {
    $_ = shift @_;
m{<h3>(?<date>.+?)</h3>.*?<h4[^>]+?>(?<weather>.+)</h4>.*?<h4.+High:(?<hightemp>[^<]+)</h4>.*?<h4.+Low:(?<lowtemp>[^>]+)</h4>}s;
    %match = %+;
    $match{weather} =~ s[<br/>][]g;
    $weather .=
      " $match{date}: $match{weather}, $match{lowtemp} ~ $match{hightemp}*";

}
printf "%s\n", $weather;
贡献一个吧...3天的天气查询,支持城市名拼音、英文和区号的...
HUNT Unfortunately No Talent...
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 天气查询.pl

#7

帖子 eexpress » 2010-01-21 11:44

额。没懂。难道是某FAQ乱翻译的?

胡须,你这啥。这么长。
● 鸣学
头像
missing
帖子: 1470
注册时间: 2008-03-28 20:52
系统: QNX

Re: 天气查询.pl

#8

帖子 missing » 2010-01-21 11:52

好啊,有人来踢馆啦*^o^*
missing is i missing you...
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: 天气查询.pl

#9

帖子 xiooli » 2010-01-21 11:53

代码: 全选

#!/bin/bash
city="$1"; i=0
weather=("晴" "阴" "雨" "雾" "雪" "多云")
icons=("32" "26" "40" "19" "15" "26")
if [ "$city" ]; then
	AGENT="User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030814 Iceweasel/3.0.9 (Debian-3.0.91)"
	url="http://www.google.cn/search?q=tq%20${city}"
	weather_text="$(wget --user-agent="$AGENT" -q "$url" -O - \
	| sed 's|alt=|<>|g;s|title=|<|g;s|</nobr>| |g;s|<[^>]*>||g;s|北京市专业气象台|\n|;s|今日.*|\n&|;s|°C||' \
	| grep "°C")"
	if [ "$weather_text" ]; then
		weather_today="`echo $weather_text|awk -F'"' '{print $2}'`"
		while [ "$i" -lt ${#weather[@]} ]; do
			[ "`echo $weather_today|grep ${weather[$i]}`" ] && icon="${icons[$i]}" && break
			((i++))
		done
		notify-send -u critical -i "./icons/$icon.png" "$city" "`echo "$weather_text" | tr '"' ' ' | sed 's|  | |g;s|°C |&\n|g'`"
	else
		notify-send -u critical "未能获取天气"
	fi
fi
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: 天气查询.pl

#11

帖子 xiooli » 2010-01-21 12:07

perl 就像草书阿,呵呵
张丞相好书而不工。当时流辈皆讥笑之。丞相自若也。一日得句,索笔疾书,满纸龙蛇飞动。使侄录之。当波险处,侄罔然而止。执所书问曰:"此何字也?"丞相熟视久之,亦不自识。诟其侄曰:"汝胡不早问,致余忘之。"
:em05
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 天气查询.pl

#12

帖子 eexpress » 2010-01-21 12:18

xiooli, 那是你没入门。bash于我,是往事了哦。
pl的思路很清晰的。特殊变量晦涩而已。
支持你用bash去复制一个mocp-lrc.pl。论坛搜索吧。 :em04
● 鸣学
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: 天气查询.pl

#13

帖子 xiooli » 2010-01-21 12:32

eexpress 写了:xiooli, 那是你没入门。bash于我,是往事了哦。
pl的思路很清晰的。特殊变量晦涩而已。
支持你用bash去复制一个mocp-lrc.pl。论坛搜索吧。 :em04
我只是小点的玩意用 bash 而已,说起思路清晰可读性好,我认为还是 python 合适些。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 天气查询.pl

#14

帖子 eexpress » 2010-01-21 12:37

支持。那你py来复制下功能啊。我比较下。包括那mocp的脚本
● 鸣学
头像
daf3707
论坛版主
帖子: 12739
注册时间: 2007-06-13 15:57
来自: 在他乡

Re: 天气查询.pl

#15

帖子 daf3707 » 2010-01-21 13:06

:em11
bash的脚本+EE的图标
perl没整出来
:em03
notify-send -u critical -i "./icons/$icon.png"
这文件名加了“倒不显示图片了,不加却可以显示。
回复