function ao3($url) { $time_start = microtime(true); //////// TESTING PURPOSES $chapters = array(); $moreChapters = true; $story = new Story(); // echo "<p>old url = " . $url . "</p>"; //////// TESTING PURPOSES if (!strpos($url, "view_full_work=true")) { if (strpos($url, "view_adult=true")) { $url = $url . "&view_full_work=true"; } else { $url = $url . "?view_full_work=true"; } } if (!strpos($url, "view_adult=true")) { $url = $url . "&view_adult=true"; } $redirect = true; $ch = curl_init(); $page = new simple_html_dom(); while ($redirect) { // echo "<p>new url = $url</p>"; //////// TESTING PURPOSES curl_setopt($ch, CURLOPT_COOKIE, "birthtime=28801; path=/; domain=archiveofourown.org"); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); $page->load($result); $firsthref = $page->find('a', 0); if (strpos($firsthref->plaintext, "edirected")) { $url = $firsthref->getAttribute('href') . "?view_adult=true&view_full_work=true"; } else { // --------- REMOVE THIS IF STATEMENT --------- \\ // it's just for testing purposes, has no place in the final app. if ($page->find('#chapters')) { echo "<h1 style='color: red'>view_full_work mode worked!</h1>"; } // -------------------------------------------- \\ curl_close($ch); $redirect = false; } } ///////////////////////////////////////////////////////////////////////////////////////// $story->title = trim($page->find('h2.title', 0)->plaintext); $story->author = $page->find('h3.byline a', 0)->plaintext; if ($page->find('#chapters .chapter')) { $story->totalChapters = count($page->find('#chapters .chapter div.userstuff')); } else { $story->totalChapters = 1; } echo "<p><span class='bold'>Execution time before for loop:</span> " . (microtime(true) - $time_start) . " seconds</p>"; //////// TESTING PURPOSES for ($i = 0; $i < $story->totalChapters; $i++) { // echo "loop $i<br>"; //////// TESTING PURPOSES $page->removeNode('div.chapter div.userstuff h3.landmark', 0); $chapter = new StoryChapter(); $chapter->chapterID = $i; // if there is a chapter title, add it, else create a title $cID = "#chapter-" . ($i + 1); $chapterTitle = $page->find($cID . ' h3.title a', 0)->plaintext; if ($chapterTitle == null) { $chapter->chapterTitle = "Chapter " . ($i + 1); } else { $chapter->chapterTitle = $chapterTitle; } // if there are chapter notes, add them to chapter text $notes = ""; if ($i == 0) { if ($page->find('div.preface div.summary')) { $story->summary = $page->find('div.preface div.summary blockquote', 0)->outertext; } if ($page->find('div.preface div.notes')) { $notes = "<div class='chapter-notes'><h4>Notes:</h4>" . $page->find('div.preface div.notes blockquote', 0)->outertext . "</div><hr>"; } } else { if ($page->find($cID . ' div.notes')) { $notes = "<div class='chapter-notes'><h4>Notes:</h4>" . $page->find($cID . ' div.notes blockquote', 0)->outertext . "</div><hr>"; } } if ($story->totalChapters == 1) { $chapter->chapterText = $notes . "<div class='chapter-text'>" . $page->find('#chapters div.userstuff', 0)->innertext . "</div>"; } else { $chapter->chapterText = $notes . "<div class='chapter-text'>" . $page->find($cID . ' div.userstuff', 0)->innertext . "</div>"; } $chapters[] = $chapter; // add Chapter object to chapters array echo " <span class='bold'>Loop {$i} execution time:</span> " . (microtime(true) - $time_start) . " seconds<br>"; //////// TESTING PURPOSES } $story->chapters = $chapters; echo "<p><span class='bold'>Execution time after for loop:</span> " . (microtime(true) - $time_start) . " seconds</p>"; //////// TESTING PURPOSES echo "<h1 id='top'>" . $story->title . "</h1>"; echo "<h2>by " . $story->author . "</h2>"; echo "<p><span class='bold'>Total number of chapters:</span> " . $story->totalChapters . ".</p>"; if ($story->summary != null) { echo "<div id='story-summary'><span class='bold'>Summary:</span><br>" . $story->summary . "</div>"; } $toc = "<p><span class='bold'>Table of Contents:</span></p><ol id='table-of-contents'>"; $chapterList = "<p><span class='bold'>Chapters:</span></p><ol id='chapter-list'>"; for ($i = 0; $i < $story->totalChapters; $i++) { $c = $story->chapters[$i]; // copies relevant chapter for ease of access $iplus = $i + 1; $toc = $toc . "<li><a href='#chapter-{$iplus}'>" . $c->chapterTitle . "</a></li>"; $chapterList = $chapterList . "<li id='chapter-{$iplus}'><h3 class='chapter-title'>" . $c->chapterTitle . " <span class='toplink'><a href='#top'>[top]</a></span></h3>" . $c->chapterText . "</li>"; } echo "<p><span class='bold'>Total execution time:</span> " . (microtime(true) - $time_start) . " seconds</p>"; //////// TESTING PURPOSES echo $toc . "</ol>"; echo $chapterList . "</ol>"; }