init commit

This commit is contained in:
root 2023-03-21 13:43:50 +03:00
commit d37e104105
9 changed files with 2927 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.php

0
README.MD Normal file
View File

2517
class_MysqliDb.php Normal file

File diff suppressed because it is too large Load Diff

153
class_livesklad.php Normal file
View File

@ -0,0 +1,153 @@
<?php
class livesklad
{
protected $token_info = array();
protected $api_url = "https://api.livesklad.com";
private $login = "";
private $password = "";
private $cURL = null;
private $headers = 'Content-Type: application/x-www-form-urlencoded';
public $id = "";
public $remainRequest = 0;
public function __construct($login, $password)
{
if(!isset($login) || !isset($password)) exit();
$this->login = $login;
$this->password = $password;
$this->cURL = curl_init();
curl_setopt_array($this->cURL, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_REFERER => "http://optima-crimea.ru/",
));
if(file_exists("class_" . __CLASS__ . ".json")) $this->token_info = json_decode(file_get_contents("class_" . __CLASS__ . ".json"), true);
$this->__checkToken();
}
public function __destruct()
{
if($this->cURL)
curl_close($this->cURL);
}
//Проверка токена на актуальность
private function __checkToken()
{
if(!isset($this->token_info['token']) || time() - $this->token_info["time"] >= 10 * 60) /* token timeout is 10 min */
$this->__getToken();
}
//Получение токена, сохранение в файл
private function __getToken()
{
$method = "auth";
$params = array('login' => $this->login, 'password' => $this->password);
$result = $this->query($method, $params, false);
if(isset($result->error))
{
return false;
}
$this->token_info = array("token" => $result->token, "time" => time(), "renew" => false, 'headers' => [$this->headers, 'Authorization: ' . $result->token]);
file_put_contents("class_" . __CLASS__ . ".json", json_encode($this->token_info));
return true;
}
protected function query($method, $params = [], $get)
{
// $get = true Метод GET, false Метод POST
$url = $this->api_url . "/" . $method;
$response = "";
if(!empty($params) && $get == true)
{
$url .= "?" . http_build_query($params);
}
curl_setopt($this->cURL, CURLOPT_URL, $url);
if($get == true)
{
curl_setopt($this->cURL, CURLOPT_POST, 0);
curl_setopt($this->cURL, CURLOPT_HTTPHEADER, $this->token_info['headers']);
$response = json_decode(curl_exec($this->cURL), true);
}
else
{
curl_setopt($this->cURL, CURLOPT_POST, 1);
curl_setopt($this->cURL, CURLOPT_POSTFIELDS, http_build_query($params));
$response = json_decode(curl_exec($this->cURL));
}
return $response;
}
public function getOrders ($params = [])
{
$this->__checkToken();
$method = "company/orders";
if(is_array($params) == false)
{
$params = [];
}
return $this->query($method, $params, true);
}
public function getCounteragents ($params = [])
{
$this->__checkToken();
$method = "counteragents";
if(is_array($params) == false)
{
$params = [];
}
return $this->query($method, $params, true);
}
public function getBranches($params = [])
{
$this->__checkToken();
$method = "shops";
return $this->query($method, $params, true);
}
public function getStatusID()
{
$this->__checkToken();
$method = "statuses";
$params = [];
$result = $this->query($method, $params, true);
return $result['data'];
}
}
?>

98
grab_counteragents.php Normal file
View File

@ -0,0 +1,98 @@
<?php
if (php_sapi_name() != "cli")
{
die("Only CLI mode allowed!");
}
require_once("class_livesklad.php");
require_once("class_MysqliDb.php");
$live = new livesklad(API_LOGIN, API_PASSWD);
$db = new MysqliDb ('localhost', DB_LOGIN, DB_PASSWD, DB_NAME);
$pauseBetweenRequests = 7;
$pauseLong = 5 * 60;
$dataLimit = 0;
$maxPageSize = 50;
$maxQueryLimit = 10000;
set_time_limit(0);
$params = array (
"page" => 1,
"pageSize" => 50
);
$i = 0;
//$db->onDuplicate(array("dateClose", "summPrice", "purchasePrice"), "orderId");
$remainRequest = 0;
do
{
$pageSize = ($params["page"] * $params["pageSize"] >= $maxQueryLimit) ? floor( ($maxQueryLimit / ($params["page"] * $maxPageSize)) * $maxPageSize) : $maxPageSize;
$params["pageSize"] = min($pageSize, $params["pageSize"]);
$results = $live->getCounteragents($params);
if ( isset($results["error"]) )
{
echo "Error: " . $results["error"]["statusCode"] . "\n" . $results["error"]["message"] . "\n";
echo "Page#: " . $params["page"] . "; pageSize: " . $pageSize . "\n";
$remainRequest = $results["error"]["remainRequest"];
}
else
{
foreach($results["data"] as $result)
{
if(isset($result["id"]))
{
$id = $db->insert ("counteragents", array(
"id" => $result["id"],
"name" => $result["name"]
));
if (is_array($result["phones"]) && sizeof($result["phones"]) > 0)
{
foreach ($result["phones"] as $phone)
{
$id = $db->insert("counteragents_phone", array(
"counteragent_id" => $result["id"],
"phone_number" => preg_replace("/[^0-9]/", "", $phone)
));
}
}
if(!$id)
{
echo "Insert failed: " . $db->getLastError() . "\n";
}
$i++;
}
}
$remainRequest = $results["remainRequest"];
if (!$dataLimit ) $dataLimit = $results["total"];
echo "Grabbed: " . $i . "[" . $dataLimit . "]; Page: " . $params["page"] . "[" . $params["pageSize"] . "]\n";
$params["page"]++;
}
echo "Remains: " . $remainRequest ."\n---\n";
if ($remainRequest < 4) sleep($pauseLong); else sleep($pauseBetweenRequests);
}
while ($remainRequest > 1 && $i < $dataLimit)
?>

100
grab_orders.php Normal file
View File

@ -0,0 +1,100 @@
<?php
if (php_sapi_name() != "cli")
{
die("Only CLI mode allowed");
}
require_once("class_livesklad.php");
require_once("class_MysqliDb.php");
$live = new livesklad(API_LOGIN, API_PASSWD);
$db = new MysqliDb ('localhost', DB_LOGIN, DB_PASSWD, DB_NAME);
$pauseBetweenRequests = 7;
$pauseLong = 5 * 60;
$dataLimit = 100;
$maxPageSize = 50;
$maxQueryLimit = 10000;
set_time_limit(0);
$params = array (
"isVisible" => true,
"page" => 1,
"pageSize" => 50,
"sort" => "id DESC"
);
$i = 0;
$db->onDuplicate(array("dateClose", "summPrice", "purchasePrice"), "orderId");
$remainRequest = 0;
do
{
$pageSize = ($params["page"] * $params["pageSize"] >= $maxQueryLimit) ? floor( ($maxQueryLimit / ($params["page"] * $maxPageSize)) * $maxPageSize) : $maxPageSize;
$params["pageSize"] = min($pageSize, $params["pageSize"]);
$orders = $live->getOrders($params);
if ( isset($orders["error"]) )
{
echo "Error: " . $orders["error"]["statusCode"] . "\n" . $orders["error"]["message"] . "\n";
echo "Page: " . $params["page"] . "; pageSize: " . $pageSize . "\n";
$remainRequest = $orders["error"]["remainRequest"];
}
else
{
foreach($orders["data"] as $order)
{
if(isset($order["id"]))
{
$order["dateCreate"] = date("Y-m-d H:i:s", strtotime($order["dateCreate"]));
$order["dateClose"] = (isset($order["dateClose"]) && !is_null($order["dateClose"])) ? date("Y-m-d H:i:s", strtotime($order["dateClose"])) : null;
$id = $db->insert ("orders", array(
"orderId" => $order["id"],
"counteragentId" => $order["counteragentId"],
"dateCreate" => $order["dateCreate"],
"dateClose" => $order["dateClose"],
"shopId" => $order["shopId"],
"statusId" => $order["statusId"],
"typeOrderId" => $order["typeOrderId"],
"summPrice" => $order["summ"]["price"],
"purchasePrice" => $order["summ"]["purchasePrice"]
));
if(!$id)
{
echo "Insert failed: " . $db->getLastError() . "\n";
}
$i++;
}
}
$remainRequest = $orders["remainRequest"];
if (!$dataLimit ) $dataLimit = $orders["total"];
echo "Grabbed: " . $i . "[" . $orders["total"] . "]; Page: " . $params["page"] . "[" . $params["pageSize"] . "]\n";
$params["page"]++;
}
echo "Remains: " . $remainRequest ."\n---\n";
//@file_put_contents('grab_orders.json', json_encode($params));
if ($remainRequest < 4) sleep($pauseLong); else sleep($pauseBetweenRequests);
}
while ($remainRequest > 1 && $i <= $dataLimit)
?>

32
grab_shops.php Normal file
View File

@ -0,0 +1,32 @@
<?php
if (php_sapi_name() != "cli")
{
die("Only CLI mode allowed!");
}
require_once("class_livesklad.php");
require_once("class_MysqliDb.php");
$live = new livesklad(API_LOGIN, API_PASSWD);
$db = new MysqliDb ('localhost', DB_LOGIN, DB_PASSWD, DB_NAME);
set_time_limit(0);
$params = array (
"page" => 1,
"pageSize" => 50
);
$results = $live->getBranches($params);
print_r($results);
/*
*/
?>

View File

@ -0,0 +1,7 @@
SELECT
c.id,
c.name,
GROUP_CONCAT(cph.phone_number SEPARATOR ';') AS phone
FROM counteragents c
LEFT JOIN counteragents_phone cph ON c.id = cph.counteragent_id
GROUP BY id

19
sql_fullquery.txt Normal file
View File

@ -0,0 +1,19 @@
SELECT
o.orderId,
c.name,
GROUP_CONCAT(DISTINCT(cph.phone_number) SEPARATOR ';') AS phone,
o.dateCreate,
o.dateClose,
o.shopId,
o.statusId,
o.summPrice,
o.purchasePrice
FROM
orders o
LEFT JOIN counteragents c ON c.id = o.counteragentId
LEFT JOIN counteragents_phone cph ON c.id = cph.counteragent_id
WHERE
o.dateClose IS NULL
GROUP BY c.id
ORDER BY dateCreate DESC
LIMIT 30