public function testParseManga() { $mangaContents = file_get_contents(__DIR__ . '/../InputSamples/manga-top.html'); $topList = Top::parse($mangaContents, 'manga'); $this->assertInternalType('array', $topList); $topItem = $topList[0]; $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Manga', $topItem); /* Although this test is against a static downloaded copy, the values are likely to change considerably * each time the source file is updated. As such, mostly check the type and confirm a few things that * shouldn't change. */ //Do some basic type checking for things that should exist (not null) $this->assertInternalType('int', $topItem->getId()); $this->assertInternalType('string', $topItem->getTitle()); $this->assertInternalType('string', $topItem->getImageUrl()); $this->assertInternalType('float', $topItem->getMembersScore()); $this->assertInternalType('int', $topItem->getMembersCount()); //Check some data that should remain consistent $this->assertStringStartsWith('https://myanimelist.cdn-dena.com/images/manga/', $topItem->getImageUrl()); $this->assertGreaterThan(8, $topItem->getMembersScore()); $this->assertGreaterThan(50000, $topItem->getMembersCount()); }
/** * Fetch Top-rated Manga by Popularity. * * Gets a list of the top-rated manga on MyAnimeList sorted by popularity. The get * variable "page" is used to select the set of results to return (in a default of 30 * items per set). An invalid or missing value defaults to page 1. * * @param string $apiVersion The API version of the request * @param Request $request Contains all the needed information to get the list. * * @return View */ public function getPopularMangaAction($apiVersion, Request $request) { // http://myanimelist.net/topmanga.php?type=bypopularity&limit=#{0} $page = (int) $request->query->get('page'); if ($page <= 0) { $page = 1; } $downloader = $this->get('atarashii_api.communicator'); try { $mangacontent = $downloader->fetch('/topmanga.php?type=bypopularity&limit=' . ($page * 50 - 50)); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } $response = new Response(); $serializationContext = SerializationContext::create(); $serializationContext->setVersion($apiVersion); //For compatibility, API 1.0 explicitly passes null parameters. if ($apiVersion == '1.0') { $serializationContext->setSerializeNull(true); } $response->setPublic(); $response->setMaxAge(10800); //Three hours $response->headers->addCacheControlDirective('must-revalidate', true); $response->setEtag('manga/popular?page=' . urlencode($page)); //Also, set "expires" header for caches that don't understand Cache-Control $date = new \DateTime(); $date->modify('+10800 seconds'); //Three hours $response->setExpires($date); if (strpos($mangacontent, 'No manga titles') !== false) { $view = $this->view(array('error' => 'not-found')); $view->setResponse($response); $view->setStatusCode(404); return $view; } else { $popularmanga = Top::parse($mangacontent, 'manga'); $view = $this->view($popularmanga); $view->setSerializationContext($serializationContext); $view->setResponse($response); $view->setStatusCode(200); return $view; } }