public function testParse() { $recsContents = file_get_contents(__DIR__ . '/../InputSamples/anime-21-recs.html'); $content = RecsParser::parse($recsContents); $this->assertInternalType('array', $content); $recsContent = $content[0]; $this->assertInternalType('array', $recsContent); $this->assertEquals(6702, $recsContent['item']->getId()); $this->assertInternalType('string', $recsContent['item']->getTitle()); $this->assertInternalType('string', $recsContent['item']->getImageUrl()); $this->assertInternalType('string', $recsContent['recommendations'][0]['information']); $this->assertInternalType('string', $recsContent['recommendations'][0]['username']); $recsContents = file_get_contents(__DIR__ . '/../InputSamples/manga-21-recs.html'); $content = RecsParser::parse($recsContents); $this->assertInternalType('array', $content); $recsContent = $content[0]; $this->assertInternalType('array', $recsContent); $this->assertEquals(1649, $recsContent['item']->getId()); $this->assertInternalType('string', $recsContent['item']->getTitle()); $this->assertInternalType('string', $recsContent['item']->getImageUrl()); $this->assertInternalType('string', $recsContent['recommendations'][0]['information']); $this->assertInternalType('string', $recsContent['recommendations'][0]['username']); }
/** * Get the recommendations of an anime or manga. * * @param int $id The ID of the anime or manga as assigned by MyAnimeList * @param string $requestType The anime or manga request string * * @return View */ public function getRecsAction($id, $requestType) { // http://myanimelist.net/anime/#{id}/_/userrecs // http://myanimelist.net/manga/#{id}/_/userrecs $downloader = $this->get('atarashii_api.communicator'); try { $details = $downloader->fetch('/' . $requestType . '/' . $id . '/_/userrecs'); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } if (strpos($details, 'No recommendations have been made for this title.') !== false) { return $this->view(array('error' => 'not-found'), 200); } else { $result = RecsParser::parse($details); $response = new Response(); $response->setPublic(); $response->setMaxAge(86400); //Two day $response->headers->addCacheControlDirective('must-revalidate', true); $response->setEtag($requestType . '/recs/' . $id); //Also, set "expires" header for caches that don't understand Cache-Control $date = new \DateTime(); $date->modify('+86400 seconds'); //Two days $response->setExpires($date); $view = $this->view($result); $view->setResponse($response); $view->setStatusCode(200); return $view; } }