|
| 1 | +#!/usr/bin/perl |
| 2 | +use strict; |
| 3 | +use JSON; |
| 4 | +use Data::Dumper; |
| 5 | +use Encode; |
| 6 | +use Getopt::Std; |
| 7 | +use Time::HiRes qw(sleep time); |
| 8 | +use MIME::Base64; |
| 9 | +require "dezhi.pl"; |
| 10 | + |
| 11 | +$| = 1; |
| 12 | + |
| 13 | +my %input; |
| 14 | +getopts("c:p:", \%input); |
| 15 | +unless ($input{'c'} && $input{'p'}) { |
| 16 | + print "Usage: $0 -c cookiefile -p passinfofile\n"; |
| 17 | + exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +my ($ua, $cookie_jar) = createua($input{'c'}); |
| 21 | +my($fltDate, $airTax, $fuelTax, $username, $pass, $flightid, $ticketNum, $pass1name, $id1, $pass2name, $id2) = getPassInfo($input{'p'}); |
| 22 | + |
| 23 | +if (-f $input{'c'}) { |
| 24 | + $cookie_jar->load($input{'c'}); |
| 25 | + checkcookies(); |
| 26 | +} |
| 27 | + |
| 28 | +sub checkcookies { |
| 29 | + my $url = "http://www.capitalairlines.com.cn/frontend/users/register/showinfo.action"; |
| 30 | + my $res = $ua->get($url); |
| 31 | + die("code: $res->code(), can't check cookies, get page error\n") unless $res->is_success(); |
| 32 | + dologin() unless ($res->content() =~ /$username/i); |
| 33 | +} |
| 34 | + |
| 35 | +my($userid, $person, $email, $cell); |
| 36 | +getUserInfo(); |
| 37 | + |
| 38 | +#my ($lostcabin, $routesid) = getflight(); |
| 39 | +#while ($lostcabin == -1) { |
| 40 | +# sleep 0.1; |
| 41 | +# ($lostcabin, $routesid) = getflight(); |
| 42 | +#} |
| 43 | +my $ordercontent = ""; |
| 44 | +do { |
| 45 | + $ordercontent = placeorder(); |
| 46 | +} while ($ordercontent !~ /$id1/); |
| 47 | +print "place order ok\n"; |
| 48 | + |
| 49 | +sub dologin { |
| 50 | + my $captcha = ""; |
| 51 | + do { |
| 52 | + $captcha = getcaptcha(); |
| 53 | + } while ($captcha =~ /recap/); |
| 54 | + print "code is $captcha\n"; |
| 55 | + |
| 56 | + my $loginurl = "http://www.capitalairlines.com.cn/login.action"; |
| 57 | + my $loginrefer = "http://www.capitalairlines.com.cn/login.action"; |
| 58 | + my %loginpost = ("userName" => "$username", |
| 59 | + "password" => "$pass", |
| 60 | + "j_captcha_response" => "$captcha"); |
| 61 | + print Dumper(\%loginpost); |
| 62 | + my $dologin = $ua->post($loginurl, \%loginpost); |
| 63 | + if ($dologin->is_success()) { |
| 64 | +#print $dologin->as_string(); |
| 65 | + sleep 1; |
| 66 | + } else { |
| 67 | + print $dologin->as_string(); |
| 68 | + die($dologin->code()."do login failed"); |
| 69 | + } |
| 70 | + my $logindone = $ua->get("http://www.capitalairlines.com.cn/"); |
| 71 | + open(my $fhout, ">", "./cap.html") or die($!); |
| 72 | + if ($logindone->is_success()) { |
| 73 | + $cookie_jar->save(); |
| 74 | + print $fhout $logindone->content(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +sub getflight { |
| 79 | + my($lostcabin, $routesid); |
| 80 | + my $flighturl = "http://www.capitalairlines.com.cn/frontend/groupbuying/groupindex/groupindex!doIndexDetail.action?detailId=$flightid"; |
| 81 | + print "$flighturl\n"; |
| 82 | + my $res = $ua->get("$flighturl"); |
| 83 | + if ($res->is_success()) { |
| 84 | +#print "res ok\n"; |
| 85 | +#print $res->content(); |
| 86 | + my ($flt) = $res->content() =~ /temptitleId"\s*>([^<]*)</; |
| 87 | + my ($routesStr) = $res->content() =~ m/var\s*routesStr\s*=\s*"(.*)";\s*$/m; |
| 88 | + $routesStr =~ s/'/"/g; |
| 89 | + $routesStr =~ s/root/"root"/; |
| 90 | +#print "$routesStr\n"; |
| 91 | + my $jsonobj = decode_json($routesStr); |
| 92 | +#print Dumper(@{ $jsonobj->{'root'} }[0]); |
| 93 | + print "$flt: Current max date ${ $jsonobj->{'root'} }[-1]->{'fltDate'}, status: ${ $jsonobj->{'root'} }[-1]->{'status'}, price:${ $jsonobj->{'root'} }[-1]->{'price'} ".time()."\n\n"; |
| 94 | + foreach (@{ $jsonobj->{'root'} }) { |
| 95 | + if ($_->{'fltDate'} =~ /$fltDate/) { |
| 96 | +#print Dumper($_); |
| 97 | + return (-1, -1) unless $_->{'status'} =~ /OPEN_FOR_SALE/; |
| 98 | + $lostcabin = $_->{'lostcabin'}; |
| 99 | + $routesid = $_->{'id'}; |
| 100 | + my $lastpage = "http://www.capitalairlines.com.cn/frontend/groupbuying/order/grouporder!passengerInfo.action?routesId=$routesid&cabinNum=$lostcabin"; |
| 101 | + print "$lastpage\n"; |
| 102 | + return ($lostcabin, $routesid); |
| 103 | + } |
| 104 | + } |
| 105 | + return (-1, -1); |
| 106 | + } else { |
| 107 | + print $res->code(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +sub getUserInfo { |
| 112 | + my $page = "http://www.capitalairlines.com.cn/frontend/groupbuying/order/grouporder!passengerInfo.action?routesId=7015&cabinNum=15"; |
| 113 | + my $lastres = $ua->get($page); |
| 114 | + my $page = $lastres->content(); |
| 115 | +#($fuelTax) = $page =~ /fuelTax1"\s*type="hidden"\s*value="([\d\.]+)"/; |
| 116 | +#($airTax) = $page =~ /airportTax1"\s*type="hidden"\s*value="([\d\.]+)"/; |
| 117 | +#($username) = $page =~ /"condition\.userLogin"\s*id="contactName"\s*type="hidden"\s*class="ipt1"\s*value="(\w+)"/; |
| 118 | + ($userid) = $page =~ /condition\.userId"\s*id="contactId"\s*type="hidden"\s*class="ipt1"\s*value="(\d+)"/; |
| 119 | + ($person) = $page =~ /condition\.person"\s*id="contactName"\s*type="text"\s*class="ipt1"\s*value="([^"]+)"/; |
| 120 | + ($email) = $page =~ /contactEmail"\s*type="text"\s*class="ipt1"\s*value="([\w@\.]+)"/; |
| 121 | + ($cell) = $page =~ /contactMobile"\s*type="text"\s*class="ipt1"\s*readonly="readonly"\s*value="(\d+)"/; |
| 122 | + print "$fuelTax, $airTax, $username, $userid, $person, $email, $cell\n"; |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +sub getPassInfo { |
| 127 | + my $filename = shift @_ or die("you must supply a filename\n"); |
| 128 | + open(my $fh, "<", $filename); |
| 129 | + while (<$fh>) { |
| 130 | + next if /#/; |
| 131 | + my($fltDate, $airTax, $fuelTax, $username, $pass, $flightid, $ticketNum, $pass1name, $id1, $pass2name, $id2) = split(" ", $_); |
| 132 | + $username = uc($username); |
| 133 | + close($fh); |
| 134 | + print "$fltDate, $airTax, $fuelTax, $username, $pass, $flightid, $ticketNum, $pass1name, $id1, $pass2name, $id2\n"; |
| 135 | + return ($fltDate, $airTax, $fuelTax, $username, $pass, $flightid, $ticketNum, $pass1name, $id1, $pass2name, $id2); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +sub placeorder { |
| 140 | + my %postform = ("routesId" => '5921', |
| 141 | + "airportTax" => $airTax, |
| 142 | + "fuelTax" => $fuelTax, |
| 143 | + "pass1box" => "unchecked", |
| 144 | + "pass2box" => "unchecked", |
| 145 | + "ticketNum" => $ticketNum, |
| 146 | + "pass1.name" => $pass1name, |
| 147 | + "pass1.certificateType" => "NI", |
| 148 | + "pass1.certificateNo" => $id1, |
| 149 | + "pass2.name" => $pass2name, |
| 150 | + "pass2.certificateType" => "NI", |
| 151 | + "pass2.certificateNo" => $id2, |
| 152 | + "condition.userId" => $userid, |
| 153 | + "condition.userLogin" => $username, |
| 154 | + "condition.person" => $person, |
| 155 | + "condition.userEmail" => $email, |
| 156 | + "condition.userMobileNo" => $cell, |
| 157 | + "condition.phone" => "", |
| 158 | + "confirmBox" => "checkbox"); |
| 159 | + print Dumper(\%postform); |
| 160 | + my $posturl = "http://www.capitalairlines.com.cn/frontend/groupbuying/order/grouporder!submitOrder.action"; |
| 161 | + my $res = $ua->post($posturl, \%postform); |
| 162 | + if ($res->is_success()) { |
| 163 | + return $res->content(); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +sub testcookie { |
| 168 | + my $testurl = "http://123.125.104.101/dezhi/cookie.php"; |
| 169 | + $ua->get($testurl); |
| 170 | + my $res2 = $ua->get($testurl); |
| 171 | + print $res2->as_string(); |
| 172 | + exit(); |
| 173 | +} |
| 174 | + |
| 175 | +sub getcaptcha { |
| 176 | + #open(my $fh, ">", "/var/www/html/dezhi/cap.jpg") or die($!); |
| 177 | + my $loginform = "http://www.capitalairlines.com.cn/login.action"; |
| 178 | + my $rand = rand 1; |
| 179 | + my $res = $ua->get($loginform); |
| 180 | + if ($res->is_success()) { |
| 181 | + my $jpgres = $ua->get("http://www.capitalairlines.com.cn/jcaptcha?.tmp=$rand"); |
| 182 | + if ($jpgres->is_success()) { |
| 183 | +#print $fh $jpgres->content(); |
| 184 | + my $img_base64 = encode_base64($jpgres->content()); |
| 185 | +#my %form = ("img" => $jpgres->content()); |
| 186 | + my %form = ("img" => $img_base64); |
| 187 | + $ua->post("http://123.125.104.101/dezhi/img.php?filename=${rand}.jpg", \%form); |
| 188 | + print "http://123.125.104.101/dezhi/img/${rand}.jpg\n"; |
| 189 | + print "enter \"recap\" to get captcha again or enter the captcha code to continue login\n"; |
| 190 | + #close $fh; |
| 191 | + } else { |
| 192 | + die("can't get captcha code\n"); |
| 193 | + } |
| 194 | + } else { |
| 195 | + die("can't get login form\n"); |
| 196 | + print $res->as_string(); |
| 197 | + } |
| 198 | + chomp (my $captcha = <STDIN>); |
| 199 | + return $captcha; |
| 200 | +} |
| 201 | + |
| 202 | +sub testJSON { |
| 203 | + my $routesStr = '{"root":[{"id":"5140","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-21","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"3","endPersons":"3","saleStartDate":"2012-12-07 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-14 23:59:59.0"},{"id":"5144","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-22","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"2","endPersons":"2","saleStartDate":"2012-12-08 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-15 23:59:59.0"},{"id":"5147","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-23","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"2","endPersons":"2","saleStartDate":"2012-12-09 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-16 23:59:59.0"},{"id":"5861","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-24","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"-1","endPersons":"-1","saleStartDate":"2012-12-10 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-17 23:59:59.0"},{"id":"5868","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-25","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"2","endPersons":"2","saleStartDate":"2012-12-11 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-18 23:59:59.0"},{"id":"5875","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-26","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"1","endPersons":"1","saleStartDate":"2012-12-12 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-19 23:59:59.0"},{"id":"5880","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-27","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"2","endPersons":"2","saleStartDate":"2012-12-13 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-20 23:59:59.0"},{"id":"5895","price":"100.00","oprice":"2200.00","discount":"0.45","fltNum":"JD5196","fltDate":"2012-12-28","fltDepTime":"20:35","fltArrTime":"0:35","minPersons":"1","tktNum":"3","endPersons":"3","saleStartDate":"2012-12-12 00:00:00.0","status":"SALE_FINISHED","lostcabin":"0","saleEndDate":"2012-12-21 00:00:00.0"}]}'; |
| 204 | + my $jsonobj = decode_json($routesStr); |
| 205 | + print Dumper($jsonobj); |
| 206 | +} |
0 commit comments