示例#1
0
 public function testMalXml()
 {
     $manga = new Manga();
     $items = array();
     $output = "<?xml version=\"1.0\"?>\n<entry><chapter>7</chapter><volume>7</volume><status>1</status><score>9</score><downloaded_chapters>8</downloaded_chapters><times_reread>1</times_reread><reread_value>4</reread_value><date_start>01012015</date_start><date_finish>01022015</date_finish><priority>0</priority><comments>This is a comment.</comments><tags>one,two,three</tags></entry>\n";
     $manga->setId(1);
     $manga->setChaptersRead(7);
     $items[] = 'chapters';
     $manga->setVolumesRead(7);
     $items[] = 'volumes';
     $manga->setReadStatus(1);
     $items[] = 'status';
     $manga->setScore(9);
     $items[] = 'score';
     $manga->setChapDownloaded(8);
     $items[] = 'downloaded';
     $manga->setRereadCount(1);
     $items[] = 'rereadCount';
     $manga->setRereadValue(4);
     $items[] = 'rereadValue';
     $manga->setReadingStart(new \DateTime('20150101'));
     $items[] = 'start';
     $manga->setReadingEnd(new \DateTime('20150102'));
     $items[] = 'end';
     $manga->setPriority(6);
     $items[] = 'priority';
     $manga->setPersonalComments('This is a comment.');
     $items[] = 'comments';
     $manga->setPersonalTags('one,two,three');
     $items[] = 'tags';
     $this->assertEquals($output, $manga->MALApiXml($items));
 }
 /**
  * Update a manga on a user's list.
  *
  * Uses the contents of the HTTP Request to get the needed data for updating the
  * requested title. The user must have passed the basic authentication needs and the
  * PHP_AUTH_USER and PHP_AUTH_PW variables must be set. If so, the get variables of
  * "status", "chapters", "volumes", and "score" are checked and used in the creation
  * of a manga object. The object is used to make an XML document that is then posted
  * to MyAnimeList.
  *
  * @param Request $request Contains all the needed information to update the title.
  * @param int     $id      ID of the manga.
  *
  * @return View
  */
 public function updateAction(Request $request, $id, $apiVersion)
 {
     // http://mymangalist.net/api/mangalist/update/#{id}.xml
     //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) {
         $view = $this->view(array('error' => 'unauthorized'), 401);
         $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
         return $view;
     }
     $manga = new Manga();
     $manga->setId($id);
     //Only use values we were sent for the Update XML
     $update_items = array();
     try {
         if ($request->request->get('status') !== null) {
             $manga->setReadStatus($request->request->get('status'));
             $update_items[] = 'status';
         }
         if ($request->request->get('chapters') !== null) {
             $manga->setChaptersRead($request->request->get('chapters'));
             $update_items[] = 'chapters';
         }
         if ($request->request->get('volumes') !== null) {
             $manga->setVolumesRead($request->request->get('volumes'));
             $update_items[] = 'volumes';
         }
         if ($request->request->get('score') !== null) {
             $manga->setScore($request->request->get('score'));
             $update_items[] = 'score';
         }
         //API 2 Items
         if ($apiVersion >= 2.0) {
             if ($request->request->get('downloaded_chap') !== null) {
                 $manga->setChapDownloaded($request->request->get('downloaded_chap'));
                 //Int
                 $update_items[] = 'downloaded';
             }
             if ($request->request->get('reread_count') !== null) {
                 $manga->setRereadCount($request->request->get('reread_count'));
                 //Int
                 $update_items[] = 'rereadCount';
             }
             if ($request->request->get('reread_value') !== null) {
                 $manga->setRereadValue($request->request->get('reread_value'));
                 //Int
                 $update_items[] = 'rereadValue';
             }
             if ($request->request->get('start') !== null) {
                 $manga->setReadingStart(DateTime::createFromFormat('Y-m-d', $request->request->get('start')));
                 //Needs to be DT!
                 $update_items[] = 'start';
             }
             if ($request->request->get('end') !== null) {
                 $manga->setReadingEnd(DateTime::createFromFormat('Y-m-d', $request->request->get('end')));
                 //Needs to be DT!
                 $update_items[] = 'end';
             }
             if ($request->request->get('priority') !== null) {
                 $manga->setPriority($request->request->get('priority'));
                 $update_items[] = 'priority';
             }
             if ($request->request->get('is_rereading') !== null) {
                 $manga->setRereading($request->request->get('is_rereading'));
                 //Bool - 0 = no, 1 = yes
                 $update_items[] = 'isRereading';
             }
             if ($request->request->get('comments') !== null) {
                 $manga->setPersonalComments($request->request->get('comments'));
                 //Plain text string. No HTML.
                 $update_items[] = 'comments';
             }
             if ($request->request->get('tags') !== null) {
                 $manga->setPersonalTags($request->request->get('tags'));
                 //Comma-separated string
                 $update_items[] = 'tags';
             }
         }
     } catch (\Exception $e) {
         return $this->view(array('error' => $e->getMessage()), 500);
     }
     $xmlcontent = $manga->MALApiXml($update_items);
     $connection = $this->get('atarashii_api.communicator');
     try {
         $connection->sendXML('/api/mangalist/update/' . $manga->getId() . '.xml', $xmlcontent, $username, $password);
         return $this->view('ok', 200);
     } catch (Exception\ClientErrorResponseException $e) {
         $view = $this->view(array('error' => 'unauthorized'), 401);
         $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
         return $view;
     } catch (Exception\ServerErrorResponseException $e) {
         //MAL broke API responses, so we have to check the content on the response to make sure
         //it actually was an error.
         $response = $e->getResponse()->getBody(true);
         if (stripos($response, 'Updated') === 0) {
             return $this->view('ok', 200);
         }
         return $this->view(array('error' => 'not-found'), 404);
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     }
 }
 public static function parseExtendedPersonal($contents, Manga $manga)
 {
     $crawler = new Crawler();
     $crawler->addHTMLContent($contents, 'UTF-8');
     #Personal tags
     #<td align="left" class="borderClass"><textarea name="tags" rows="2" id="tagtext" cols="45" class="textarea"></textarea><div class="spaceit_pad"><small>Popular tags: <a href="javascript:void(0);" onclick="detailedadd_addTag('cooking');">cooking</a>, <a href="javascript:void(0);" onclick="detailedadd_addTag('seinen');">seinen</a>, <a href="javascript:void(0);" onclick="detailedadd_addTag('drama');">drama</a>, <a href="javascript:void(0);" onclick="detailedadd_addTag('slice of life');">slice of life</a></small></div></td>
     $personalTags = $crawler->filter('textarea[id="add_manga_tags"]')->text();
     if (strlen($personalTags) > 0) {
         $personalTags = explode(',', $personalTags);
         foreach ($personalTags as $tag) {
             $tagArray[] = trim($tag);
         }
         $manga->setPersonalTags($tagArray);
     }
     #Start and Finish Dates
     #<tr>
     #    <td align="left" class="borderClass">Start Date</td>
     #                <td align="left" class="borderClass">
     #    Month:
     #    <select name="startMonth" id="smonth"  class="inputtext">
     #        <option value="00">
     #        <option value="01" >Jan<option value="02" >Feb<option value="03" >Mar<option value="04" >Apr<option value="05" >May<option value="06" >Jun<option value="07" >Jul<option value="08" >Aug<option value="09" selected>Sep<option value="10" >Oct<option value="11" >Nov<option value="12" >Dec			</select>
     #    Day:
     #    <select name="startDay"  class="inputtext">
     #        <option value="00">
     #        <option value="01" >1<option value="02" >2<option value="03" >3<option value="04" >4<option value="05" >5<option value="06" >6<option value="07" >7<option value="08" >8<option value="09" >9<option value="10" >10<option value="11" >11<option value="12" >12<option value="13" >13<option value="14" >14<option value="15" >15<option value="16" >16<option value="17" >17<option value="18" >18<option value="19" >19<option value="20" >20<option value="21" >21<option value="22" >22<option value="23" >23<option value="24" >24<option value="25" selected>25<option value="26" >26<option value="27" >27<option value="28" >28<option value="29" >29<option value="30" >30<option value="31" >31			</select>
     #    Year:
     #    <select name="startYear"  class="inputtext">
     #        <option value="0000">
     #        <option value="2014" >2014<option value="2013" selected>2013<option value="2012" >2012<option value="2011" >2011<option value="2010" >2010<option value="2009" >2009<option value="2008" >2008<option value="2007" >2007<option value="2006" >2006<option value="2005" >2005<option value="2004" >2004<option value="2003" >2003<option value="2002" >2002<option value="2001" >2001<option value="2000" >2000<option value="1999" >1999<option value="1998" >1998<option value="1997" >1997<option value="1996" >1996<option value="1995" >1995<option value="1994" >1994<option value="1993" >1993<option value="1992" >1992<option value="1991" >1991<option value="1990" >1990<option value="1989" >1989<option value="1988" >1988<option value="1987" >1987<option value="1986" >1986<option value="1985" >1985<option value="1984" >1984			</select>
     #    &nbsp;
     #    <label><input type="checkbox"  onchange="ChangeStartDate();" name="unknownStart" value="1"> <small>Unknown Date</label><br>Start Date represents the date you started watching the Anime <a href="javascript:setToday(1);">Insert Today</a></small>
     #    </td>
     #</tr>
     #<tr>
     #    <td align="left" class="borderClass">Finish Date</td>
     #                <td align="left" class="borderClass">
     #    Month:
     #    <select name="endMonth" id="emonth" class="inputtext" >
     #        <option value="00">
     #        <option value="01" >Jan<option value="02" >Feb<option value="03" >Mar<option value="04" >Apr<option value="05" >May<option value="06" >Jun<option value="07" >Jul<option value="08" >Aug<option value="09" >Sep<option value="10" selected>Oct<option value="11" >Nov<option value="12" >Dec			</select>
     #    Day:
     #    <select name="endDay" class="inputtext" >
     #        <option value="00">
     #        <option value="01" >1<option value="02" >2<option value="03" >3<option value="04" >4<option value="05" >5<option value="06" >6<option value="07" >7<option value="08" >8<option value="09" >9<option value="10" >10<option value="11" selected>11<option value="12" >12<option value="13" >13<option value="14" >14<option value="15" >15<option value="16" >16<option value="17" >17<option value="18" >18<option value="19" >19<option value="20" >20<option value="21" >21<option value="22" >22<option value="23" >23<option value="24" >24<option value="25" >25<option value="26" >26<option value="27" >27<option value="28" >28<option value="29" >29<option value="30" >30<option value="31" >31			</select>
     #    Year:
     #    <select name="endYear" class="inputtext" >
     #        <option value="0000">
     #        <option value="2014" >2014<option value="2013" selected>2013<option value="2012" >2012<option value="2011" >2011<option value="2010" >2010<option value="2009" >2009<option value="2008" >2008<option value="2007" >2007<option value="2006" >2006<option value="2005" >2005<option value="2004" >2004<option value="2003" >2003<option value="2002" >2002<option value="2001" >2001<option value="2000" >2000<option value="1999" >1999<option value="1998" >1998<option value="1997" >1997<option value="1996" >1996<option value="1995" >1995<option value="1994" >1994<option value="1993" >1993<option value="1992" >1992<option value="1991" >1991<option value="1990" >1990<option value="1989" >1989<option value="1988" >1988<option value="1987" >1987<option value="1986" >1986<option value="1985" >1985<option value="1984" >1984			</select>
     #    &nbsp;
     #    <small><label><input type="checkbox" onchange="ChangeEndDate();"  name="unknownEnd" value="1"> Unknown Date</label><br>Do <u>not</u> fill out the Finish Date unless status is <em>Completed</em> <a href="javascript:setToday(2);">Insert Today</a></small>
     #    </td>
     #</tr>
     $isStarted = $crawler->filter('input[id="unknown_start"]')->attr('checked');
     $isEnded = $crawler->filter('input[id="unknown_end"]')->attr('checked');
     if ($isStarted != 'checked') {
         //So, MAL allows users to put in just years, just years and months, or all three values.
         //This mess here is to try and avoid things breaking.
         if ($crawler->filter('select[id="add_manga_start_date_year"] option:selected')->count() > 0) {
             $startYear = $crawler->filter('select[id="add_manga_start_date_year"] option:selected')->attr('value');
             $startMonth = 6;
             $startDay = 15;
             if ($startYear !== '') {
                 if ($crawler->filter('select[id="add_manga_start_date_month"] option:selected')->count() > 0) {
                     $startMonth = $crawler->filter('select[id="add_manga_start_date_month"] option:selected')->attr('value');
                     if ($crawler->filter('select[id="add_manga_start_date_day"] option:selected')->count() > 0) {
                         $startDay = $crawler->filter('select[id="add_manga_start_date_day"] option:selected')->attr('value');
                     }
                 }
                 $manga->setReadingStart(DateTime::createFromFormat('Y-n-j', "{$startYear}-{$startMonth}-{$startDay}"));
             }
         }
     }
     if ($isEnded != 'checked') {
         //Same here, avoid breaking MAL's allowing of partial dates.
         if ($crawler->filter('select[id="add_manga_finish_date_year"] option:selected')->count() > 0) {
             $endYear = $crawler->filter('select[id="add_manga_finish_date_year"] option:selected')->attr('value');
             $endMonth = 6;
             $endDay = 15;
             if ($endYear !== '') {
                 if ($crawler->filter('select[id="add_manga_finish_date_month"] option:selected')->count() > 0) {
                     $endMonth = $crawler->filter('select[id="add_manga_finish_date_month"] option:selected')->attr('value');
                     if ($crawler->filter('select[id="add_manga_finish_date_day"] option:selected')->count() > 0) {
                         $endDay = $crawler->filter('select[id="add_manga_finish_date_day"] option:selected')->attr('value');
                     }
                 }
                 $manga->setReadingEnd(DateTime::createFromFormat('Y-n-j', "{$endYear}-{$endMonth}-{$endDay}"));
             }
         }
     }
     #Priority
     #<td align="left" class="borderClass">Priority</td>
     #<td align="left" class="borderClass"><select name="priority" class="inputtext">
     #<option value="0">Select</option>
     #<option value="0" selected>Low<option value="1" >Medium<option value="2" >High                </select>
     #<div style="margin-top 3px;"><small>What is your priority level to read this manga?</small></div></td>
     $priorityList = $crawler->filter('select[id="add_manga_priority"] option:selected');
     if (count($priorityList)) {
         $priority = $priorityList->attr('value');
         $manga->setPriority($priority);
     }
     #Rewatched
     #<label><input type="checkbox" id="add_manga_is_rereading" name="add_manga[is_rereading]" value="1" checked="checked">
     $reread = $crawler->filter('input[id="add_manga_is_rereading"]')->attr('checked');
     if ($reread == null) {
         $manga->setRereading(false);
     } else {
         $manga->setRereading(true);
     }
     #Times Reread
     #<td align="left" class="borderClass"><input type="text" class="inputtext" size="4" value="0" name="times_read">
     $rereadCount = $crawler->filter('input[id="add_manga_num_read_times"]')->attr('value');
     if ($rereadCount > 0) {
         $manga->setRereadCount($rereadCount);
     }
     #Reread Value
     #<td align="left" class="borderClass"><select class="inputtext" name="reread_value">
     #	<option value="0">Select reread value</option><option value="1">Very Low</option><option value="2">Low</option><option value="3">Medium</option><option value="4">High</option><option value="5">Very High			</option></select>
     $rereadValue = $crawler->filter('select[id="add_manga_reread_value"] option:selected');
     if (count($rereadValue)) {
         $manga->setRereadValue($rereadValue->attr('value'));
     }
     #Comments
     #<td align="left" class="borderClass"><textarea class="textarea" cols="45" rows="5" name="comments"></textarea></td>
     $comments = trim($crawler->filter('textarea[id="add_manga_comments"]')->text());
     if (strlen($comments)) {
         $manga->setPersonalComments($comments);
     }
     return $manga;
 }