/** * @param AddNewItem $event */ public function onAddNewItem(AddNewItem $event) { $item = $event->getItem(); if (!$event->getFillers()->contains($this->filler) && ($url = $this->refiller->getSourceForFill($item)) && preg_match(Filler::REG_ITEM_ID, $url, $match)) { try { // get data $body = $this->browser->get('anime', ['aid' => $match['id']]); } catch (\Exception $e) { return; } // fill item if (!$item->getDateEnd()) { $item->setDateEnd(new \DateTime($body->filter('enddate')->text())); } if (!$item->getDatePremiere()) { $item->setDatePremiere(new \DateTime($body->filter('startdate')->text())); } if (!$item->getEpisodes()) { $this->filler->setEpisodes($item, $body); } if (!$item->getEpisodesNumber()) { $item->setEpisodesNumber($body->filter('episodecount')->text()); } if (!$item->getSummary()) { $item->setSummary($this->cleaner->clean($body->filter('description')->text())); } if (!$item->getType()) { $this->filler->setType($item, $body); } if (!$item->getCover()) { $this->filler->setCover($item, $body, $match['id']); } $this->filler->setGenres($item, $body); // copy main and other names $new_item = $this->filler->setNames(new Item(), $body); // set main name in top of names list $new_names = $new_item->getNames()->toArray(); array_unshift($new_names, (new Name())->setName($new_item->getName())); /* @var $new_name Name */ foreach ($new_names as $new_name) { $item->addName($new_name->setItem(null)); } $event->addFiller($this->filler); // resend event $this->dispatcher->dispatch(StoreEvents::ADD_NEW_ITEM, clone $event); $event->stopPropagation(); } }
/** * Refill item field from source. * * @param Item $item * @param string $field * * @return Item */ public function refill(Item $item, $field) { $url = $this->getSourceForFill($item); if (!$url || !preg_match(Filler::REG_ITEM_ID, $url, $match)) { return $item; } // get data $body = $this->browser->get('anime', ['aid' => $match['id']]); switch ($field) { case self::FIELD_DATE_END: $item->setDateEnd(new \DateTime($body->filter('enddate')->text())); break; case self::FIELD_DATE_PREMIERE: $item->setDatePremiere(new \DateTime($body->filter('startdate')->text())); break; case self::FIELD_EPISODES: $this->filler->setEpisodes($item, $body); break; case self::FIELD_EPISODES_NUMBER: $item->setEpisodesNumber($body->filter('episodecount')->text()); break; case self::FIELD_GENRES: $new_item = $this->filler->setGenres(new Item(), $body); foreach ($new_item->getGenres() as $new_genre) { $item->addGenre($new_genre); } break; case self::FIELD_NAMES: $new_item = $this->filler->setNames(new Item(), $body); // set main name in top of names list $new_names = $new_item->getNames()->toArray(); array_unshift($new_names, (new Name())->setName($new_item->getName())); foreach ($new_names as $new_name) { $item->addName($new_name); } break; case self::FIELD_SOURCES: if ($url = $body->filter('url')->text()) { $item->addSource((new Source())->setUrl($url)); } break; case self::FIELD_SUMMARY: $item->setSummary($this->cleaner->clean($body->filter('description')->text())); break; } return $item; }