/** * Get the details of an anime or manga. * * @param int $id The ID of the anime or manga as assigned by MyAnimeList * @param string $apiVersion The API version of the request * @param Request $request HTTP Request object * @param string $requestType The anime or manga request string * * @return View */ public function getAction($id, $apiVersion, $requestType, Request $request) { //General information (and basic personal information) at: // http://myanimelist.net/anime/#{id} // http://myanimelist.net/manga/#{id} //Detailed personal information at: // http://myanimelist.net/ownlist/anime/{id}/edit?hideLayout // http://myanimelist.net/ownlist/manga/{id}/edit?hideLayout $usepersonal = (int) $request->query->get('mine'); $downloader = $this->get('atarashii_api.communicator'); if ($usepersonal) { //get the credentials we received $username = $this->getRequest()->server->get('PHP_AUTH_USER'); $password = $this->getRequest()->server->get('PHP_AUTH_PW'); try { if (!$downloader->cookieLogin($username, $password)) { $view = $this->view(array('error' => 'unauthorized'), 401); $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"'); return $view; } } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } } try { $recordDetails = $downloader->fetch('/' . $requestType . '/' . $id); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } catch (Exception\ClientErrorResponseException $e) { $recordDetails = $e->getResponse(); } if (strpos($recordDetails, 'No series found') !== false || strpos($recordDetails, 'This page doesn\'t exist') !== false) { return $this->view(array('error' => 'not-found'), 404); } else { if ($requestType === 'anime') { $record = AnimeParser::parse($recordDetails, $apiVersion); } else { $record = MangaParser::parse($recordDetails, $apiVersion); } //Parse extended personal details if API 2.0 or better and personal details are requested if ($apiVersion >= '2.0' && $usepersonal) { try { $recordDetails = $downloader->fetch('/ownlist/' . $requestType . '/' . $id . '/edit?hideLayout'); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } if (strpos($recordDetails, 'delete-form') !== false) { if ($requestType === 'anime') { $record = AnimeParser::parseExtendedPersonal($recordDetails, $record); } else { $record = MangaParser::parseExtendedPersonal($recordDetails, $record); } } } $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); } //Only include cache info if it doesn't include personal data. if (!$usepersonal) { $response->setPublic(); $response->setMaxAge(3600); //One hour $response->headers->addCacheControlDirective('must-revalidate', true); $response->setEtag($requestType . '/' . $id); //Also, set "expires" header for caches that don't understand Cache-Control $date = new \DateTime(); $date->modify('+3600 seconds'); //One hour $response->setExpires($date); } $view = $this->view($record); $view->setSerializationContext($serializationContext); $view->setResponse($response); $view->setStatusCode(200); return $view; } }
/** * @covers ::parseExtendedPersonal */ public function testParseExtendedPersonal() { $anime = new Anime(); $animeContents = file_get_contents(__DIR__ . '/../InputSamples/anime-1689-mine-detailed.html'); AnimeParser::parseExtendedPersonal($animeContents, $anime); $this->assertInternalType('array', $anime->getPersonalTags()); $this->assertContains('beautiful', $anime->getPersonalTags()); $this->assertEquals('2013-03-04', $anime->getWatchingStart()->format('Y-m-d')); $this->assertEquals('2013-03-04', $anime->getWatchingEnd()->format('Y-m-d')); $this->assertEquals('High', $anime->getPriority('string')); $this->assertEquals(4, $anime->getStorage()); $this->assertEquals(1, $anime->getStorageValue()); $this->assertEquals(0, $anime->getRewatchCount()); $this->assertEquals('High', $anime->getRewatchValue('string')); $this->assertStringStartsWith('The beautiful art direction', $anime->getPersonalComments()); }