Пример #1
0
 protected function _run_cron_job($api, $from = 'no')
 {
     //if ( ! in_array($from, $this->fromAr)) {
     //	echo "You are not allowed to run this cron";
     //	exit;
     //}
     // Count the active proxies
     // Send a warning if 5 or less are active
     $threshold = 10;
     $proxies = getProxyIPS(TRUE, $threshold + 1);
     if (count($proxies) <= $threshold) {
         $this->email_alert('Low on proxies', 'The active proxy count has reached ' . $threshold . ' or less as of ' . date('Y-m-d H:i:s'));
     }
     $apis = ensure_array($api);
     $storesData = $this->Store->get_stores(TRUE);
     $currentDate = date('Y-m-d H:i:s');
     $this->Crowl_model->stats = array();
     // Cycle Through API crons
     for ($j = 0, $n = count($apis); $j < $n; $j++) {
         $this->Crowl_model->stats[$apis[$j]] = array('data_found' => 0, 'price_found' => 0);
         $this->Crowl_model->load_sticky_api($apis[$j]);
         $data = array('api_type' => $apis[$j], 'key' => generate_rand(32), 'run_from' => $from, 'start_datetime' => $currentDate);
         try {
             // Update cron log information
             $lastLog = $this->getMaxCronLog($data['api_type']);
             $lastRun = empty($lastLog) ? '0000-00-00 00:00:00' : $lastLog->datetime;
             if (!empty($lastLog)) {
                 if ($lastLog->datetime === "0000-00-00 00:00:00") {
                     $update = array('datetime' => $currentDate, 'end_datetime' => $currentDate);
                     $this->db->where('datetime', '0000-00-00 00:00:00')->where('api_type', $data['api_type'])->update('cron_log', $update);
                 }
             }
             if ($lastRun == '0000-00-00 00:00:00' || $currentDate >= $lastRun) {
                 // No entry in cron log table
                 $insert_id = $this->create_cron_log($data);
                 $this->resetViolationCount();
                 // Crawl the products for each store
                 for ($i = 0, $z = count($storesData); $i < $z; $i++) {
                     $this->index($storesData[$i]->id, $data['api_type'], $insert_id);
                 }
                 $data_edit['end_datetime'] = $data_edit['datetime'] = date("Y-m-d H:i:s");
                 $this->update_cron_log($insert_id, $data_edit);
             }
         } catch (Exception $e) {
             email_alertToTeam('Run Cron Job ' . ucwords($apis[$j]), $e->getMessage());
         }
         // Now that the crawl has finished,
         // compare these crawl numbers with the last
         // and send out notifications if needed.
         $currentLog = $this->getMaxCronLog($data['api_type']);
         $lastCount = $lastLog ? $lastLog->google_count : 0;
         $currentCount = $currentLog->google_count;
         $percentThresh = 25;
         // send a notification if completed this percent of the last crawl
         $smallThresh = 10;
         // send a notification if complete less than this many products
         $msg = '';
         $percentDrop = 100 - $percentThresh;
         if ($lastCount > 0 and $currentCount * 100 / $lastCount <= $percentThresh) {
             $msg = ' crawl completed ' . $percentDrop . '% less products than its previous crawl.';
         } elseif ($currentCount < $smallThresh) {
             $msg = ' crawl completed less than ' . $smallThresh . ' products.';
         }
         if (!empty($msg)) {
             $msg_txt = 'Crawl Warning: ' . $data['api_type'] . $msg;
             $msg_html = 'Crawl Warning: ' . '<b>' . $data['api_type'] . '</b>' . $msg;
             log_message('error', $msg_txt);
             send_email($this->config->item('alerts'), 'TrackStreet Errors', 'TrackStreet Crawl Warning', $msg_html, $msg_txt);
         }
     }
     $this->_checkNotFoundRatio();
     // todo: should we not return to the main script here?
     exit;
 }
Пример #2
0
function combi3($x)
{
    $password1 = generate_rand(12);
    $password2 = readable_random_string($_GET['number']);
    return myArrayMerge([$password1], [$password2]);
}
Пример #3
0
 * as a illegal request.
 */
if (!array_key_exists("serial", $_GET)) {
    echo "~failed~Illegal request!~failed~";
    exit;
}
$serial = $_GET["serial"];
if ($serial != md5($appserial)) {
    echo "~failed~Illegal request!~failed~";
    exit;
}
$time = date("Y-m-d H:i:s");
if (!array_key_exists("key", $_GET)) {
    $jump = 0;
    do {
        $key = generate_rand(20);
        $sql = sprintf("insert into clients (`id`, `key`, `cretime`, `time`, `times`) values (null, '{$key}', '{$time}', null, 1)");
        mysql_query($sql, $zconn->dblink);
        if (mysql_affected_rows() > 0) {
            echo "~successful~{$key}~successful~";
            exit;
        }
        $jump++;
        if ($jump > 3) {
            break;
        }
    } while (true);
    echo "~failed~Failed to generate the key.~failed~";
    exit;
} else {
    $key = $_GET["key"];
Пример #4
0
 public function run()
 {
     $this->_start_time = date('Y-m-d H:i:s');
     $this->_cron_log_id = $this->Log->create_log($this->_start_time);
     //need to get all products
     $productCount = $this->Products->get_all_tracked_products_count();
     $offset = 0;
     //queue up products -- so we don't crash the machine with a lot of products
     do {
         $products = $this->Products->get_all_tracked_products();
         $this->_products = array_merge($products, $this->_products);
         $this->_numproducts += count($products);
         $offset += 500;
     } while ($offset < $productCount);
     if (count($this->_crawlers) > 0) {
         foreach ($this->_crawlers as $crawler) {
             $crawlerName = strtolower(trim($crawler['name']));
             $filePath = FCPATH . 'system/application/crawlers/' . $crawlerName . '.php';
             //check to see if crawler exists
             if (@file_exists($filePath) === false) {
                 throw new Exception('The crawler ' . $crawlerName . ' file does not exist. Please add it.');
             }
             require_once $filePath;
             if (class_exists($crawlerName) === false) {
                 throw new Exception('The crawler ' . $crawlerName . ' crawler class is not available. Please check that its defined within the file');
             }
             $this->_currentCrawler = $crawlerName;
             $this->_crawlerClass = new $crawlerName($crawler);
             foreach ($this->_products as $product) {
                 if ($product === end($this->_products)) {
                     $this->_last_upc = $product->upc_code;
                 }
                 try {
                     $this->_current_product_id = $product->id;
                     $this->_crawlerClass->setIdentifier($product->upc_code);
                     $productData = $this->_crawlerClass->getProduct();
                     $offers = $this->_crawlerClass->getAllOffers();
                     $this->_numfoundproducts++;
                     foreach ($offers as $offer) {
                         $this->_numofferings++;
                         $crawled_products = array();
                         if (empty($offer['merchant'])) {
                             throw new Exception('Merchant not defined.');
                         }
                         $marketplace = $offer['marketplace'];
                         if (empty($marketplace)) {
                             throw new Exception('Marketplace not defined');
                         }
                         $r = preg_split('/(?=\\.[^.]+$)/', $marketplace);
                         $marketplace = strtolower($r[0]);
                         $merchant = $this->Crowl->getMerchantNameForKey($offer['merchant'], $product->upc_code, $marketplace, $offer['merchant'], $offer['merchant'], extractDomainByURL($offer['url']));
                         $this->_marketplaces[] = $offer['marketplace'];
                         if (empty($offer['price_floor']) || !is_numeric($offer['price_floor'])) {
                             throw new Exception('Price floor is undefined');
                         }
                         $crawled_products['ap'] = $offer['price_floor'];
                         $title = $productData->get("title");
                         if (empty($title)) {
                             throw new Exception('Title is undefined');
                         }
                         $crawled_products['t'] = $title;
                         $crawled_products['ar'] = $marketplace;
                         $crawled_products['il'] = null;
                         if (empty($offer['url'])) {
                             throw new Exception('Url is undefined');
                         }
                         $crawled_products['l'] = $offer['url'];
                         $crawled_products['mu'] = $offer['merchant_url'];
                         if (empty($offer['price']) || !is_numeric($offer['price'])) {
                             throw new Exception('Price is undefined');
                         }
                         $crawled_products['mpo'] = $offer['price'];
                         $hashKey = $marketplace . '#' . $product->upc_code;
                         $crawled_products['um'] = $hashKey;
                         $crawled_products['dt'] = time();
                         // Get the price from the last crawl
                         $dynamo = new AmazonDynamoDB();
                         $lastRecordResponse = $dynamo->query(array('TableName' => $this->_dynamo_products_trends, 'HashKeyValue' => array(AmazonDynamoDB::TYPE_STRING => $crawled_products['um']), 'ConsistentRead' => true, 'Limit' => 1, 'ScanIndexForward' => false));
                         var_dump($crawled_products);
                         $insert_response = $this->amzdb->insertData($this->_dynamo_products_trends, $crawled_products, $marketplace);
                         if (isset($insert_response->status) && $insert_response->status == 200) {
                             $this->Crowl->insertUPCMerchant($merchant, $product->upc_code, $marketplace, $crawled_products['mpo']);
                         }
                         $crowlMerchantName = $this->Crawl_data->crowlMerchantByMerchantName($merchant);
                         if (empty($crowlMerchantName)) {
                             log_message('error', __FILE__ . ' Crowl_m::amazon_lookup() Line ' . __LINE__ . ': crowl_merchant_name record not found for merchant ' . $offer['merchant']);
                         }
                         $crowlMerchantNameID = isset($crowlMerchantName->id) ? $crowlMerchantName->id : 0;
                         $violatedPrice = (double) $crawled_products['mpo'];
                         $dataVio = $this->Products->get_products_by_floor($product->upc_code, $violatedPrice, $product->store_id);
                         if ($dataVio) {
                             //$price_floor > $crowled_products['mpo'])
                             $violation['um'] = $hashKey;
                             $violation['dt'] = $crawled_products['dt'];
                             //time();
                             $violation['ss'] = date('Ymd', $violation['dt']) . '/' . md5($hashKey . $violation['dt']) . '.png';
                             $this->Crowl->updateViolationSummary($dataVio);
                             $this->Violator->updatePriceViolator($crowlMerchantNameID, $product->upc_code, 1, $crawled_products['dt']);
                             $lastCrawlPrice = (double) 0;
                             $hashKey = null;
                             $rangeKey = null;
                             if ($lastRecordResponse->isOK()) {
                                 $mpo = isset($lastRecordResponse->body->Items->mpo->N) ? (double) $lastRecordResponse->body->Items->mpo->N : (double) 0;
                                 // Merchant Price Offered
                                 $lastCrawlPrice = $mpo;
                                 $hashKey = isset($lastRecordResponse->body->Items->um->S) ? $lastRecordResponse->body->Items->um->S : null;
                                 $rangeKey = isset($lastRecordResponse->body->Items->dt->N) ? $lastRecordResponse->body->Items->dt->N : null;
                             }
                             // Check if the price has changed
                             if ($lastCrawlPrice != $violatedPrice) {
                                 if (!isset($uniquArr[$crawled_products['l']])) {
                                     $uniquArr[$crawled_products['l']] = $violation['ss'];
                                     $this->Crowl->addScreenShot($crawled_products['l'], $violation['ss'], false, $violatedPrice);
                                 } else {
                                     $violation['ss'] = $uniquArr[$crawled_products['l']];
                                 }
                             } else {
                                 $takeNewScreenShot = false;
                                 // Get the violation screen shot
                                 if (!is_null($hashKey) and !is_null($rangeKey)) {
                                     $lastViolationResponse = $dynamo->query(array('TableName' => $this->_dynamo_violations, 'HashKeyValue' => array(AmazonDynamoDB::TYPE_STRING => (string) $hashKey), 'RangeKeyCondition' => array('ComparisonOperator' => AmazonDynamoDB::CONDITION_EQUAL, 'AttributeValueList' => array(array(AmazonDynamoDB::TYPE_NUMBER => (string) $rangeKey)))));
                                     if ($lastViolationResponse->isOK() and $lastViolationResponse->body->Count == 1) {
                                         if (isset($lastViolationResponse->body->Items->ss->S)) {
                                             if (@fopen(get_instance()->config->item('s3_cname') . 'stickyvision/violations/' . $lastViolationResponse->body->Items->ss->S, 'r')) {
                                                 $violation['ss'] = $lastViolationResponse->body->Items->ss->S;
                                                 $takeNewScreenShot = false;
                                             } else {
                                                 $takeNewScreenShot = true;
                                             }
                                         }
                                     }
                                 }
                                 if ($takeNewScreenShot === true) {
                                     $this->Crowl->addScreenShot($crawled_products['l'], $violation['ss'], false, $violatedPrice);
                                 }
                             }
                             $this->amzdb->insertData($this->_dynamo_violations, $violation, $marketplace);
                             $violationFlag = true;
                             $violation = null;
                         } else {
                             // update price violators for products not in violation
                             $this->Violator->updatePriceViolator($crowlMerchantNameID, $product->upc_code, 0, $crawled_products['dt']);
                         }
                         $this->Crowl->updateUPCFlag($product->upc_code, $marketplace, '1');
                     }
                     //end offers iteration
                     $this->_crawlerClass->reset();
                 } catch (Exception $e) {
                     $exceptionDetail = array('crawler_log_id' => $this->_cron_log_id, 'crawler_name' => $this->_currentCrawler, 'message' => $e->getMessage(), 'created_at' => date('Y-m-d H:i:s'), 'product_id' => $this->_current_product_id);
                     echo 'exception detected..';
                     var_dump($exceptionDetail);
                     $this->CrawlerErrorLog->create_log($exceptionDetail);
                     //send an alert to the team
                     email_alertToTeam('Run Cron Job - Error Cron Log ID ' . $this->_cron_log_id . ' : ' . $e->getMessage());
                 }
             }
         }
     }
     $this->_end_time = date('Y-m-d H:i:s');
     $this->Log->update_log($this->_cron_log_id, array('end_datetime' => $this->_end_time, 'products_count' => $this->_numfoundproducts, 'offerings_count' => $this->_numofferings));
     //create cron_log -- this is needed to handle code that still thinks we need to use old crawler
     $this->_marketplaces = array_unique($this->_marketplaces);
     foreach ($this->_marketplaces as $marketplace) {
         $domain = trim($marketplace);
         $r = preg_split('/(?=\\.[^.]+$)/', $domain);
         $marketplace = strtolower($r[0]);
         $cron_log_id = $this->CronLog->create_log(array('datetime' => date('Y-m-d H:i:s'), 'key' => generate_rand(32), 'api_type' => $marketplace, 'start_datetime' => $this->_start_time, 'end_datetime' => $this->_end_time, 'google_count' => 0, 'last_UPC' => $this->_last_upc, 'run_from' => 'cronTab1'));
         $this->Marketplace->add_retailer(strtolower($marketplace), strtolower($domain));
     }
     exit;
 }