<pre name="code" class="python">use LWP::UserAgent;
use URI::Escape;
use Net::Ping;
use JSON qw(encode_json);
use Socket;
use Net::SMTP;
use LWP;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Headers;
use HTTP::Response;
use Encode;
use URI::Escape;
use URI::URL;
use File::Temp qw/tempfile/;
use AE;
my $now = time();
my $ua = LWP::UserAgent->new;
$ua->timeout(5);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
my $cookie_jar = HTTP::Cookies->new(
file => 'lwp_cookies.txt',
autosave => 1,
ignore_discard => 1
);
$ua->cookie_jar($cookie_jar); my $host =
"https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=$now";
my $response = $ua->get($host);
$ua->default_headers; if ( $response->is_success ) {
print $response->decoded_content; # or whatever
$r = $response->decoded_content;
print "\n";
}
else {
die $response->status_line;
} if ( $r =~ /window\.QRLogin\.code = 200; window\.QRLogin\.uuid = "(.*?)"/g ) {
$uuid = $1;
print "\$uuid is $uuid\n";
}; my $api = "https://login.weixin.qq.com/qrcode/$uuid";
my $response = $ua->get($api);
$ua->default_headers;
if ( $response->is_success ) {
$r = $response->decoded_content; # 返回验证码内容
print "\n";
}
else {
die $response->status_line;
} my ( $fh, $filename ) =
tempfile( "weixin_qrcode_XXXX", SUFFIX => ".jpg", DIR => '/tmp' );
binmode $fh;
print $fh $r;
close $fh;
print "登录二维码已经下载到本地 [ $filename ] \n"; #https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=YaZ1MmzJ3Q==&tip=0&r=-$now&_=1453360034431 my $show_tip = 1; sub login {
while (1) {
my $api =
"https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=$uuid&tip=$show_tip&r=-$now&_=$now";
my $response = $ua->get($api);
$ua->default_headers;
if ( $response->is_success ) {
$r = $response->decoded_content;
print "\$r is $r\n";
next unless defined $r;
my %data = $r =~ /window\.(.+?)=(.+?);/g; if ( $data{code} == 201 ) {
print
"手机微信扫码成功,请在手机微信上点击 [登录] 按钮...\n";
$show_tip = 0;
next;
}
elsif ( $data{code} == 200 ) {
print "正在进行登录...\n";
foreach ($r) {
if ( $_ =~ /window.redirect_uri="(.*?)"/ ) {
$api = $1;
print "\$api is $api\n";
my $response = $ua->get($api);
$ua->default_headers;
}
} foreach ($api) {
if ( $_ =~
/https\:\/\/wx\.qq\.com\/cgi\-bin\/mmwebwx-bin\/webwxnewloginpage\?ticket=(.*?)\&uuid=(.*?)\&lang=(.*?)\&scan=(.*)/
)
{
print "\$1 is $1\n";
print "\$2 is $2\n";
print "\$3 is $3\n";
print "\$4 is $4\n";
$ticket=$1;
$user = $4;
print "\$user is $user\n";
return 1;
}
} }
else {
die $response->status_line;
} }
}
};
&login(); #####获取
# $Skey = $1;
# $wxsid = $2;
# $wxuin = $3;
# $pass_ticket = $4; my $api =
"https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=$ticket&uuid=$uuid&lang=zh_CN&scan=$user&fun=new&version=v2&lang=zh_CN";
my $response = $ua->get($api);
$ua->default_headers; if ( $response->is_success ) {
print $response->decoded_content; # or whatever
$r = $response->decoded_content;
if ( $r =~
/\<error.*\<skey\>(.*?)\<\/skey\>\<wxsid\>(.*?)\<\/wxsid\>\<wxuin\>(.*?)\<\/wxuin\>\<pass_ticket\>(.*?)\<\/pass_ticket.*/
)
{
$Skey = $1;
$wxsid = $2;
$wxuin = $3;
$pass_ticket = $4; print "\$Skey is $Skey\n";
print "\$wxsid is $wxsid\n";
print "\$wxuin is $wxuin\n";
print "\$pass_ticket is $pass_ticket\n";
$Skey_x = uri_escape($Skey);
use POSIX;
my $a = "e";
for ( my $b = 0 ; 15 > $b ; $b++ ) {
$a .= POSIX::floor( 10 * rand() );
}
$DeviceID = $a;
} }; sub RUN {
print "启动全局事件循环...\n";
AE::cv->recv;
} my @chatroom_id = (); #my $response= $browser->post("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit", [ "r" => "-$now","lang" => "zh_CN","pass_ticket"=>"$pass_ticket"]);
my $login_url =
"https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-$now&lang=zh_CN&pass_ticket=$pass_ticket";
my $post = {
BaseRequest => {
Uin => $wxuin,
Sid => $wxsid,
Skey => $Skey,
DeviceID => $DeviceID,
}
};
use JSON qw(encode_json);
$json_string = encode_json($post); my $req = HTTP::Request->new(
'POST' => $login_url,
[ 'r' => "-$now", 'lang' => 'zh_CN', 'pass_ticket' => "$pass_ticket" ]
);
$req->referer("https://wx.qq.com/?&lang=zh_CN");
$req->content_type('application/json; charset=UTF-8')
; #post请求,如果有发送参数,必须要有这句
$req->content("$json_string"); #发送post的参数
my $res = $ua->request($req); #print "-------------init-----------------\n";
#print $res->status_line."\n";
#print $res->as_string();#获取的是原始内容,包括响应头,响应正文
print $res->content(); #获取的是响应正文
$d = $res->content(); ##导入 encode_json decode_json
use JSON qw/encode_json decode_json/;
$d = encode_utf8($d);
$d = decode_json($d); use Data::Dumper; my $var = Dumper($d);
print $var; foreach $m ( @{ $d->{ContactList} } ) { #print "----------------\n";
#print $m->{UserName};
if ( $m->{UserName} =~ /\@\@/ ) {
push( @chatroom_id, $m->{UserName} );
}
print "\n"; };
use Data::Dumper; $m = $d->{SyncKey};
@xa = $m->{List};
print "\$m is $m\n";
my $var = Dumper($m);
print $var; print "\@xa is @xa\n";
my $var = Dumper(@xa);
print $var;
$count = $m->{Count}; #print "\$count is $count\n";
for ( $i = 0 ; $i <= $count - 1 ; $i++ ) { #print $m->{List}->[$i]->{Val};
#print $m->{List}->[$i]->{Key};
$synckey .= "$m->{List}->[$i]->{Key}\_$m->{List}->[$i]->{Val}\|"; }; $synckey =~ s/\|$//;
$synckey = uri_escape($synckey);
print "\$synckey is $synckey\n"; #print "SyncKey----------------\n"; $chat_num = $d->{Count}; $name=$d->{User}->{NickName};
print "\$name is $name\n"; $UserName= $d->{User}->{UserName};print $UserName ;
print "\n"; print "-------------init-----------------\n"; sub send_sms {
($msg,$ToUserName,$FromUserName)=@_;
print "\$msg is $msg\n";
print "\$FromUserName is $FromUserName\n";
print "\$ToUserName is $ToUserName\n";
$now= time();
$url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?pass_ticket=$pass_ticket";
my @query_string = (
sid => $wxsid,
skey => $Skey,
r => $now,
pass_ticket => $pass_ticket
);
my $t = $now;
my $post = {
BaseRequest => {
Uin => $wxuin,
Sid => $wxsid,
Skey => $Skey,
DeviceID => $DeviceID,
},
Msg => {
ClientMsgId => $t,
Content => decode("utf8",$msg),
FromUserName => $FromUserName,
LocalID => $t,
ToUserName => $ToUserName,
Type => 1,
},
}; use JSON qw(encode_json);
$json_string = encode_json($post); my $req = HTTP::Request->new(
'POST' => $url,
[
'pass_ticket' => "$pass_ticket"
]
);
# $req->referer("https://wx.qq.com/?&lang=zh_CN");
$req->content_type('application/json; charset=UTF-8')
; #post请求,如果有发送参数,必须要有这句
$req->content("$json_string"); #发送post的参数
my $res = $ua->request($req);
print $res->as_string();
print $res->content();#获取的是响应正文 print "\n"; }; sub get_msg{
if ($recm =~/\s*\@\s*env-*\s*/){
$recm =~ s/\s+//;
$gp =(split(/\@/,"$recm"))[1];
use DBI;
my $dbName = 'oadb';
my $dbUser = 'query';
my $dbUserPass = 'xxxx';
@arr2="";
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ;
my $hostSql = qq{select IP,INFO,ENV from machine_info where ENV='$gp' };
my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);
my $selStmt = $dbh->prepare($hostSql);
$selStmt->bind_columns(undef, \$a1, \$a2, \$a3);
$selStmt->execute();
while( $selStmt->fetch() ){ push (@arr2, "$a1 $a2 $a3\n" );
};
&send_sms("@arr2",$FromUserName,$UserName);}
};
sub get_synccheck {
$now= time(); $url =
"https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?r=$now&skey=$Skey_x&sid=$wxsid&uin=$wxuin&deviceid=$DeviceID&synckey=$synckey&_=$now"; my $response = $ua->get($url);
$ua->default_headers; if ( $response->is_success ) {
$r = $response->decoded_content;
} ##window.synccheck={retcode:"0",selector:"0"}
if ( $r =~ /window.*selector:"(.*?)"}/ ) { $mss = $1;
# print "\$mss is $1\n";
} if ( $mss != 0 ) { $sync_url =
"https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=$wxsid&skey=$Skey&lang=zh_CN&pass_ticket=$pass_ticket"; my @query_string = (
sid => $wxsid,
skey => $Skey,
r => $now,
pass_ticket => $pass_ticket
);
my $post = {
BaseRequest => { Uin => $wxuin, Sid => $wxsid },
SyncKey => $m,
rr => $now
};
use JSON qw(encode_json);
$json_string = encode_json($post); my $req = HTTP::Request->new(
'POST' => $sync_url,
[
'lang' => 'zh_CN',
'pass_ticket' => "$pass_ticket",
'sid' => $wxsid,
'skey' => $Skey
]
);
$req->referer("https://wx.qq.com/?&lang=zh_CN");
$req->content_type('application/json; charset=UTF-8')
; #post请求,如果有发送参数,必须要有这句
$req->content("$json_string"); #发送post的参数
my $res = $ua->request($req);
print "-------------syncinfo-----------------\n"; #print $res->status_line."\n";
#print $res->as_string();#获取的是原始内容,包括响应头,响应正文
#print $res->content();#获取的是响应正文
$d = $res->content();
use JSON qw/encode_json decode_json/;
$d = encode_utf8($d);
$d = decode_json($d);
$m = $d->{SyncKey};
$synckey = "";
#$xa=$d->{SyncKey}->{List};
$count = $m->{Count}; #print "\$count is $count\n";
for ( $i = 0 ; $i <= $count - 1 ; $i++ ) { #print $m->{List}->[$i]->{Val};
#print $m->{List}->[$i]->{Key};
$synckey .= "$m->{List}->[$i]->{Key}\_$m->{List}->[$i]->{Val}\|"; };
###$j每个元素,元素内容为引用
foreach $j (@{$d->{AddMsgList}}) {
use Data::Dumper;
my $var= Dumper($j);
# my $var=encode("gbk",decode("utf8","$var"));
if ("$j->{MsgType}" == 1){
print "\$message is $j->{Content}\n";
print "\n"; ##@d91e48886e46846a143e4dc4f0430ae464469faf3d20c1fc108f34936906789f:<br/>那jiandan
#@734f2197ad7f69b5d6bdc4e7051eead7a95b191d9204861f7232bb496ef456f2:<br/>jj
$recm= $j->{Content}; if ($recm =~/^(\@.+):<br\/>(.*)/g){
$recm=$2;
print "\$recm is $recm\n";
}; $synckey =~ s/\|$//;
$synckey = uri_escape($synckey);
print "\$synckey is $synckey \n"; $FromUserName=$j->{FromUserName};
$ToUserName=$j->{ToUserName};
print "1--------\$FromUserName is $FromUserName\n";
print "2--------\$ToUserName is $ToUserName\n";
if ($FromUserName ne $UserName)
{ print "hihi\n";
&get_msg();
} }else {print "now not support message $j->{MsgType}\n";}
}; }} sub get_event { use AnyEvent;
##定义watch
my $t = AnyEvent->timer(
after => 0,
interval => 5,
cb => \&get_synccheck
);
##不要再每秒打印时间
##undef $t;
my $cv = AnyEvent->condvar;
$cv->recv;
} &get_event();
print "now is blocking\n"; #&RUN();

												

perl 实现微信简版<2>的更多相关文章

  1. 精通Perl(第2版)

    精通Perl(第2版)(通往Perl大师之路必读经典书籍,体现了一种编程思维,能够帮你解决很多实际的问题) [美]brian d foy(布瑞恩·D·福瓦)著   王兴宇 刘宸宇 译 ISBN 978 ...

  2. 微信网页版APP - 网页微信客户端电脑版体验

    微信网页版很早就出来了,解决了很多人上班不能玩手机的问题.微信电脑版-网页微信客户端,直接安装在桌面的微信网页版,免去了开浏览器的麻烦.双击就启动了,和其他的应用程序一样:运行过程中可以隐藏在桌面右下 ...

  3. 微信电脑版-微信for windows客户端发布

    12月份微信Windows版客户端1.0 Alpha推出,昨天微信for windows 1.0客户端(测试版)发布更新,超过三亿人使用的聊天应用,现在登录Windows桌面.你可以在Windows上 ...

  4. 微信 5.3 for iPhone已放出 微信iphone版更新下载

    就在几个小时前,微信发布了更新,本次只放出微信 5.3 for iPhone,距离发布微信5.3内测版也就几天时间.和往常一样微信iphone版先发布,微信android版延后发布,微信看重的是ios ...

  5. 微信安卓版下载 Android微信各版本列表

    前面ytkah弄了一个iso微信各版本列表,现在就来整一个微信 for Android各版本列表,方便大伙下载.每个版本都放出一些新的功能或修复相关错误,详情可以点击下面的版本链接进行查看.资源收集于 ...

  6. 微信电脑版即将到来了 安装QQ浏览器微信版体验吧

    之前说过在手机上微信打字慢,tx最终还是想开了,最近TX邀请测试微信电脑版,想要尝鲜的朋友可以去exp.qq.com申请QQ浏览器微信版体验,不过体验将要结束了,相信正式版很快就要出来了.[微信网页版 ...

  7. 微信电脑版真的要来了 微信Windows版客户端1.0 Alpha推出

    微信电脑版的搜索量一直很大,但只有网页版,之前也写了微信网页版APP - 网页微信客户端电脑版体验,在键盘上打字的感觉就是快.现在微信Windows版客户端1.0 Alpha推出了,来一睹芳容吧(20 ...

  8. 微信电脑版微信1.1 for Windows更新 可@人/转发撤回消息/可播小视频

    微信电脑版微信1.1 for Windows发布更新了,版本号为1.1.0.18,群聊可@人/可转发撤回消息/可播小视频,功能越来越接近微信手机版了. 本次更新的一些新特点: 群聊中可以@人. 消息可 ...

  9. 微信电脑版也能用公众号自定义菜单 微信1.2 for Windows发布

    昨日,微信电脑版发布更新,版本为微信1.2 for Windows,最大的特色就是加入了保存聊天记录功能,可以使用公账号菜单,手机上收藏的表情也能在电脑版上发送,可以接收转账消息. 本次微信pc版更新 ...

随机推荐

  1. [转]Windows平台下LispBox环境搭建

    转自http://www.cnblogs.com/sunt2012/archive/2013/05/27/3102816.html Lisp in a Box软件包可以让新Lisp程序员在一流的Lis ...

  2. Qt中调用PolarSSL库(一)

    最近一直在学习SSL相关的知识,也是先了解理论相关的知识,主要是SSL相关的基本概念和连接建立过程,主要是基于PolarSSL开源库进行学习.学习完了之后就希望能给有所运用,就想用Qt写一个简单的程序 ...

  3. 线程池 异步I/O线程 <第三篇>

    在学习异步之前先来说说异步的好处,例如对于不需要CPU参数的输入输出操作,可以将实际的处理步骤分为以下三步: 启动处理: 实际的处理,此时不需要CPU参数: 任务完成后的处理: 以上步骤如果仅仅使用一 ...

  4. g++ error: expected ‘)’ before ‘*’ token

    原本*号前面的类型是我用typedef自定义的类型的,MyType* const p: 发生这样的错误是,编译器根本不知道MyType是什么东西,这是我在C++多重继承中遇到的.MyType是我在基类 ...

  5. Android APK安装包瘦身[转]

    很显然,APK安装包越小越好.下面从代码,资源文件,使用策略几个方面简要介绍下: 代码 保持良好的编程习惯,不要重复或者不用的代码,谨慎添加libs,移除使用不到的libs. 使用proguard混淆 ...

  6. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.1

    /** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...

  7. Best Time to Buy and Sell Stock II 解答

    Question Say you have an array for which the ith element is the price of a given stock on day i. Des ...

  8. PHP Database ODBC 之 ODBC

    ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数据库). 创建 ODBC ...

  9. Unity 代码规范(PlateFace)1.0版本

    PlateFace Unity开发规范(1.0) 1.项目都采用英文名命名,图片,音频,资源都不能采用中文. 2.方法名: 帕斯卡命名(所有单词首字母大写) 3.字段: 在Unity中组件字段名第一个 ...

  10. ubuntu 文件readonly的问题: W10: Warning: Changing a readonly file 解决办法

    日前,笔者遇到一个奇怪且让人蛋疼的问题,借用别人的话"大家在linux上编辑文件的时候,明明是使用的root登录的,可是这种至高无上的权限在按下i的时候被那串红色错误亵渎了W10: Warn ...