Example #1
0
 /**
  * Fill item from source.
  *
  * @param array $data
  *
  * @return Item|null
  */
 public function fill(array $data)
 {
     if (empty($data['url']) || !is_string($data['url']) || strpos($data['url'], $this->browser->getHost()) !== 0 || !preg_match(self::REG_ITEM_ID, $data['url'], $match)) {
         return null;
     }
     $body = $this->browser->get('anime', ['aid' => $match['id']]);
     $item = new Item();
     $item->setEpisodesNumber($body->filter('episodecount')->text());
     $item->setDatePremiere(new \DateTime($body->filter('startdate')->text()));
     $item->setDateEnd(new \DateTime($body->filter('enddate')->text()));
     $item->setSummary($this->cleaner->clean($body->filter('description')->text()));
     // set main source
     $source = new Source();
     $source->setUrl($data['url']);
     $item->addSource($source);
     // add url to offsite
     $source = new Source();
     $source->setUrl($body->filter('url')->text());
     $item->addSource($source);
     // set complex data
     $this->setCover($item, $body, $match['id']);
     $this->setNames($item, $body);
     $this->setEpisodes($item, $body);
     $this->setType($item, $body);
     $this->setGenres($item, $body);
     return $item;
 }
 /**
  * Fill item from source.
  *
  * @param array $data
  *
  * @return Item|null
  */
 public function fill(array $data)
 {
     if (empty($data['url']) || !is_string($data['url']) || strpos($data['url'], $this->browser->getHost()) !== 0 || !preg_match(self::REG_ITEM_ID, $data['url'], $match)) {
         return null;
     }
     $path = str_replace('#ID#', $match['id'], self::FILL_URL);
     $body = $this->browser->get($path);
     $item = new Item();
     $item->setDuration($body['duration']);
     $item->setSummary($body['description']);
     $item->setDatePremiere(new \DateTime($body['aired_on']));
     if ($body['released_on']) {
         $item->setDateEnd(new \DateTime($body['released_on']));
     }
     $ep_num = $body['episodes_aired'] ? $body['episodes_aired'] : $body['episodes'];
     $item->setEpisodesNumber($ep_num . ($body['ongoing'] ? '+' : ''));
     // set main source
     $source = new Source();
     $source->setUrl($data['url']);
     $item->addSource($source);
     // set complex data
     $this->setSources($item, $body);
     $this->setCover($item, $body);
     $this->setType($item, $body);
     $this->setNames($item, $body);
     $this->setGenres($item, $body);
     $this->setStudio($item, $body);
     if (!empty($data['frames'])) {
         $this->setImages($item, $body);
     }
     return $item;
 }
 /**
  * Fill body data
  *
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item
  * @param \DOMXPath $xpath
  * @param \DOMElement $body
  * @param integer $id
  * @param boolean $frames
  * @param string $type
  *
  * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
  */
 private function fillBodyData(Item $item, \DOMXPath $xpath, \DOMElement $body, $id, $frames, $type)
 {
     for ($i = 0; $i < $body->childNodes->length; $i++) {
         if ($value = trim($body->childNodes->item($i)->nodeValue)) {
             switch ($value) {
                 // get summary
                 case 'Краткое содержание:':
                     $summary = $xpath->query('tr/td/p[1]', $body->childNodes->item($i + 2));
                     if ($summary->length) {
                         $item->setSummary($this->getNodeValueAsText($summary->item(0)));
                     }
                     $i += 2;
                     break;
                     // get episodes
                 // get episodes
                 case 'Эпизоды:':
                     if (!trim($body->childNodes->item($i + 1)->nodeValue)) {
                         // simple list
                         $item->setEpisodes($this->getNodeValueAsText($body->childNodes->item($i + 2)));
                         $i += 2;
                     } else {
                         // episodes in table
                         $rows = $xpath->query('tr/td[2]', $body->childNodes->item($i + 1));
                         $episodes = '';
                         for ($j = 1; $j < $rows->length; $j++) {
                             $episode = $xpath->query('font', $rows->item($j));
                             $episodes .= $j . '. ' . $episode->item(0)->nodeValue;
                             if ($rows->length > 1) {
                                 $episodes .= ' (' . $episode->item(1)->nodeValue . ')';
                             }
                             $episodes .= "\n";
                         }
                         $item->setEpisodes($episodes);
                         $i++;
                     }
                     break;
                     // get date premiere
                 // get date premiere
                 case 'Даты премьер и релизов':
                     $rows = $xpath->query('tr/td/table/tr/td[3]', $body->childNodes->item($i + 1));
                     foreach ($rows as $row) {
                         if (preg_match('/\\d{4}\\.\\d{2}\\.\\d{2}/', $row->nodeValue, $match)) {
                             $date = new \DateTime(str_replace('.', '-', $match[0]));
                             if (!$item->getDatePremiere() || $item->getDatePremiere() > $date) {
                                 $item->setDatePremiere($date);
                             }
                         }
                     }
                     break;
                 default:
                     // get frames
                     if ((strpos($value, 'кадры из аниме') !== false || strpos($value, 'Кадры из фильма') !== false) && $id && $frames) {
                         foreach ($this->getFrames($id, $type) as $frame) {
                             $item->addImage($frame);
                         }
                     }
             }
         }
     }
 }
 /**
  * Fill item
  *
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $new_item
  * @param string $field
  *
  * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
  */
 protected function fillItem(Item $item, Item $new_item, $field)
 {
     switch ($field) {
         case self::FIELD_COUNTRY:
             $item->setCountry($new_item->getCountry());
             break;
         case self::FIELD_DATE_END:
             $item->setDateEnd($new_item->getDateEnd());
             break;
         case self::FIELD_DATE_PREMIERE:
             $item->setDatePremiere($new_item->getDatePremiere());
             break;
         case self::FIELD_DURATION:
             $item->setDuration($new_item->getDuration());
             break;
         case self::FIELD_EPISODES:
             $item->setEpisodes($new_item->getEpisodes());
             break;
         case self::FIELD_EPISODES_NUMBER:
             $item->setEpisodesNumber($new_item->getEpisodesNumber());
             break;
         case self::FIELD_FILE_INFO:
             $item->setFileInfo($new_item->getFileInfo());
             break;
         case self::FIELD_GENRES:
             /* @var $new_genre \AnimeDb\Bundle\CatalogBundle\Entity\Genre */
             foreach ($new_item->getGenres() as $new_genre) {
                 $item->addGenre($new_genre);
             }
             break;
         case self::FIELD_NAMES:
             // 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:
             foreach ($new_item->getSources() as $new_source) {
                 $item->addSource($new_source);
             }
             break;
         case self::FIELD_STUDIO:
             $item->setStudio($new_item->getStudio());
             break;
         case self::FIELD_SUMMARY:
             $item->setSummary($new_item->getSummary());
             break;
     }
     return $item;
 }