Skip to content

Instantly share code, notes, and snippets.

@fnx4
Last active December 25, 2021 22:15
Show Gist options
  • Save fnx4/aba7ca67dedb93eee5f77262b79ffcd3 to your computer and use it in GitHub Desktop.
Save fnx4/aba7ca67dedb93eee5f77262b79ffcd3 to your computer and use it in GitHub Desktop.
Ранобэ.рф парсер
<html>
<head>
<meta charset="UTF-8">
<title>Ранобэ.рф парсер</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
* {
font-family: sans-serif;
}
</style>
</head>
<body>
<?php
//example:
//https://fnx4.ru/parser/index.php?id=9
//or
//https://fnx4.ru/parser/index.php?id=9&start=527
//or
//https://fnx4.ru/parser/index.php?id=9&end=531
//or
//https://fnx4.ru/parser/index.php?id=9&start=527&end=531
ini_set('max_execution_time', 65535);
if (isset($_GET["id"])){
$id = $_GET["id"]; // F12 -> JSON.parse(document.getElementById("__NEXT_DATA__").innerHTML).props.pageProps.book.id
$start = (isset($_GET["start"])) ? ($_GET["start"]) : 1;
$xmlList = file_get_contents('https://xn--80ac9aeh6f.xn--p1ai/v3/chapters?filter[bookId]='.$id);
$listData = json_decode($xmlList);
foreach ($listData -> items as $i => $listValue){
$chapter[$i] = $listValue;
}
$end = (isset($_GET["end"])) ? ($_GET["end"]) : count($chapter);
foreach (array_reverse($chapter) as $j => $value){
$chapterNumber = $j + 1;
if (($chapterNumber >= $start) && ($chapterNumber <= $end)) {
$url = 'https://xn--80ac9aeh6f.xn--p1ai'.($value -> url);
$bookHtml = file_get_contents($url);
$bookHtml = mb_convert_encoding($bookHtml, 'HTML-ENTITIES', 'UTF-8');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom -> loadHTML($bookHtml);
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
$chapter = $xpath -> query("//div[contains(@class, 'text-base')]/*");
$chapterName = ($value -> title);
if(count($chapter) > 0) {
$chapterText = '<h2>['.$chapterNumber.'] '.$chapterName.'</h2>';
foreach ($chapter as $node) {
$chapterText .= '<p>' . $node -> nodeValue . '</p>';
}
print_r($chapterText);
} else {
print_r('<p style="color: gray">['.$chapterNumber.'] '.$chapterName.' (ошибка парсера или платная глава)</p>');
}
}
}
echo "<a href = https://gist.github.com/fnx4/aba7ca67dedb93eee5f77262b79ffcd3>github</a>";
}
else {
$err = "<h2>Error!</h2> Use: <br/>";
$err .= ".../index.php?id=BOOK-ID-HERE<br/>";
$err .= "or<br/>";
$err .= ".../index.php?id=BOOK-ID-HERE&start=CHAPTER<br/>";
$err .= "or<br/>";
$err .= ".../index.php?id=BOOK-ID-HERE&end=CHAPTER<br/>";
$err .= "or<br/>";
$err .= ".../index.php?id=BOOK-ID-HERE&start=CHAPTER&end=CHAPTER<br/><br/>";
$err .= "example: https://fnx4.ru/parser/index.php?id=9&start=527&end=531";
die ($err);
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment