/**
  * @param string $string
  *
  * @return string
  */
 public function clean($string)
 {
     $reg = '#' . preg_quote($this->browser->getHost()) . '/ch\\d+ \\[([^\\]]+)\\]#';
     $string = preg_replace($reg, '$1', $string);
     $string = preg_replace('/\\[[^\\[\\]]+\\]/', '', $string);
     // remove BB tags
     return $string;
 }
 /**
  * @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();
     }
 }
 /**
  * Test get from cache
  */
 public function testGetFromCache()
 {
     $this->cache->expects($this->once())->method('get')->with($this->getUrl('foo', ['bar' => 'baz']))->will($this->returnValue($this->xml));
     $this->cache->expects($this->never())->method('set');
     $this->browser->setResponseCache($this->cache);
     $this->browser->get('foo', ['bar' => 'baz']);
 }
 /**
  * @param Item $item
  *
  * @return string
  */
 public function getSourceForFill(Item $item)
 {
     /* @var $source Source */
     foreach ($item->getSources() as $source) {
         if (strpos($source->getUrl(), $this->browser->getHost()) === 0) {
             return $source->getUrl();
         }
     }
     return '';
 }
Example #5
0
 /**
  * Search source by name.
  *
  * @param array $data
  *
  * @return ItemSearch[]
  */
 public function search(array $data)
 {
     // if the db does not exists, send a request to download
     if (!file_exists($this->titles_db)) {
         return [];
     }
     $search = $this->getUnifiedTitle($data['name']);
     // search by name
     $aids = [];
     $fp = gzopen($this->titles_db, 'r');
     while (!gzeof($fp)) {
         $line = trim(gzgets($fp, 4096));
         list($aid, $type, $lang, $unified) = explode('|', $line);
         if (mb_strpos($unified, $search, 0, 'utf8') === 0) {
             if ($type == 1 || $type == 4 && $lang == $this->locale || empty($titles[$aid])) {
                 $aids[] = $aid;
             }
         }
     }
     gzclose($fp);
     $aids = array_unique($aids);
     // get all names for aid
     $items = [];
     $fp = gzopen($this->titles_db, 'r');
     while (!gzeof($fp)) {
         $line = trim(gzgets($fp, 4096));
         list($aid, $type, $lang, , $title) = explode('|', $line);
         if (in_array($aid, $aids)) {
             $items[$aid][$lang][$type] = $title;
         }
     }
     gzclose($fp);
     // build result
     foreach ($items as $aid => $item) {
         if (!empty($item[$this->locale])) {
             $main_name = $this->getNameForLocale($this->locale, $item);
         } elseif ($this->locale != 'en' && !empty($item['en'])) {
             $main_name = $this->getNameForLocale('en', $item);
         } else {
             $main_name = $this->getNameForLocale(array_keys($item)[0], $item);
         }
         $description = [];
         foreach ($item as $names) {
             foreach ($names as $name) {
                 $description[] = $name;
             }
         }
         sort($description);
         $items[$aid] = new ItemSearch($main_name, $this->getLinkForFill($this->browser->getHost() . str_replace('#ID#', $aid, self::ITEM_LINK)), $this->router->generate('ani_db_media_cover', ['id' => $aid]), implode("\n", array_unique($description)), $this->browser->getHost() . str_replace('#ID#', $aid, self::ITEM_LINK));
     }
     return $items;
 }
Example #6
0
 /**
  * @param string $url
  *
  * @return bool
  */
 public function isSupportedUrl($url)
 {
     return strpos($url, $this->browser->getHost()) === 0;
 }