Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types = 1);
- namespace sid;
- $CONFIG = [
- 'base-dir' => __DIR__ . '/downloads/',
- 'start-from' => 1,
- 'count' => 2000,
- 'output-extension' => '', // jpg|gif|png|mp3|mp4|webm etc.
- 'logs-file' => 'Logs.txt' // See the log file in %base-dir%. Leave empty to disable file logs
- ];
- // No changes required after this line
- $baseDir = rtrim($CONFIG['base-dir'], '\/') . DIRECTORY_SEPARATOR;
- $startFrom = $CONFIG['start-from'];
- $count = $CONFIG['count'];
- $stopAfter = $startFrom + $count - 1;
- $ext = !empty($CONFIG['output-extension']) ? '.' . $CONFIG['output-extension'] : '';
- global $enableLogs, $logFile;
- $enableLogs = !empty($CONFIG['logs-file']);
- $logFile = $baseDir . $CONFIG['logs-file'];
- // Check base dir
- if (!file_exists($baseDir)) {
- say('Error. The destination directory does not exist.');
- exit(1);
- }
- // Check the log file
- if ($enableLogs) {
- if (!file_exists($logFile)) {
- file_put_contents($logFile, '');
- if (!file_exists($logFile)) {
- say('Error. Failed to create a log file ' . $logFile);
- exit(2);
- }
- }
- }
- // All ready to start
- $fails = new FailuresRecorder($startFrom);
- for ($i = $startFrom, $done = 0; $i <= $stopAfter; $i++, $done++) {
- // Notify about the progress
- if ($done % 50 == 0) {
- say("{$done}/{$count} done...");
- }
- $file = "{$baseDir}{$i}{$ext}";
- if (file_exists($file)) {
- $fails->nextSucceed();
- } else {
- $fails->nextFailed();
- }
- }
- $fails->finish();
- say('Finished.');
- say("Failed to download the following files: {$fails}.");
- exit(0);
- function say(string $message)
- {
- global $enableLogs, $logFile;
- echo $message, PHP_EOL;
- if ($enableLogs) {
- file_put_contents($logFile, $message . PHP_EOL, FILE_APPEND);
- }
- }
- /**
- * Record failures like: 2, 4, 10-12, 17-43.
- */
- class FailuresRecorder
- {
- const STATUS_FAILED = 0;
- const STATUS_SUCCEED = 1;
- const STATUS_UNASSIGNED = 2;
- protected $failures = []; // ["2", "4", "10-12", "17-43"]
- protected $failuresCount = 0; // 32
- protected $firstFailId = 0;
- protected $lastFailId = 0;
- protected $currentId = 1;
- protected $currentStatus = self::STATUS_UNASSIGNED;
- public function __construct(int $startId)
- {
- $this->currentId = $startId;
- }
- public function nextSucceed()
- {
- switch ($this->currentStatus) {
- case self::STATUS_FAILED:
- if ($this->lastFailId == $this->firstFailId) {
- $this->failures[] = $this->lastFailId;
- } else {
- $this->failures[] = $this->firstFailId . '-' . $this->lastFailId;
- }
- break;
- case self::STATUS_SUCCEED:
- break;
- }
- $this->currentId++;
- $this->currentStatus = self::STATUS_SUCCEED;
- }
- public function nextFailed()
- {
- $this->lastFailId = $this->currentId;
- switch ($this->currentStatus) {
- case self::STATUS_SUCCEED:
- case self::STATUS_UNASSIGNED:
- $this->firstFailId = $this->currentId;
- break;
- }
- $this->failuresCount++;
- $this->currentId++;
- $this->currentStatus = self::STATUS_FAILED;
- }
- public function finish()
- {
- $this->nextSucceed();
- }
- public function getFailures(): array
- {
- return $this->failures;
- }
- public function getFailuresCount(): int
- {
- return $this->failuresCount;
- }
- public function hasFailures(): bool
- {
- return !empty($this->failures);
- }
- public function __toString(): string
- {
- if ($this->hasFailures()) {
- return implode(', ', $this->failures) . " ({$this->failuresCount} in total)";
- } else {
- return 'none';
- }
- }
- } // class FailuresRecorder
Add Comment
Please, Sign In to add comment