/** * Get the messages list. * * @param Request $request HTTP Request object * * @internal param int $page The pagenumber assigned by MAL. * * @return View */ public function getAction(Request $request) { // http://myanimelist.net/mymessages.php?go=&show=#{page} $downloader = $this->get('atarashii_api.communicator'); $page = (int) $request->query->get('page'); if ($page <= 0) { $page = 1; } //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; } Date::setTimeZone($downloader->fetch('/editprofile.php')); $messagesdetails = $downloader->fetch('/mymessages.php?go=&show=' . ($page * 20 - 20)); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } if (strpos($messagesdetails, 'You have 0 messages') !== false) { return $this->view(array('error' => 'No messages found.'), 404); } else { $messages = MessagesParser::parse($messagesdetails); $response = new Response(); $view = $this->view($messages); $view->setResponse($response); $view->setStatusCode(200); return $view; } }
public static function parse($contents, $id, $type) { $crawler = new Crawler(); $crawler->addHTMLContent($contents, 'UTF-8'); $rows = $crawler->filter('div[class="spaceit_pad"]'); $title = preg_replace('/ (\\w+?) Details/', '$2', $crawler->filter('div[class="normal_header"]')->text()); $result = array(); if ($type === 'anime') { foreach ($rows as $historyItem) { $crawler = new Crawler($historyItem); $date = explode(' ', $crawler->text()); $historyinfo['item'] = new Anime(); $historyinfo['item']->setId((int) $id); $historyinfo['item']->setTitle($title); $historyinfo['item']->setWatchedEpisodes((int) $date[1]); $historyinfo['type'] = $type; $historyinfo['time_updated'] = Date::formatTime($date[4] . ' ' . $date[6]); $result[] = $historyinfo; } } else { foreach ($rows as $historyItem) { $crawler = new Crawler($historyItem); $date = explode(' ', $crawler->text()); $historyinfo['item'] = new Manga(); $historyinfo['item']->setId((int) $id); $historyinfo['item']->setTitle($title); $historyinfo['item']->setChaptersRead((int) $date[1]); $historyinfo['type'] = $type; $historyinfo['time_updated'] = Date::formatTime($date[4] . ' ' . $date[6]); $result[] = $historyinfo; } } return $result; }
/** * Set the time property. * * @param DateTime $time The time of messages. */ public function setTime($time) { $this->time = Date::formatTime($time); }
/** * Get the watching history 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 getHistoryAction($id, $requestType) { // http://myanimelist.net/ajaxtb.php?detailedaid=#{id} // http://myanimelist.net/ajaxtb.php?detailedmid=#{id} $downloader = $this->get('atarashii_api.communicator'); //get the credentials we received $username = $this->getRequest()->server->get('PHP_AUTH_USER'); $password = $this->getRequest()->server->get('PHP_AUTH_PW'); //Don't bother making a request if the user didn't send any authentication if ($username === null || $password === null || $username === '' || $password === '') { $view = $this->view(array('error' => 'unauthorized'), 401); $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"'); return $view; } 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 { if ($requestType === 'anime') { $content = $downloader->fetch('/ajaxtb.php?detailedaid=' . $id); } else { $content = $downloader->fetch('/ajaxtb.php?detailedmid=' . $id); } Date::setTimeZone($downloader->fetch('/editprofile.php')); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } catch (Exception\ClientErrorResponseException $e) { $content = $e->getResponse(); } if (strpos($content, 'Not logged in') !== false) { $view = $this->view(array('error' => 'unauthorized'), 401); $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"'); return $view; } else { $result = HistoryParser::parse($content, $id, $requestType); $view = $this->view($result); $view->setResponse(new Response()); $view->setStatusCode(200); } return $view; }
private static function parseTopicsDetails($item) { $crawler = new Crawler($item); if ($crawler->filter('td')->count() >= 4) { $topics = new Forum(); # id. # Example: # <span id="wt439011">...</span> $topics->setId(str_replace('/forum/?topicid=', '', $crawler->filter('td[class="forum_boardrow1"] a')->attr('href'))); //->filter('span')->attr('id'))); # name. # Example: # <a href="/forum/?topicid=439011">BBCode Fixes</a> $topics->setName($crawler->filter('td[class="forum_boardrow1"] a')->text()); # username. # Example: # <a href="/profile/ratan12">ratan12</a> $topics->setUsername(str_replace('?board=', '', $crawler->filter('span[class="forum_postusername"] a')->text())); # replies. # Example: # <td align="center" width="75" class="forum_boardrow2" style="border-width: 0px 1px 1px 0px;">159</td> $topics->setReplies(str_replace('?board=', '', $crawler->filter('td[class="forum_boardrow2"]')->eq(1)->text())); # creation time. # Example: # <span class="lightLink">Jun 25, 2008</span> $topics->setTime($crawler->filter('span[class="lightLink"]')->text()); //note: eq(1) is the second node and !first. $username = $crawler->filter('td[class="forum_boardrow1"]')->eq(1)->filter('a')->text(); $time = explode("»»", $crawler->filter('td[class="forum_boardrow1"]')->eq(1)->text()); $topics->setReply(array('username' => $username, 'time' => Date::formatTime($time[1]))); return $topics; } else { return; } }
/** * Set the date property. * * @param string $date The creation date of this review */ public function setDate($date) { $this->date = Date::formatTime($date); }
/** * Set the status property. * * @param string $broadcast The next broadcasting time and date. */ public function setBroadcast($broadcast) { $this->broadcast = Date::formatTime($broadcast); }
protected function tearDown() { Date::$timeZone = 'America/Los_Angeles'; }
/** * Set the joinDate property. * * This function is for compatibility with certain parts of API1 * * @param string $joinDate The MAL join date of an user. */ public function setJoinDate($joinDate) { $this->joinDate = $joinDate; $this->joinDate2 = Date::formatTime($joinDate); }
public static function parseHistory($contents) { $crawler = new Crawler(); $crawler->addHTMLContent($contents, 'UTF-8'); $maincontent = $crawler->filter('table')->filter('tr'); //Empty array so we return something non-null if the list is empty. $historylist = array(); foreach ($maincontent as $historyentry) { $crawler = new Crawler($historyentry); // bypass for the MAL generated strings if ($crawler->filter('a')->count() > 0) { if (strpos($crawler->filter('a')->attr('href'), 'anime') !== false) { $historyinfo['item'] = new Anime(); $historyinfo['item']->setWatchedEpisodes((int) $crawler->filter('strong')->text()); $historyinfo['type'] = 'anime'; } else { $historyinfo['item'] = new Manga(); $historyinfo['item']->setChaptersRead((int) $crawler->filter('strong')->text()); $historyinfo['type'] = 'manga'; } $historyinfo['item']->setTitle($crawler->filter('a')->text()); $historyinfo['item']->setId((int) str_replace('/' . $historyinfo['type'] . '.php?id=', '', $crawler->filter('a')->attr('href'))); $historyinfo['time_updated'] = Date::formatTime(substr($crawler->filter('td')->eq(1)->text(), 1)); $historylist[] = $historyinfo; } } return $historylist; }