Skip to content

Instantly share code, notes, and snippets.

@spicydog
Last active December 22, 2015 17:20
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 spicydog/c7aadaccc20dfab874d7 to your computer and use it in GitHub Desktop.
Save spicydog/c7aadaccc20dfab874d7 to your computer and use it in GitHub Desktop.
<?php
chdir(__DIR__);
define('LAST_FILE', 'last_manga.json');
define('MANGA_DIR', 'manga');
$names = [];
$names[] = 'terra-formars';
$names[] = 'fairy-tail';
$names[] = 'one-piece';
$names[] = 'fairy-tail-gaiden-road-knight';
$names[] = 'one-piece-party';
$names[] = 'fairy-tail-sabertooth';
$names[] = 'fairy-tail-gaiden-kengami-no-souryuu';
$names[] = 'fairy-girls';
// $names[] = 'fairy-tail-zero';
// $names[] = 'fairy-tail-blue-mistral';
// $names[] = 'fairy-tail-christmas-special';
// $names[] = 'fairy-tail-side-story';
// $names[] = 'fairy-tail-special';
// $names[] = 'tale-of-fairy-ice-trail-koori-no-kiseki';
$name = $names[rand() % count($names)];
$chapter = '1';
$page = 1;
$limit = 100;
echo "downloading: $name\n";
for($i=0;$i<$limit;$i++) {
$nextJob = getJob($name,+1);
$chapter = $nextJob['chapter'];
$page = $nextJob['page'];
$url = "http://www.mangapanda.com/$name/$chapter/";
$html = @file_get_contents($url.$page);
$lastPage = subStringBetween($html,'/select> of ','</div');
if(strlen($lastPage) < 1 && strlen($lastPage) > 3) {
echo "done\n";
exit();
}
$imageUrl = grabImageUrl($html);
$isSuccess = false;
$download = false;
for ($j=0; $j<2; $j++) {
$download = downloadImageAndSave($imageUrl,$name,$chapter,$page,$lastPage);
if($download === FALSE) {
echo "failed $name $chapter $page\n";
sleep(3);
} else {
echo 'downloaded: ' . $download . "\n";
saveJobDone($name,$chapter,$page,$lastPage);
$isSuccess = true;
break;
}
}
if (!$isSuccess && $page === 1) {
echo "up to date\n";
break;
}
sleep(rand()%5);
}
function getJob($name,$offset=0){
$lastData = json_decode(@file_get_contents(LAST_FILE), true);
$nextChapter = 1;
$nextPage = 1;
$lastChapterPage = 0;
if(isset($lastData[$name])) {
$nextChapter = $lastData[$name]['chapter'];
$nextPage = $lastData[$name]['page'];
$lastChapterPage = $lastData[$name]['lastPage'];
$nextPage += $offset;
if($nextPage > $lastChapterPage) {
$nextChapter += 1;
$nextPage = 1;
}
}
$result['chapter'] = $nextChapter;
$result['page'] = $nextPage;
return $result;
}
function saveJobDone($name,$chapter,$page,$lastPage) {
$lastData = json_decode(@file_get_contents(LAST_FILE), true);
$save = [];
$save['chapter'] = $chapter;
$save['page'] = $page;
$save['lastPage'] = intval($lastPage);
$lastData[$name] = $save;
file_put_contents(LAST_FILE, json_encode($lastData));
}
function grabImageUrl($html) {
$image = subStringBetween($html,'id="img"','alt=');
$image = substr($image, strpos($image, 'http://') );
$image = substr($image, 0,strpos($image, '"') );
return $image;
}
function downloadImageAndSave($url,$name,$chapter,$page,$lastPage) {
if(strlen($page) === 1) {
$page = '0'.$page;
}
$img = str_replace('-', '', $name)."-$chapter-$lastPage-$page.jpg";
$file = @file_get_contents($url);
if($file===FALSE || strlen($file) < 10) {
return FALSE;
} else {
if (!is_dir(MANGA_DIR)) {
mkdir(MANGA_DIR);
}
$filename = MANGA_DIR . '/' . $img;
file_put_contents($filename, @file_get_contents($url));
// Check saved file is correct or not
$checkFile = @file_get_contents($filename);
if(strlen($checkFile) > 0) {
return $img;
} else {
return FALSE;
}
}
}
function subStringBetween($string, $begin, $end) {
if($begin === 0)
return substr( $string, 0, strpos($string,$end) );
if($end === 0)
return substr($string, strpos($string, $begin)+strlen($begin));
$string = ' '.$string;
$ini = strpos($string,$begin);
if ($ini == 0) return '';
$ini += strlen($begin);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment