%PDF- %PDF-
Direktori : /data/www_bck/varak.net_bck/ingress.varak.net/api/ |
Current File : //data/www_bck/varak.net_bck/ingress.varak.net/api/get3.php |
<?php function getmicrotime() { list($usec,$sec)=explode(" ",microtime()); return ((float)$usec+(float)$sec); } ini_set('mongo.native_long', 1); error_reporting(0); require("config.php"); $spojeni = mysql_connect($srv, $usr, $pwd); mysql_select_db($dbname, $spojeni); header("Content-type: text/plain"); $total_count = 0; $cities = array(); // Brno $c = new stdClass(); $c->name = "Brno"; $c->minLat = 49128973; $c->minLng = 16015598; $c->maxLat = 49353120; $c->maxLng = 17128650; $cities[] = $c; // Kromeriz $c = new stdClass(); $c->name = "Kromeriz"; $c->minLat = 49241285; $c->minLng = 17308453; $c->maxLat = 49349083; $c->maxLng = 17473746; $cities[] = $c; // Ostrava $c = new stdClass(); $c->name = "Ostrava"; $c->minLat = 49698283; $c->minLng = 17907715; $c->maxLat = 49941057; $c->maxLng = 18566895; $cities[] = $c; ob_start(); $m = new MongoClient(); $db = $m->selectDB("ingress_messages"); $tstart = getmicrotime(); $players = array(); $player_guids = array(); $res = mysql_query("select id, name, guid from players"); while($r = mysql_fetch_array($res)) { $players[$r[1]] = $r[0]; $player_guids[$r[0]] = trim($r[2]); } foreach($cities as $city) { echo "Processing ".$city->name."\n========================================\n"; $col = $db->selectCollection($city->name); $maxtime_guids = array(); $cnt = 0; $end = false; $toInsert = array(); $guids = array(); $last = 0; $iter = 0; $msgs = array(); $duplicates = 0; $maxtime = 0; $res = mysql_query("select max(`when`) from actions where city='".$city->name."'"); if($res != false) { if($r = mysql_fetch_array($res)) { $maxtime = $r[0]; $res = mysql_query("select guid from messages where time >= ".$maxtime."000 AND city='".$city->name."'"); while($r = mysql_fetch_object($res)) { $maxtime_guids[] = trim($r->guid); } } } $maxtimems = -1; while(!$end) { $dashboard = false; $i = 0; while((!$dashboard || !isset($dashboard->result)) && $i < 3) { $i++; $data = ""; $actions_data = '{\"u3uxpkqd4pn37ydn\":200,\"krpywcgq1voq71z3\":'.$city->minLat.',\"yo6lte88zvoneqi6\":'.$city->minLng.',\"dncli54tfafmtk6y\":'.$city->maxLat.',\"76pq437r7vm3osx9\":'.$city->maxLng.',\"msw5gcxhuuk46rb2\":-1,\"bps0ekgdzakdfvr0\":'.$maxtimems.',\"pm4fm8bjvotjm30h\":\"all\",\"g9cmy5g6vpxpmcxz\":\"h785pmet6wrx6xoa\",\"blq7574e6kkg0fig\":\"465c62b22b3bc9ecae01e08b30703752186a1dc9\"}'; $cmd = 'curl https://www.ingress.com/r/h785pmet6wrx6xoa --compressed --insecure --header "DNT: 1" --user-agent "'.$ing_user_agent.'" --cookie "'.$ing_cookie.'" --cookie-jar "ingress_intel_chat.cookie.jar" --referer "https://www.ingress.com/intel" --header "X-CSRFToken: '.$ing_csrf.'" --header "X-Requested-With: XMLHttpRequest" --data-binary "'.$actions_data.'" --interface eth1:2 --header "x-rc4-random: '.$ing_rc4_random.'" 2> /dev/null'; // debug //echo $cmd."\n"; //die; // END debug $input = popen($cmd, "r"); while(!feof($input)) { $data .= fgets($input, 64*1024); } pclose($input); //print_r($data); $dashboard = json_decode($data); } $last = $dashboard->result[0][1]; foreach($dashboard->result as $res) { $time = (int)substr($res[1], 0, 10); if($time < $maxtime) { $end = true; continue; } $mguid = trim($res[0]); if(in_array($mguid, $guids) || in_array($mguid, $maxtime_guids)) { $duplicates++; continue; } $guids[] = $mguid; $msg = "insert into messages(time, guid, message, city) values(".$res[1].", '".$res[0]."', '".str_replace("'", "\\'", json_encode($res))."', '".$city->name."')"; $msgs[] = $msg; //print_r($msgs); // Insert into MONGO error_reporting(E_ALL); $col->insert($res); error_reporting(0); $maxtimems = $res[1]; if($res[2]->plext->plextType == "PLAYER_GENERATED") continue; if($res[2]->plext->team == "NEUTRAL") continue; if($res[2]->plext->plextType == "SYSTEM_NARROWCAST") continue; if($res[2]->plext->markup[4][1]->plain == 'has decayed') continue; if($res[2]->plext->markup[0][0] != "PLAYER" && $res[2]->plext->markup[4][1]->plain != 'has decayed') { print_r($res); continue; } $level = 0; $who = $res[2]->plext->markup[0][1]->plain; $side = $res[2]->plext->markup[0][1]->team; $guid = $res[2]->plext->markup[0][1]->guid; // Get player ID or insert into DB if(!isset($players[$who])) { mysql_query("insert into players(name, side, guid) values('$who', '$side', '$guid')"); $players[$who] = mysql_insert_id(); $player_guids[$players[$who]] = $guid; echo "Inserting player $who (#".$players[$who].", GUID: $guid)\n"; } $who = $players[$who]; if(!isset($player_guids[$who]) || $player_guids[$who] == "") { mysql_query("update players set guid='$guid' where id=$who"); echo "Adding GUID $guid to player ".$res[2]->plext->markup[0][1]->plain."\n"; $player_guids[$who] = $guid; } $action = $res[2]->plext->markup[1][1]->plain; $ap = 0; switch(trim($action)) { case 'destroyed an': { $level = (int)substr($res[2]->plext->markup[2][1]->plain, 1); $action = 'resonator_destroy'; $ap = 75; break; } case 'destroyed the Link': { $action = 'link_destroy'; $ap = 187; break; } case 'destroyed a Control Field @': { $action = 'field_destroy'; $ap = 750; break; } case 'deployed an': { $level = (int)substr($res[2]->plext->markup[2][1]->plain, 1); $action = 'resonator_create'; $ap = 125; break; } case 'linked': { $action = 'link_create'; $ap = 313; break; } case 'created a Control Field @': { $action = "field_create"; $ap = 1250; break; } case 'captured': { $action = 'capture'; $ap = 500; break; } default: { print_r($res); break; } } // Now get position of the portal $lat = 0; $lng = 0; foreach($res[2]->plext->markup as $item) { if($item[0] == 'PORTAL') { $lat = $item[1]->latE6; $lng = $item[1]->lngE6; break; } } $query = "insert into actions(who, `when`, action, level, ap, latitude, longitude, city) values($who, $time, '$action', $level, $ap, $lat, $lng, '".$city->name."')"; //echo $query."\n"; //continue; if($maxtimems != $last) { //echo "aaa\n"; $last = $maxtimems; foreach ($toInsert as $act) { mysql_query($act); $cnt++; } foreach ($msgs as $m) { mysql_query($m); } $toInsert = array(); //$guids = array(); $msgs = array(); $toInsert[] = $query; } else { //echo "bb\n"; $toInsert[] = $query; } } //die; $iter++; echo "Request $iter...\n"; flush(); sleep(rand(10, 20)); if($iter > 20) $end = true; } echo "Inserted $cnt actions ($duplicates duplicates)\n"; $total_count += $cnt; if($end == false) echo "END NOT REACHED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; echo "\n\n"; } mysql_close(); $tend = getmicrotime(); $tim = $tend - $tstart; printf("Saving %d actions took %01.4f seconds\n", $total_count, $tim); ?> <?php $ret = ob_get_clean(); // Gzip/Deflate/Bzip2 compression if(stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "bzip2")) { $ret = bzcompress($ret, 9); header("Content-Encoding: bzip2"); } elseif(stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) { $ret = gzencode($ret, 9); header("Content-Encoding: gzip"); } elseif (stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "deflate")) { $ret = gzdeflate($ret, 9); header("Content-Encoding: deflate"); } header("Content-length: ".strlen($ret)); echo $ret; ?>