Skip to content

Instantly share code, notes, and snippets.

@ob-ivan
Last active February 29, 2016 20:49
Show Gist options
  • Save ob-ivan/73dad18910d4a8762f4f to your computer and use it in GitHub Desktop.
Save ob-ivan/73dad18910d4a8762f4f to your computer and use it in GitHub Desktop.
ElfQuest comics downloader
<?php
function __autoload($className)
{
$fileName = dirname(__FILE__) . '/' . $className . '.php';
if (file_exists($fileName)) {
require_once $fileName;
}
}
$questId = $argv[1];
$startChapter = $argv[2];
$factory = new QuestDataFactory();
if (! $factory->isKnown($questId)) {
print "Unknown quest id '$questId'\n";
exit;
}
$questData = $factory->produce($questId);
$downloader = new ElfQuestDownloader(
$questData,
dirname(__FILE__) . '/data',
1000000
);
if ($startChapter) {
$downloader->downloadFromChapter($startChapter);
} else {
$downloader->downloadAll();
}
<?php
class ElfQuestDownloader
{
const FILE_TEMPLATE = '$quest-chapter$chapter-page$page.jpg';
private $questData;
private $baseDir;
private $sleepMicroseconds;
public function __construct(QuestData $questData, $baseDir, $sleepMicroseconds)
{
$this->questData = $questData;
$this->baseDir = $baseDir;
$this->sleepMicroseconds = $sleepMicroseconds;
}
public function downloadAll()
{
print "Begin\n";
foreach ($this->questData->getChapterList() as $chapter) {
$this->downloadChapter($chapter);
}
print "Done\n";
}
public function downloadFromChapter($startChapter)
{
print "Skipping to chapter $startChapter\n";
$found = false;
foreach ($this->questData->getChapterList() as $chapter) {
if (! $found && $chapter == $startChapter) {
$found = true;
print "Begin\n";
}
if ($found) {
$this->downloadChapter($chapter);
}
}
print "Done\n";
}
public function downloadChapter($chapter)
{
print "Starting chapter $chapter\n";
for ($page = 0; $page < 200; ++$page) {
$this->downloadPage($chapter, $page);
}
print "Finished chapter $chapter with $page pages\n";
}
private function downloadPage($chapter, $page)
{
$url = $this->makeUrl($chapter, $page);
print "Downloading from url $url\n";
$contents = @file_get_contents($url);
if (false === $contents) {
print "Chapter $chapter does not have $page\n";
break;
}
print "Finished downloading from url $url\n";
$filename = $this->makeFilename($chapter, $page);
$dirname = dirname($filename);
if (! file_exists($dirname)) {
mkdir($dirname, 0777, true);
}
print "Saving file $filename\n";
$success = file_put_contents($filename, $contents);
if (false === $success) {
print "Error while saving file $filename\n";
break;
}
print "Saved file $filename\n";
}
private function makeUrl($chapter, $page)
{
return $this->applyTemplate(
$this->questData->getUrlTemplate(),
$this->pad($chapter),
$page
);
}
private function makeFilename($chapter, $page)
{
return implode('/', [
$this->baseDir,
$this->questData->getQuestId(),
$this->applyTemplate(
self::FILE_TEMPLATE,
$this->pad($chapter),
$this->pad($page)
),
]);
}
private function applyTemplate($template, $chapter, $page)
{
return str_replace(
array(
'$quest',
'$chapter',
'$page',
),
array(
$this->questData->getQuestId(),
$chapter,
$page,
),
$template
);
}
private function pad($number)
{
if (is_int($number))
return sprintf('%02d', $number);
return sprintf('%04.1f', $number);
}
}
<?php
class QuestData
{
/** @var string */
private $questId;
/** @var string */
private $urlTemplate;
/** @var float[] */
private $chapterList;
public function __construct($questId, $urlTemplate, array $chapterList)
{
$this->questId = $questId;
$this->urlTemplate = $urlTemplate;
$this->chapterList = $chapterList;
}
public function getQuestId()
{
return $this->questId;
}
public function getUrlTemplate()
{
return $this->urlTemplate;
}
public function getChapterList()
{
return $this->chapterList;
}
}
<?php
class QuestDataFactory
{
private $urlTemplates = [
'OQ' => 'http://elfquest.com/read/OQ/OQ$chapter/oq$chapter-$page.jpg',
'SABM' => 'http://elfquest.com/read/SABM/SABM$chapter/sabm$chapter-$page.jpg',
'KOBW' => 'http://elfquest.com/read/KOBW/KOBW$chapter/kobw$chapter-$page.jpg',
'HY' => 'http://elfquest.com/read/HY/HY$chapter/hy$chapter-$page.jpg',
'HYC' => 'http://elfquest.com/read/HYC/hyc-$page.jpg',
'SH' => 'http://elfquest.com/read/SH/SH$chapter/sh$chapter-$page.jpg',
'SHC' => 'http://elfquest.com/read/SHC/shc-$page.jpg',
];
private $chapterLists;
public function __construct()
{
$this->chapterLists = [
'OQ' => range(1, 21),
'SABM' => range(1, 8),
'KOBW' => range(1, 9),
'HY' => array_merge([9.5], range(10, 29)),
'HYC' => [1],
'SH' => range(1, 16),
'SHC' => [1],
];
}
public function isKnown($questId)
{
return isset($this->urlTemplates[$questId]) && isset($this->chapterLists[$questId]);
}
public function produce($questId)
{
return new QuestData(
$questId,
$this->urlTemplates[$questId],
$this->chapterLists[$questId]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment