Skip to content

Instantly share code, notes, and snippets.

@antonve
Last active October 17, 2018 15:16
Show Gist options
  • Save antonve/b5925ad70513f43147f5f3c0f765e39d to your computer and use it in GitHub Desktop.
Save antonve/b5925ad70513f43147f5f3c0f765e39d to your computer and use it in GitHub Desktop.
Quick & dirty Syosetsu reader
<?php
$count = 0;
$series = 'n9669bk';
$br = php_sapi_name() === 'cli' ? "\n" : "<br />";
$chapter_range = [
"from" => 10,
"until" => 12,
];
for ($i = $chapter_range["from"]; $i <= $chapter_range["until"]; $i++) {
$data = file_get_contents('http://ncode.syosetu.com/' . $series . '/' . $i . '/');
$dom = new DOMDocument;
@$dom->loadHTML($data);
$xpath = new DOMXpath($dom);
$text = $xpath->query('//div[@id="novel_honbun"]');
$txt = $text->item(0)->textContent;
$cnt = mb_strlen($txt, 'utf-8');
$count += $cnt;
echo $i . ": " . $cnt . $br;
}
echo 'Total: ' . $count;
<?php
$series = 'n9669bk';
$chapter = intval($_GET['ch']);
$data = file_get_contents('http://ncode.syosetu.com/' . $series . '/' . $chapter . '/');
$dom = new DOMDocument;
@$dom->loadHTML($data);
$xpath = new DOMXpath($dom);
$text = $xpath->query('//div[@id="novel_honbun"]');
$title = $xpath->query('//p[@class="novel_subtitle"]');
$progress = $xpath->query('//div[@id="novel_no"]');
function innerXML($node)
{
$doc = $node->ownerDocument;
$frag = $doc->createDocumentFragment();
foreach ($node->childNodes as $child)
{
$frag->appendChild($child->cloneNode(TRUE));
}
return $doc->saveXML($frag);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>無職転生</title>
<style media="screen">
body, html {
direction: rtl;
background-color: #222233;
color: #EDEDED;
font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'Segoe UI', 'Hiragino Kaku Gothic ProN', 'Yu Gothic', sans-serif;
font-size: 62.5%;
width: 100%;
overflow: hidden;
}
body::after {
content: ' ';
position: fixed;
top: -200px; bottom: -200px;
left: 0;
width: 0;
z-index: 100;
box-shadow: 0px 0px 30px 30px rgba(34,34,51,1);
}
body::before {
content: ' ';
position: fixed;
top: -200px; bottom: -200px;
right: 0;
width: 0;
z-index: 100;
box-shadow: 0px 0px 30px 30px rgba(34,34,51,1);
}
::-webkit-scrollbar {
display: none;
}
header {
direction: ltr;
position: fixed;
top: 0;
left: 0;
right: 0;
height: 100px;
text-align: center;
z-index: 200;
box-sizing: border-box;
}
h1 {
margin: 0;
width: 100%;
padding: 0;
box-sizing: border-box;
text-align: center;
font-size: 3em;
line-height: 100px;
}
footer {
direction: ltr;
z-index: 200;
position: fixed;
bottom: 0;
left: 0;
right: 0;
text-align: left;
opacity: .4;
padding: 20px;
box-sizing: border-box;
font-size: 2em;
}
a {
color: #fff;
text-decoration: none;
}
.novel {
direction: ltr;
margin: 0 auto;
margin-top: 100px;
padding: 0 100px;
writing-mode: vertical-rl;
line-height: 2;
font-size: 3.5em;
height: 500px;
max-height: calc(100% - 200px);
overflow-x: scroll;
width: 100%;
box-sizing: border-box;
}
</style>
</head>
<body>
<header>
<h1><?php echo $title->item(0)->nodeValue; ?></h1>
</header>
<p class="novel">
<?php echo innerXML($text->item(0)) ?>
</p>
<footer>
Chapter
<a href="?ch=<?php echo $chapter-1; ?>">&laquo;</a>
<?php echo $progress->item(0)->nodeValue; ?>
<a href="?ch=<?php echo $chapter+1; ?>">&raquo;</a>
</footer>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</body>
</html>
@antonve
Copy link
Author

antonve commented Jan 1, 2018

Preview

screen shot 2018-01-01 at 19 26 19

Start php server with

Update with the path you save the scripts in.

cd ~/xdev/reader_syosetu
php -S localhost:8000

Visit reader at http://localhost:8000/reader.php?ch=10 and update the ch parameter with the chapter you want to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment