Exemplo n.º 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;
 }
Exemplo n.º 2
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;
     }
     $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;
 }
Exemplo n.º 3
0
 /**
  * Fill head data
  *
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item
  * @param \DOMXPath $xpath
  * @param \DOMElement $head
  *
  * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
  */
 private function fillHeadData(Item $item, \DOMXPath $xpath, \DOMElement $head)
 {
     /* @var $data \DOMElement */
     $data = $xpath->query('font', $head)->item(0);
     $length = $data->childNodes->length;
     for ($i = 0; $i < $length; $i++) {
         if ($data->childNodes->item($i)->nodeName == 'b') {
             switch ($data->childNodes->item($i)->nodeValue) {
                 // set country
                 case 'Производство':
                     $j = 1;
                     do {
                         if ($data->childNodes->item($i + $j)->nodeName == 'a') {
                             $country_name = trim($data->childNodes->item($i + $j)->nodeValue);
                             if ($country_name && ($country = $this->getCountryByName($country_name))) {
                                 $item->setCountry($country);
                             }
                             break;
                         }
                         $j++;
                     } while ($data->childNodes->item($i + $j)->nodeName != 'br');
                     $i += $j;
                     break;
                     // add genre
                 // add genre
                 case 'Жанр':
                     $j = 2;
                     do {
                         if ($data->childNodes->item($i + $j)->nodeName == 'a' && ($genre = $this->getGenreByName($data->childNodes->item($i + $j)->nodeValue))) {
                             $item->addGenre($genre);
                         }
                         $j++;
                     } while ($data->childNodes->item($i + $j)->nodeName != 'br');
                     $i += $j;
                     break;
                     // set type and add file info
                 // set type and add file info
                 case 'Тип':
                     $type = $data->childNodes->item($i + 1)->nodeValue;
                     if (preg_match('/(?<type>[\\w\\s]+)(?: \\((?:(?<episodes_number>>?\\d+) эп.)?(?<file_info>.*)\\))?(, (?<duration>\\d{1,3}) мин\\.)?$/u', $type, $match)) {
                         // add type
                         if ($type = $this->getTypeByName(trim($match['type']))) {
                             $item->setType($type);
                         }
                         // add duration
                         if (!empty($match['duration'])) {
                             $item->setDuration((int) $match['duration']);
                         }
                         // add number of episodes
                         if (!empty($match['episodes_number'])) {
                             if ($match['episodes_number'][0] == '>') {
                                 $item->setEpisodesNumber(substr($match['episodes_number'], 1) . '+');
                             } else {
                                 $item->setEpisodesNumber((int) $match['episodes_number']);
                             }
                         } elseif ($item->getType()->getId() != 'tv') {
                             // everything except the TV series consist of a single episode
                             $item->setEpisodesNumber(1);
                         }
                         // add file info
                         if (!empty($match['file_info'])) {
                             $file_info = $item->getFileInfo();
                             $item->setFileInfo(($file_info ? $file_info . "\n" : '') . trim($match['file_info']));
                         }
                     }
                     $i++;
                     break;
                     // set date premiere and date end if exists
                 // set date premiere and date end if exists
                 case 'Премьера':
                 case 'Выпуск':
                     $j = 1;
                     $date = '';
                     do {
                         $date .= $data->childNodes->item($i + $j)->nodeValue;
                         $j++;
                     } while ($length > $i + $j && $data->childNodes->item($i + $j)->nodeName != 'br');
                     $i += $j;
                     $reg = '/(?<start>(?:(?:\\d{2})|(?:\\?\\?)).\\d{2}.\\d{4})' . '(?:.*(?<end>(?:(?:\\d{2})|(?:\\?\\?)).\\d{2}.\\d{4}))?/';
                     if (preg_match($reg, $date, $match)) {
                         $item->setDatePremiere(new \DateTime(str_replace('??', '01', $match['start'])));
                         if (isset($match['end'])) {
                             $item->setDateEnd(new \DateTime($match['end']));
                         }
                     }
                     break;
                 case 'Хронометраж':
                     if (preg_match('/(?<duration>\\d+)/', $data->childNodes->item($i + 1)->nodeValue, $match)) {
                         $item->setDuration((int) $match['duration']);
                     }
                     break;
                 case 'Кол-во серий':
                     $number = trim($data->childNodes->item($i + 1)->nodeValue, ' :');
                     if (strpos($number, '>') !== false) {
                         $number = str_replace('>', '', $number) . '+';
                     }
                     $item->setEpisodesNumber($number);
                     break;
             }
         }
     }
 }
Exemplo n.º 4
0
 /**
  * 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;
 }