Commit 15b5d6f2 authored by Gorodkov Denis's avatar Gorodkov Denis

0_0

parent 13320772
......@@ -9,10 +9,9 @@
*/
function mymodule_cron() {
$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
$queue_api = \Drupal::queue('mymodule_swapi');
$time = \Drupal::state()->get('date_queue_swapi');
$result_api = $api->getResponse('https://swapi.dev/api');
foreach($result_api as $item) {
$bundle = $api->getTypeNode($item);
......@@ -21,22 +20,11 @@ function mymodule_cron() {
while (!empty($uri)) {
$result = $api->getResponse($uri);
foreach($result['results'] as $value) {
$bundle = $api->getTypeNode($value['url']);
$swapi_id = $api->getSwapiId($value['url']);
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => $bundle,
'field_swapi_id' => $swapi_id,
]);
foreach ($node as $val) {
$node_edited = $val;
}
$node = $api->getNodeByUrl($value['url']);
if (!empty($node)) {
$changed = $node_edited->get('changed')->getValue();
$changed = $node->get('changed')->getValue();
$changed = $changed[0]['value'];
if ($time - $changed < 0) {
if ($time < $changed) {
$queue_api->createItem($value);
}
} else {
......@@ -49,10 +37,8 @@ function mymodule_cron() {
\Drupal::logger('Queue mymodule_api')->notice("Content type $bundle не создан!");
}
}
$date = new DateTime();
$date = $date->getTimestamp();
\Drupal::state()->set('date_queue_swapi', $date);
$time = \Drupal::time()->getCurrentTime();
Drupal::state()->set('date_queue_swapi', $time);
}
/**
......
......@@ -3,6 +3,7 @@
namespace Drupal\mymodule;
use GuzzleHttp\ClientInterface;
use Drupal\Core\Datetime\DrupalDateTime;
class API {
protected $httpClient;
......@@ -15,21 +16,21 @@ class API {
$request = $this->httpClient->request('GET', $uri);
$responseCode = $request->getStatusCode();
if ($responseCode == 200) {
$response = $request->getBody()->getContents();
$response = json_decode($response, true);
return $response;
}
return false;
}
public function getTypeNode($uri) {
$type_node = explode('/', stristr($uri, 'api/'));
return $type_node[1];
}
public function getSwapiId($uri) {
$swapi_id = explode('/', stristr($uri, 'api/'));
return $swapi_id[2];
}
......@@ -69,7 +70,6 @@ class API {
'type' => $bundle,
'field_swapi_id' => $swapi_id,
]);
foreach ($nodes as $value) {
$node = $value;
}
......@@ -81,7 +81,7 @@ class API {
$field_name = "field_" . $key;
$bool = (bool)($key != 'name' and $key != 'title' and $key != 'homeworld' and $key != 'url' and $key != 'created' and $key != 'people_exclude');
if ($bool and !is_array($value)) {
!empty($value) ? $node->set("field_" . strtolower($key), $value) : '';
isset($value) ? $node->set("field_" . strtolower($key), $value) : '';
}
if (is_array($value)) {
$node->set($field_name, NULL);
......
<?php
namespace Drupal\mymodule\Batch;
class Import {
public $batch;
public function __construct() {
$this->batch = [
'title' => 'Import nodes',
];
}
public function getNodes($time = '01.01.1970') {
$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
foreach($result_api as $item) {
$bundle = $api->getTypeNode($item);
if ($api->checkBundle($bundle)) {
$uri = $item;
while (!empty($uri)) {
$result = $api->getResponse($uri);
foreach($result['results'] as $value) {
$node = $api->getNodeByUrl($value['url']);
if (!empty($node)) {
$swapi_edited = date_parse($value['edited']);
$swapi_edited = $swapi_edited['day'] . "." . $swapi_edited['month'] . "." . $swapi_edited['year'] . " " . $swapi_edited['hour'] . $swapi_edited['minute'] . $swapi_edited['second'];
$swapi_edited = strtotime($swapi_edited);
$time = strtotime($time);
if ($time < $swapi_edited) {
$this->setOperation([$value]);
}
} else {
$api->createNode($value);
$title = !empty($value['title']) ? $value['title'] : $value['name'];
#$this->output()->writeln("Страница $title создана");
$this->setOperation([$value]);
}
}
$uri = $result['next'];
}
} else {
#$this->output()->writeln("Content type $bundle не создан");
}
}
}
public function processItem($data) {
$api = \Drupal::service('mymodule.custom_services');
$node = $api->getNodeByUrl($data['url']);
$api->edited($data, $node);
}
public function setOperation($data) {
$this->batch['operations'][] = [[$this, 'processItem'], $data];
}
public function setBatch() {
batch_set($this->batch);
}
public function processBatch() {
batch_process();
}
public function finished($success, $results, $operations) {
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results), 'One post processed.', '@count posts processed.');
}
else {
$message = t('Finished with an error.');
}
// ВЫВЕСТИ $message
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\mymodule\Commands;
use Drush\Commands\DrushCommands;
use Drupal\mymodule\Batch\Import;
/**
* A Drush command file.
......@@ -13,18 +14,23 @@ class BatchCommands extends DrushCommands {
/**
* Drush command that displays the given text.
*
* @param string $date
* @param string $time
* @command mymodule:custom
* @aliases custom
* @usage drush custom $date
* @usage drush custom $time
*/
public function custom($date = '01.01.1970') {
$api = \Drupal::service('mymodule.custom_services');
public function custom($time = '01.01.1970') {
$import = new Import();
$import->getNodes();
batch_set($import->batch);
drush_backend_batch_process();
/*$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
$queue_api = \Drupal::queue('mymodule_swapi_drush');
$queue_api->deleteQueue();
$count_create_node = 0;
$count_queue = 0;
$queue_api->deleteQueue();
foreach($result_api as $item) {
$bundle = $api->getTypeNode($item);
......@@ -33,22 +39,14 @@ class BatchCommands extends DrushCommands {
while (!empty($uri)) {
$result = $api->getResponse($uri);
foreach($result['results'] as $value) {
$bundle = $api->getTypeNode($value['url']);
$swapi_id = $api->getSwapiId($value['url']);
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => $bundle,
'field_swapi_id' => $swapi_id,
]);
$node = $api->getNodeByUrl($value['url']);
if (!empty($node)) {
$swapi_edited = date_parse($value['edited']);
$swapi_edited = $swapi_edited['day'] . "." . $swapi_edited['month'] . "." . $swapi_edited['year'];
$swapi_edited = strtotime($swapi_edited);
$date = strtotime($date);
$time = strtotime($time);
if ($date - $swapi_edited < 0) {
if ($time < $swapi_edited) {
$queue_api->createItem($value);
$count_queue++;
}
......@@ -79,6 +77,6 @@ class BatchCommands extends DrushCommands {
}
}
$this->output()->writeln("Страниц обновлено " . $count_queue . ", страниц создано " . $count_create_node);
$this->output()->writeln("Страниц обновлено " . $count_queue . ", страниц создано " . $count_create_node);*/
}
}
......@@ -7,6 +7,7 @@ namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\mymodule\Batch\Import;
class BatchForm extends FormBase {
......@@ -15,7 +16,7 @@ class BatchForm extends FormBase {
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['text'] = array(
$form['date'] = array(
'#type' => 'datetime',
'#title' => 'Date and time',
);
......@@ -29,18 +30,15 @@ class BatchForm extends FormBase {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$api = \Drupal::service('mymodule.custom_services');
$api->batch();
/*$nids = \Drupal::entityQuery('node')->condition('type','people')->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$operations = [];
foreach ($nodes as $node) {
$operations[] = ['callback', $node];
$date = $form_state->getValue('date');
$date = $date->getTimestamp();
$this->startImport();
}
batch_set([
'title' => 'Nodes import',
'operations' => $operations,
]);*/
public function startImport() {
$import = new Import();
$import->getNodes();
batch_set($import->batch);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment