public function post_refresh() { $result = 0; $val_error = []; $auc_ids = []; $page = (int) \Input::post('pages'); $select = \DB::select('auc_id')->from('auctions')->order_by('id', 'desc')->limit(Config::get('my.task.last_won_limit'))->execute()->as_array(); $user_id = \DB::select('id')->from('users')->where('username', Config::get('my.main_bidder'))->execute()->as_array(); foreach ($select as $value) { $auc_ids[] = $value['auc_id']; } $val = Model_Auction::validate(); try { $browser = new Browser(); foreach ($browser->won($page) as $auc_id) { if (!in_array($auc_id, $auc_ids)) { try { $auc_xml = $browser->getXmlObject($auc_id); $auc_values = []; $auc_values['auc_id'] = (string) $auc_xml->Result->AuctionID; $auc_values['title'] = (string) $auc_xml->Result->Title; $auc_values['price'] = (int) $auc_xml->Result->Price; $auc_values['won_date'] = Date::create_from_string((string) $auc_xml->Result->EndTime, 'yahoo_date')->format('mysql'); $auc_values['user_id'] = $user_id[0]['id']; $vendor_name = (string) $auc_xml->Result->Seller->Id; $vendor_id = \DB::select('id')->from('vendors')->where('name', '=', $vendor_name)->execute()->as_array(); if (!empty($vendor_id)) { $auc_values['vendor_id'] = $vendor_id[0]['id']; } else { if (Model_Vendor::forge()->set(['name' => $vendor_name, 'by_now' => 0])->save()) { $vendor_id = \DB::select('id')->from('vendors')->where('name', '=', $vendor_name)->execute()->as_array(); $auc_values['vendor_id'] = $vendor_id[0]['id']; } } if ($val->run($auc_values)) { Model_Auction::forge()->set($auc_values)->save(); $result++; } else { foreach ($val->error() as $value) { Log::error('Validation error in controller/admin/api.php: ' . $value); } $val_error[] = "Could not save auction " . $auc_values['auc_id']; } } catch (BrowserException $e) { $val_error[] = "ID: " . $auc_id . " Error: " . $e->getMessage(); } } } } catch (BrowserLoginException $e) { $val_error[] = "Login error: " . $e->getMessage(); } catch (ParserException $e) { $val_error[] = "Parser error: " . $e->getMessage(); } $this->response(['result' => $result, 'error' => implode('<br>', (array) $val_error)]); }
private static function check_won_at_time() { $interval = \Config::get('my.task.lot_update_interval'); if (self::$LAST_CHECK_TIME < strtotime("-{$interval} minute")) { $auc_ids = []; $select = \DB::select('auc_id')->from('auctions')->order_by('id', 'desc')->limit(\Config::get('my.task.last_won_limit'))->execute()->as_array(); $user_id = \DB::select('id')->from('users')->where('username', \Config::get('my.main_bidder'))->execute()->as_array(); foreach ($select as $value) { $auc_ids[] = $value['auc_id']; } $val = \Model_Auction::validate(); try { $browser = new \Browser(); foreach ($browser->won(self::$PAGE_TO_UPDATE) as $auc_id) { if (!in_array($auc_id, $auc_ids)) { try { $auc_xml = $browser->getXmlObject($auc_id); $auc_values = []; $auc_values['auc_id'] = (string) $auc_xml->Result->AuctionID; $auc_values['title'] = (string) $auc_xml->Result->Title; $auc_values['price'] = (int) $auc_xml->Result->Price; $auc_values['won_date'] = \Date::create_from_string((string) $auc_xml->Result->EndTime, 'yahoo_date')->format('mysql'); $auc_values['user_id'] = $user_id[0]['id']; $vendor_name = (string) $auc_xml->Result->Seller->Id; $vendor_id = \DB::select('id')->from('vendors')->where('name', '=', $vendor_name)->execute()->as_array(); if (!empty($vendor_id)) { $auc_values['vendor_id'] = $vendor_id[0]['id']; } else { if (\Model_Vendor::forge()->set(['name' => $vendor_name, 'by_now' => 0])->save()) { $vendor_id = \DB::select('id')->from('vendors')->where('name', '=', $vendor_name)->execute()->as_array(); $auc_values['vendor_id'] = $vendor_id[0]['id']; } } if ($val->run($auc_values)) { \Model_Auction::forge()->set($auc_values)->save(); } else { foreach ($val->error() as $value) { \Log::error('Validation error in task Minutely on method check_won_at_time : ' . $value); } } } catch (\BrowserException $e) { \Log::error("ID: " . $auc_id . " Error: " . $e->getMessage()); } } } } catch (\BrowserLoginException $e) { \Log::error("Login error: " . $e->getMessage()); } catch (\ParserException $e) { \Log::error("Parser error: " . $e->getMessage()); } \Cache::set('yahoo.won_last_check', time()); } }