Guest User

Untitled

a guest
Feb 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types = 1);
  4.  
  5. namespace sid;
  6.  
  7. $CONFIG = [
  8. 'base-dir' => __DIR__ . '/downloads/',
  9. 'start-from' => 1,
  10. 'count' => 2000,
  11. 'output-extension' => '', // jpg|gif|png|mp3|mp4|webm etc.
  12. 'logs-file' => 'Logs.txt' // See the log file in %base-dir%. Leave empty to disable file logs
  13. ];
  14.  
  15. // No changes required after this line
  16. $baseDir = rtrim($CONFIG['base-dir'], '\/') . DIRECTORY_SEPARATOR;
  17. $startFrom = $CONFIG['start-from'];
  18. $count = $CONFIG['count'];
  19. $stopAfter = $startFrom + $count - 1;
  20. $ext = !empty($CONFIG['output-extension']) ? '.' . $CONFIG['output-extension'] : '';
  21.  
  22. global $enableLogs, $logFile;
  23.  
  24. $enableLogs = !empty($CONFIG['logs-file']);
  25. $logFile = $baseDir . $CONFIG['logs-file'];
  26.  
  27. // Check base dir
  28. if (!file_exists($baseDir)) {
  29. say('Error. The destination directory does not exist.');
  30. exit(1);
  31. }
  32.  
  33. // Check the log file
  34. if ($enableLogs) {
  35. if (!file_exists($logFile)) {
  36. file_put_contents($logFile, '');
  37.  
  38. if (!file_exists($logFile)) {
  39. say('Error. Failed to create a log file ' . $logFile);
  40. exit(2);
  41. }
  42. }
  43. }
  44.  
  45. // All ready to start
  46. $fails = new FailuresRecorder($startFrom);
  47.  
  48. for ($i = $startFrom, $done = 0; $i <= $stopAfter; $i++, $done++) {
  49. // Notify about the progress
  50. if ($done % 50 == 0) {
  51. say("{$done}/{$count} done...");
  52. }
  53.  
  54. $file = "{$baseDir}{$i}{$ext}";
  55.  
  56. if (file_exists($file)) {
  57. $fails->nextSucceed();
  58. } else {
  59. $fails->nextFailed();
  60. }
  61. }
  62.  
  63. $fails->finish();
  64.  
  65. say('Finished.');
  66. say("Failed to download the following files: {$fails}.");
  67.  
  68. exit(0);
  69.  
  70. function say(string $message)
  71. {
  72. global $enableLogs, $logFile;
  73.  
  74. echo $message, PHP_EOL;
  75.  
  76. if ($enableLogs) {
  77. file_put_contents($logFile, $message . PHP_EOL, FILE_APPEND);
  78. }
  79. }
  80.  
  81. /**
  82. * Record failures like: 2, 4, 10-12, 17-43.
  83. */
  84. class FailuresRecorder
  85. {
  86. const STATUS_FAILED = 0;
  87. const STATUS_SUCCEED = 1;
  88. const STATUS_UNASSIGNED = 2;
  89.  
  90. protected $failures = []; // ["2", "4", "10-12", "17-43"]
  91. protected $failuresCount = 0; // 32
  92.  
  93. protected $firstFailId = 0;
  94. protected $lastFailId = 0;
  95.  
  96. protected $currentId = 1;
  97. protected $currentStatus = self::STATUS_UNASSIGNED;
  98.  
  99. public function __construct(int $startId)
  100. {
  101. $this->currentId = $startId;
  102. }
  103.  
  104. public function nextSucceed()
  105. {
  106. switch ($this->currentStatus) {
  107. case self::STATUS_FAILED:
  108. if ($this->lastFailId == $this->firstFailId) {
  109. $this->failures[] = $this->lastFailId;
  110. } else {
  111. $this->failures[] = $this->firstFailId . '-' . $this->lastFailId;
  112. }
  113. break;
  114.  
  115. case self::STATUS_SUCCEED:
  116. break;
  117. }
  118.  
  119. $this->currentId++;
  120. $this->currentStatus = self::STATUS_SUCCEED;
  121. }
  122.  
  123. public function nextFailed()
  124. {
  125. $this->lastFailId = $this->currentId;
  126.  
  127. switch ($this->currentStatus) {
  128. case self::STATUS_SUCCEED:
  129. case self::STATUS_UNASSIGNED:
  130. $this->firstFailId = $this->currentId;
  131. break;
  132. }
  133.  
  134. $this->failuresCount++;
  135. $this->currentId++;
  136. $this->currentStatus = self::STATUS_FAILED;
  137. }
  138.  
  139. public function finish()
  140. {
  141. $this->nextSucceed();
  142. }
  143.  
  144. public function getFailures(): array
  145. {
  146. return $this->failures;
  147. }
  148.  
  149. public function getFailuresCount(): int
  150. {
  151. return $this->failuresCount;
  152. }
  153.  
  154. public function hasFailures(): bool
  155. {
  156. return !empty($this->failures);
  157. }
  158.  
  159. public function __toString(): string
  160. {
  161. if ($this->hasFailures()) {
  162. return implode(', ', $this->failures) . " ({$this->failuresCount} in total)";
  163. } else {
  164. return 'none';
  165. }
  166. }
  167. } // class FailuresRecorder
Add Comment
Please, Sign In to add comment