Skip to content

Instantly share code, notes, and snippets.

@NewEXE
Last active November 30, 2021 08:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NewEXE/9e78f7be801c1db36a57aa27afbb4911 to your computer and use it in GitHub Desktop.
Save NewEXE/9e78f7be801c1db36a57aa27afbb4911 to your computer and use it in GitHub Desktop.
LoveRead.ec parser (PHP)
<?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("~&#8230;<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', '~&#769;~', '~&#039;~', '~&#252;~');
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);
}
}
@Jaimies
Copy link

Jaimies commented Sep 24, 2019

Hi, Vlad!
I've looked over your Loveread parser and have developed an enhanced version. It saves the file with a name of the book, that is specified on Loveread. So, instead of saving the file 8107.txt it will save 'Игра престолов - Джордж Мартин.txt'
You can check out my gist here: https://gist.github.com/Jaimies/67fa446ce70390706fbbb7619fcac7e9
What do you think?

@NewEXE
Copy link
Author

NewEXE commented Sep 26, 2019

Hi, Jaimies! I use this function sometimes. Thanks for realizing this "nice-to-have" feature

@Jaimies
Copy link

Jaimies commented Sep 26, 2019

:)

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