/**
  * Get the topic of MAL.
  *
  * @param Request $request HTTP Request object
  * @param int     $id      The ID of the forum topic as assigned by MyAnimeList
  *
  * @return View
  */
 public function getForumTopicAction(Request $request, $id)
 {
     // http://myanimelist.net/forum/?topicid=#{id}
     $page = (int) $request->query->get('page');
     if ($page <= 0) {
         $page = 1;
     }
     if ((int) $id == '') {
         return $this->view(array('error' => 'Invalid topic ID'), 200);
     }
     $downloader = $this->get('atarashii_api.communicator');
     try {
         $forumcontent = $downloader->fetch('/forum/?topicid=' . $id . '&show=' . ($page * 50 - 50));
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     }
     $forumtopic = ForumParser::parseTopic($forumcontent);
     $response = new Response();
     $response->setPublic();
     $response->setMaxAge(300);
     //5 minutes
     $response->headers->addCacheControlDirective('must-revalidate', true);
     $response->setEtag('forum/topic/' . $id);
     //Also, set "expires" header for caches that don't understand Cache-Control
     $date = new \DateTime();
     $date->modify('+300 seconds');
     //5 minutes
     $response->setExpires($date);
     $view = $this->view($forumtopic);
     $view->setResponse($response);
     $view->setStatusCode(200);
     return $view;
 }
Esempio n. 2
0
 public function testParseTopic()
 {
     $topicContent = file_get_contents(__DIR__ . '/../InputSamples/forum-topic-516059.html');
     $topic = ForumParser::parseTopic($topicContent);
     //Some sanity checking on the pages and main content
     $this->assertInternalType('array', $topic);
     $this->assertGreaterThan(0, $topic['pages']);
     $this->assertInternalType('array', $topic['list']);
     $comment = $topic['list'][0];
     //Some sanity checking on the topic details
     $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Forum', $comment);
     $this->assertInternalType('int', $comment->getId());
     $this->assertInternalType('string', $comment->getUsername());
     $this->assertInternalType('string', $comment->getComment());
     $this->assertInstanceOf('\\DateTime', new \DateTime($comment->getTime()));
     //Some sanity checking on profile class in the topic details
     $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Profile', $comment->profile);
     $this->assertInternalType('string', $comment->profile->getAvatarUrl());
     $this->assertInternalType('string', $comment->profile->details->getStatus());
     $this->assertInstanceOf('\\DateTime', new \DateTime($comment->profile->details->getJoinDate()));
     $this->assertInternalType('string', $comment->profile->details->getAccessRank());
     $this->assertInternalType('string', $comment->profile->details->getForumPosts());
 }