Skip to content

Instantly share code, notes, and snippets.

@divinity76
Created November 27, 2022 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinity76/07c8084ae571c0dfcc0f9e29e4c280ce to your computer and use it in GitHub Desktop.
Save divinity76/07c8084ae571c0dfcc0f9e29e4c280ce to your computer and use it in GitHub Desktop.
nhentai downloader
<?php
declare(strict_types=1);
function dd($var)
{
var_dump($var);
die;
}
function fetch(string $url): string
{
static $db = null;
if ($db === null) {
$db = new \PDO('sqlite:' . __FILE__ . '.cache.db', null, null, [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
]);
$db->exec("CREATE TABLE IF NOT EXISTS urlcache(
id INTEGER PRIMARY KEY,
url TEXT NOT NULL UNIQUE,
body BLOB NOT NULL
)");
//dd($db->query("SELECT * FROM urlcache")->fetchAll());
}
$dbResult = $db->query("SELECT body FROM urlcache WHERE url = " . $db->quote($url))->fetchAll();
if (!empty($dbResult)) {
return $dbResult[0]['body'];
}
static $ch = null;
if ($ch === null) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_TIMEOUT => 10,
//CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
CURLOPT_ENCODING => '',
]);
}
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
));
$result = curl_exec($ch);
if ($result === false || curl_errno($ch) !== CURLE_OK) {
throw new Exception('Curl error: ' . curl_error($ch));
}
$sql = "INSERT OR REPLACE INTO urlcache (url,body) VALUES(" . $db->quote($url) . "," . $db->quote($result) . ")";
//dd($sql);
$db->exec($sql);
return $result;
}
function downloadEntireSeries(int $id)
{
$workDir = __DIR__ . '/nhen/' . $id;
if (!is_dir($workDir)) {
if(!mkdir($workDir, 0777, true)){
throw new Exception('Cannot create work dir');
}
}
chdir($workDir);
$url = "https://nhentai.to/g/{$id}";
$html = fetch($url);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//a[contains(@class,"gallerythumb")]');
foreach ($nodes as $node) {
$href = "https://nhentai.to" . $node->getAttribute('href');
$html = fetch($href);
$dom2 = new DOMDocument();
@$dom2->loadHTML($html);
$xpath2 = new DOMXPath($dom2);
$src = $xpath2->query('//section[@id="image-container"]//img')->item(0)->getAttribute('src');
$filename = basename($src);
var_dump($filename);
if (!file_exists($filename)) {
file_put_contents($filename, fetch($src), LOCK_EX);
}
}
}
downloadEntireSeries(399262);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment