| 1 | package Webqq::Client::Plugin::LinkInfo; | ||
| 2 | use HTML::Parser; | ||
| 3 | use Webqq::Client::Util qw(console); | ||
| 4 | use Date::Parse; | ||
| 5 | use POSIX qw(strftime); | ||
| 6 | use Encode; | ||
| 7 | sub call{ | ||
| 8 | my $client = shift; | ||
| 9 | my $msg = shift; | ||
| 10 | if($msg->{content}=~m#(https?://[^/]+[^\s\x80-\xff]+)#s){ | ||
| 11 | my $url = $1; | ||
| 12 | print "HEAD $url\n" if $client->{debug}; | ||
| 13 | $client->{asyn_ua}->head($url,(),sub{ | ||
| 14 | my $response = shift; | ||
| 15 | return if !$response->is_success; | ||
| 16 | if($response->header("content-type") !~ /text\/html/){ | ||
| 17 | print "$url [not-text/html]\n"; | ||
| 18 | return; | ||
| 19 | } | ||
| 20 | print "GET $url\n" if $client->{debug}; | ||
| 21 | $client->{asyn_ua}->get($url,(),sub{ | ||
| 22 | my $response = shift; | ||
| 23 | return if !$response->is_success; | ||
| 24 | return if $response->header("content-type") !~ /text\/html/; | ||
| 25 | my $charset ; | ||
| 26 | if($response->header("content-type")=~/charset\s*=\s*(utf\-?8|gb2312|gbk|gb18030)/i){ | ||
| 27 | $charset = $1; | ||
| 28 | } | ||
| 29 | elsif($response->content()=~/<meta.*?charset\s*=\s*(utf\-?8|gb2312|gbk|gb18030)/si){ | ||
| 30 | $charset = $1; | ||
| 31 | } | ||
| 32 | else{ | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | return unless defined $charset; | ||
| 37 | console "获取 $url 编码信息[$charset]\n" if $client->{debug}; | ||
| 38 | my $p=HTML::Parser->new; | ||
| 39 | $p->ignore_elements(qw(script style a img)); | ||
| 40 | #$p->report_tags(qw(div p)); | ||
| 41 | $p->utf8_mode(0); | ||
| 42 | |||
| 43 | my $is_title = 0; | ||
| 44 | my $title; | ||
| 45 | my $content; | ||
| 46 | my $expires = $response->header("last-modified"); | ||
| 47 | if(defined $expires){ | ||
| 48 | $expires = strftime('%c',localtime(str2time($expires))); | ||
| 49 | $expires =~s/ \d+时\d+分\d+秒$//; | ||
| 50 | } | ||
| 51 | |||
| 52 | $p->handler(start=>sub{ | ||
| 53 | my $tagname = shift; | ||
| 54 | $is_title=($tagname eq 'title'?1:0); | ||
| 55 | $p->handler(text=>sub{my $text = shift;$is_title?($title .=$text):($content .= $text);},"text"); | ||
| 56 | },"tagname"); | ||
| 57 | |||
| 58 | my $html; | ||
| 59 | if($charset=~/^gb/i){ | ||
| 60 | $html = decode("gb2312",$response->content); | ||
| 61 | } | ||
| 62 | elsif($charset=~/^utf/i){ | ||
| 63 | $html = decode("utf8",$response->content); | ||
| 64 | } | ||
| 65 | $p->parse($html); | ||
| 66 | $p->eof; | ||
| 67 | $title=~s/\s+|&[^&;]+;/ /g; | ||
| 68 | $content=~s/\s+|&[^&;]+;/ /g; | ||
| 69 | |||
| 70 | return unless $title; | ||
| 71 | return unless $content; | ||
| 72 | |||
| 73 | $title = substr($title,0,100) . (length($title)>100?"...":""); | ||
| 74 | $content = substr($content,0,100) . "..."; | ||
| 75 | |||
| 76 | $url = substr($url,0,50) . (length($url)>50?"...":""); | ||
| 77 | |||
| 78 | $title = "【网页标题】" . encode("utf8",$title); | ||
| 79 | $expires = "【更新时间】" . $expires . "\n" if defined $expires; | ||
| 80 | $content = "【网页正文】" . encode("utf8",$content); | ||
| 81 | |||
| 82 | $client->reply_message($msg,"$title\n${expires}$content\n$url"); | ||
| 83 | }); | ||
| 84 | }); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | 1; |