Last active
November 30, 2021 08:25
-
-
Save NewEXE/9e78f7be801c1db36a57aa27afbb4911 to your computer and use it in GitHub Desktop.
LoveRead.ec parser (PHP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Book ID on loveread.ec | |
* For example, for | |
* http://loveread.ec/read_book.php?id=2555&p=1 | |
* ID is 2555. | |
* | |
* @param int $id | |
*/ | |
public function getFromLoveread(int $id) { | |
$htmlFirstPage = iconv('windows-1251', 'utf-8', file_get_contents("http://loveread.ec/read_book.php?id=$id&p=1")); | |
@preg_match("~…<a href='read_book.php\?id=$id&p=[0-9]+~", $htmlFirstPage, $pagesCount); | |
$pagesCount = ltrim(substr($pagesCount[0], -3), '='); | |
if(! is_numeric($pagesCount) || empty($pagesCount)) { | |
$pagesCount = ltrim(substr($pagesCount[0], -4), '='); | |
} | |
if($pagesCount) { | |
$patterns = array('~(\<(/?[^>]+)>)~is', '~́~', '~'~', '~ü~'); | |
header('Content-type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename="'.$id.'.txt"'); | |
for($p = 1; $p <= $pagesCount; $p++) { | |
$url = "http://loveread.ec/read_book.php?id=$id&p=$p"; | |
$html = file_get_contents($url); | |
$html = iconv('windows-1251', 'utf-8', $html); | |
preg_match('~<p.*class=MsoNormal>(.*?)</p>~is', $html, $matches); | |
$content = preg_replace($patterns, '', $matches[0]); | |
echo $content; | |
} | |
exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:)