Example #1
0
 public static function validRequestedPage(XPath $hxs, $requestedPage)
 {
     $pager = $hxs->select('(//ul[@class="pager"]/li/a)[last()]');
     if ($pager->hasResults()) {
         $href = $pager->select('@href')->extract();
         $currentPage = (int) substr($href, strrpos($href, '/') + 1);
         if ($requestedPage > $currentPage) {
             return false;
         } else {
             return true;
         }
     } elseif ($requestedPage > 1) {
         return false;
     } else {
         return true;
     }
 }
Example #2
0
 protected function updateFriend($citizenId, $status = 'add')
 {
     $request = $this->getClient()->get('citizen/profile/' . $citizenId);
     $response = $request->send();
     $html = $response->getBody(true);
     $hxs = Selector\XPath::loadHTML($html);
     if ($hxs->select('//a[@class="action_friend tip"]/@href')->hasResults()) {
         $addurl = $hxs->select('//a[@class="action_friend tip"][1]/@href')->extract();
     }
     if ($hxs->select('//a[@class="action_friend_remove tip"]/@href')->hasResults()) {
         $removeurl = $hxs->select('//a[@class="action_friend_remove tip"][1]/@href')->extract();
     }
     if (isset($addurl) && $status == 'add') {
         $url = $addurl;
     }
     if (isset($removeurl) && $status == 'remove') {
         $url = $removeurl;
     }
     if (isset($url)) {
         $crequest = $this->getClient()->post($url);
         $cresponse = $crequest->send();
         $newRequest = $this->getClient()->get($cresponse->getLocation());
         $newResponse = $newRequest->send();
         $html = $newResponse->getBody(true);
         $hxs = Selector\XPath::loadHTML($html);
         $node = $hxs->select('//table[@class="success_message"]');
         if ($node->hasResults()) {
             return $node->select('tr/td')->extract();
         }
     }
     return false;
 }
Example #3
0
 protected function parseCommentsFeed($html, $postId)
 {
     $hxs = Selector\XPath::loadHTML($html);
     $commentItems = $hxs->select('//li');
     if (!$commentItems->hasResults()) {
         return array();
     }
     $comments = [];
     foreach ($commentItems as $commentItem) {
         $commentId = substr($postId, strripos($postId, '_') + 1);
         $profileId = $commentItem->select('div[@class="post_reply"]/p/strong/a/@href')->extract();
         $profileId = substr($profileId, strripos($profileId, '/profile/') + 10);
         $profileName = $commentItem->select('div[@class="post_reply"]/p/strong/a/@title')->extract();
         $reportRef = $commentItem->select('//a[@class="report"]/@href')->extract();
         $time = $commentItem->select('div[@class="post_reply"]/b/text()')->extract();
         $message = $commentItem->select('div[@class="post_reply"]/p')->extract();
         $message = str_replace(PHP_EOL, '<br />', $message);
         $comment = new Comment();
         $comment->postId = (int) $postId;
         $comment->commentId = (int) $commentId;
         $comment->profileId = (int) $profileId;
         $comment->profileName = $profileName;
         $comment->reportRef = $reportRef;
         $comment->time = $time;
         $comment->message = trim($message);
         $comments[] = $comment;
     }
     return $comments;
 }
Example #4
0
 public function getAvailableCampaigns()
 {
     $this->getClient()->checkLogin();
     $request = $this->getClient()->get('main/group-show/' . $this->getMilitaryUnitId() . '?page=1');
     $response = $request->send();
     $hxs = Selector\XPath::loadHTML($response->getBody(true));
     //get MU location
     $militaryUnitLocation = $hxs->select('//span[@id="error_message"]/../a')->extract();
     $militaryUnitLocation = trim($militaryUnitLocation);
     //get info about all battles
     $allBattlesSelect = $hxs->select('//div[@id="group_orders"]/../script');
     if (!$allBattlesSelect->hasResults()) {
         throw new ScrapeException();
     }
     $allBattlesJS = $allBattlesSelect->extract();
     $allBattlesJS = explode('var tempBattle = new Object();', $allBattlesJS);
     array_shift($allBattlesJS);
     $allBattles = array();
     foreach ($allBattlesJS as $battleJS) {
         preg_match('/allBattles\\[\\"(\\d+)\\"\\]/', $battleJS, $matches);
         $allBattles[$matches[1]] = Filter::extractJSObject('tempBattle', $battleJS);
     }
     $regions = $this->getEntityManager()->getRepository('Erpk\\Common\\Entity\\Region');
     $countries = $this->getEntityManager()->getRepository('Erpk\\Common\\Entity\\Country');
     $xpath = '//div[@class="mission pusher"]/div[@class="sublist"]/a';
     $list = $hxs->select($xpath);
     if (!$list->hasResults()) {
         return array();
     }
     $result = array();
     foreach ($list as $battle) {
         $id = (int) $battle->select('@reference_id')->extract();
         preg_match_all('/[\\w\\-]+/', $battle, $matches);
         array_shift($matches[0]);
         $regionName = join(' ', $matches[0]);
         $isResistance = false;
         $fightForName = $militaryUnitLocation;
         foreach ($battle->select('//a[@reference_id=' . $id . ']/span/img') as $image) {
             $url = $image->select('@src')->extract();
             if (preg_match('/\\/small_mpp\\.png/', $url)) {
                 $fightForName = $image->select('@title')->extract();
             } else {
                 if (preg_match('/\\/small_resistance\\.png/', $url)) {
                     $isResistanceWar = true;
                 }
             }
         }
         $campaign = new Campaign();
         $campaign->setId($id);
         $attackerId = $allBattles[$id]['invader_country_id'];
         $campaign->setAttacker($countries->find((int) $attackerId));
         $defenderId = $allBattles[$id]['defender_country_id'];
         $campaign->setDefender($countries->find((int) $defenderId));
         $campaign->setRegion($regions->findOneByName($regionName));
         $campaign->setResistance($isResistance);
         $fightFor = $countries->findOneByName($fightForName);
         $result[] = array('campaign' => $campaign, 'fightFor' => $fightFor);
     }
     //$result['changesLeft'] = $this->extractChangesLeft($response->getBody());
     return $result;
 }