-
-
Save Jaimies/67fa446ce70390706fbbb7619fcac7e9 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', '~́~', '~'~', '~ü~'); | |
/* Finding the page title tag */ | |
preg_match('/<title>(.*?)<\/title>/', $htmlFirstPage, $output); | |
$pagetitle = $output[1]; | |
/* Removing LoveRead.ec message */ | |
$booktitle = str_replace(' | LoveRead.ec - читать книги онлайн бесплатно', '', $pagetitle); | |
/* For some reason, the '|' symbol is not treated correctly when downloading the file, so replacing it with '-' */ | |
$booktitle = str_replace('|', '-', $booktitle); | |
/* Finally, downloading the book with its title */ | |
header('Content-type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename="' . $booktitle . '.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