/** * @param string $html * @return TorrentsCollection */ public function parse(string $html) : TorrentsCollection { libxml_use_internal_errors(true); $doc = new \DOMDocument(); $doc->loadHTML($html); $finder = new \DomXPath($doc); $torrents = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' hl-tr ')]"); $links = $finder->query("//*[contains(@class, 'med tLink hl-tags bold')]"); $result = new TorrentsCollection(); for ($i = 0; $i < $torrents->length; $i++) { if (is_null($links->item($i))) { continue; } $childNodes = $torrents->item($i)->childNodes; $status = self::clean($childNodes->item(2)->attributes->getNamedItem('title')->textContent); $category = self::clean($childNodes->item(4)->textContent); $title = self::clean($childNodes->item(6)->textContent); $torrentId = (int) self::clean($links->item($i)->attributes->getNamedItem('data-topic_id')->textContent); $detailUrl = self::DETAIL_URL . $torrentId; $authorUrl = parse_url($childNodes->item(8)->firstChild->firstChild->attributes->getNamedItem('href')->textContent); $authorId = (int) explode('=', $authorUrl['query'])[1]; $authorName = self::clean($childNodes->item(8)->firstChild->firstChild->textContent); $downloadUrl = self::clean($childNodes->item(10)->childNodes->item(3)->attributes->getNamedItem('href')->textContent); $downloadUrl = self::DOWNLOAD_URL . $downloadUrl; $size = self::clean(str_replace(' ↓', '', $childNodes->item(10)->childNodes->item(3)->textContent)); $seeders = (int) self::clean($childNodes->item(12)->firstChild->textContent); $leachers = (int) self::clean($childNodes->item(14)->textContent); $downloads = (int) self::clean($childNodes->item(16)->textContent); $added = (new \DateTimeImmutable())->setTimestamp((int) self::clean($childNodes->item(18)->childNodes->item(1)->textContent)); $torrent = (new Torrent())->setId($torrentId)->setTitle($title)->setCategory($category)->setStatus($status)->setDetailUrl($detailUrl)->setAuthorId($authorId)->setAuthorName($authorName)->setSize($size)->setDownloadUrl($downloadUrl)->setSeeders($seeders)->setLeachers($leachers)->setDownloads($downloads)->setAdded($added); $result->add($torrent); } return $result; }
public function merge(TorrentsCollection $torrentsCollection) { $this->torrents = $this->torrents + $torrentsCollection->getTorrents(); }