/** * Search source by name * * Return structure * <code> * [ * \AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Item * ] * </code> * * @param array $data * * @return array */ public function search(array $data) { $name = iconv('utf-8', 'cp1251', $data['name']); $url = str_replace('#NAME#', urlencode($name), self::SEARH_URL); $url = str_replace('#SECTOR#', isset($data['type']) ? $data['type'] : self::DEFAULT_SECTOR, $url); // get list from xpath $dom = $this->browser->getDom($url); $xpath = new \DOMXPath($dom); // if for request is found only one result is produced forwarding $refresh = $xpath->query('//meta[@http-equiv="Refresh"]/@content'); if ($refresh->length) { list(, $url) = explode('url=', $refresh->item(0)->nodeValue, 2); // add http if need if ($url[0] == '/') { $url = $this->browser->getHost() . $url; } $name = iconv('cp1251', 'utf-8', $name); if (!preg_match('/id=(?<id>\\d+)/', $url, $mat) || !($type = $this->filler->getItemType($url))) { throw new NotFoundHttpException('Incorrect URL for found item'); } return [new ItemSearch($name, $this->getLinkForFill($url), $this->filler->getCoverUrl($mat['id'], $type), '')]; } $rows = $xpath->query(self::XPATH_FOR_LIST); $list = []; foreach ($rows as $el) { $link = $xpath->query('a', $el); // has link on source if ($link->length && ($href = $link->item(0)->getAttribute('href')) && ($name = $link->item(0)->nodeValue) && preg_match('/id=(?<id>\\d+)/', $href, $mat) && ($type = $this->filler->getItemType($href))) { $list[] = new ItemSearch(str_replace(["\r\n", "\n"], ' ', $name), $this->getLinkForFill($this->browser->getHost() . '/' . $href), $this->filler->getCoverUrl($mat['id'], $type), trim(str_replace($name, '', $el->nodeValue)), $this->browser->getHost() . '/' . $href); } } return $list; }
/** * Get item frames * * @param integer $id * @param string $type * * @return array */ public function getFrames($id, $type) { $dom = $this->browser->getDom('/' . $type . '/' . $type . '_photos.php?id=' . $id); if (!$dom) { return []; } $images = (new \DOMXPath($dom))->query('//table//table//table//img'); $frames = []; foreach ($images as $image) { $src = $this->getAttrAsArray($image)['src']; $entity = new Image(); if ($type == self::ITEM_TYPE_ANIMATION) { $src = str_replace('optimize_b', 'optimize_d', $src); if (strpos($src, 'http://') === false) { $src = $this->browser->getHost() . '/' . $type . '/' . $src; } if (preg_match('/\\-(?<image>\\d+)\\-optimize_d(?<ext>\\.jpe?g|png|gif)/', $src, $mat)) { $entity->setSource(self::NAME . '/' . $id . '/' . $mat['image'] . $mat['ext']); if ($this->uploadImage($src, $entity)) { $frames[] = $entity; } } } elseif (preg_match('/_(?<round>\\d+)\\/.+\\/(?<id>\\d+)-(?<image>\\d+)-.+(?<ext>\\.jpe?g|png|gif)/', $src, $mat)) { $src = $this->browser->getHost() . '/' . $type . '/img/' . $mat['round'] . '/' . $mat['id'] . '/' . $mat['image'] . $mat['ext']; $entity->setSource(self::NAME . '/' . $id . '/' . $mat['image'] . $mat['ext']); if ($this->uploadImage($src, $entity)) { $frames[] = $entity; } } } return $frames; }