From 538532f3189520f49977e05f7e1d0e71a86e29e1 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Fri, 20 Mar 2020 16:21:48 +0100 Subject: [PATCH] Support logging in --- README.md | 5 +++ build.php | 6 ++- src/app.php | 86 ++++++++++++++++++++++++++++++++---------- src/config.cfg.example | 5 +++ 4 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 src/config.cfg.example diff --git a/README.md b/README.md index 2f0d9aa..a83d7f0 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,11 @@ _Current working directory = directory the script was started from, usually it w _Moving directory over a launch script works well too!_ +### Downloading blocked content - logging in + +- Rename `config.cfg.example` to `config.cfg` +- Fill your login details inside it + ## License See [LICENSE](https://github.com/jacklul/e621-Pool-Downloader/blob/master/LICENSE). diff --git a/build.php b/build.php index d29afa5..81a9d1e 100644 --- a/build.php +++ b/build.php @@ -9,7 +9,7 @@ $buildRoot = realpath("build"); $buildFile = 'build/e621_Pool_Downloader.zip'; -$ignoredFiles = ['.', '..', '.gitkeep', 'e621_Pool_Downloader.zip', '.updatecheck']; +$ignoredFiles = ['.', '..', '.gitkeep', 'e621_Pool_Downloader.zip', '.updatecheck', 'config.cfg']; if (ini_get("phar.readonly") == 0) { echo "Building...\n"; @@ -34,6 +34,10 @@ copy($srcRoot . "/run.sh", $buildRoot . "/run.sh"); } + if (file_exists($srcRoot . "/config.cfg.example")) { + copy($srcRoot . "/config.cfg.example", $buildRoot . "/config.cfg.example"); + } + if (file_exists(__DIR__ . '/vendor/erusev/parsedown/Parsedown.php')) { echo " Converting markdown files into HTML...\n"; require __DIR__ . '/vendor/erusev/parsedown/Parsedown.php'; diff --git a/src/app.php b/src/app.php index 7515774..2821d12 100644 --- a/src/app.php +++ b/src/app.php @@ -32,7 +32,7 @@ class App * * @var int */ - private $VERSION = '1.2.0'; + private $VERSION = '1.3.0'; /** * App update URL @@ -104,10 +104,25 @@ class App */ private $POOL_IMAGES = 0; + /** + * Login for authorization + * + * @var string + */ + private $LOGIN = ''; + + /** + * API key for authorization + * + * @var string + */ + private $API_KEY = ''; + /** * Class constructor * - * @param string $arg + * @param string $arg + * * @throws Exception */ public function __construct($arg = '') @@ -130,6 +145,7 @@ public function __construct($arg = '') } } + /** @noinspection TypeUnsafeComparisonInspection */ if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') { $this->IS_LINUX = true; } @@ -140,6 +156,18 @@ public function __construct($arg = '') $this->START_TIME = microtime(true); $this->last_api_request = 0; + + if (file_exists(ROOT . '/config.cfg')) { + $config = parse_ini_file(ROOT . '/config.cfg'); + + if (isset($config['LOGIN'])) { + $this->LOGIN = $config['LOGIN']; + } + + if (isset($config['API_KEY'])) { + $this->API_KEY = $config['API_KEY']; + } + } } private function setPoolIDFromFile($path) @@ -205,6 +233,7 @@ private function cURLProgress($resource = null, $download_size = 0, $downloaded * Parse user input * * @param $string + * * @return mixed */ private function parseInput($string) @@ -215,24 +244,32 @@ private function parseInput($string) return $string; } + /** * Perform simple cURL download request * - * @param $url - * @param bool $progress + * @param string $url + * @param bool $progress + * @param bool $e621auth + * * @return mixed */ - private function cURL($url, $progress = true) + private function cURL($url, $progress = true, $e621auth = false) { if ($this->USE_CURL) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $this->USER_AGENT); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + /** @noinspection CurlSslServerSpoofingInspection */ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 60); + if ($e621auth && !empty($this->LOGIN) && !empty($this->API_KEY)) { + curl_setopt($ch, CURLOPT_USERPWD, $this->LOGIN . ":" . $this->API_KEY); + } + if ($progress) { curl_setopt($ch, CURLOPT_NOPROGRESS, false); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, [$this, 'cURLProgress']); @@ -242,6 +279,7 @@ private function cURL($url, $progress = true) } else { print(str_repeat(' ', 10) . "\r" . $this->LINE_BUFFER); $result = file_get_contents($url, false, stream_context_create(['http' => ['user_agent' => $this->USER_AGENT]])); + return $result; } } @@ -258,10 +296,10 @@ private function getPool() $result = json_decode($result, true); - if (count($result) === 1 && is_array($result[0])) { - $result = $result[0]; - } - + if (count($result) === 1 && is_array($result[0])) { + $result = $result[0]; + } + if (is_array($result)) { if (!empty($result) && !empty($result['post_ids'])) { if (!empty($result['name'])) { @@ -289,7 +327,8 @@ private function getPool() /** * Get needed post data from e621 API * - * @param int $post_id + * @param int $post_id + * * @return mixed */ private function getPost($post_id) @@ -298,20 +337,23 @@ private function getPost($post_id) sleep(1); } - $result = $this->cURL('https://e621.net/posts.json?tags=id:' . $post_id, false); + $result = $this->cURL('https://e621.net/posts.json?tags=id:' . $post_id, false, true); $this->last_api_request = time(); $result = json_decode($result, true); if (is_array($result) && is_array($result['posts']) && count($result['posts']) === 1) { - return isset($result['posts'][0]['file']) ? $result['posts'][0] : null; + return isset($result['posts'][0]['file']) ? $result['posts'][0] : null; } elseif (is_array($result) && is_array($result['posts']) && count($result['posts']) === 0) { return null; } else { print("\rEmpty or invalid result from the API!\n"); + + print_r($result); + exit; } } - + /** * Main function */ @@ -333,7 +375,9 @@ public function run() $ch = curl_init($this->UPDATE_URL); curl_setopt($ch, CURLOPT_USERAGENT, $this->USER_AGENT); curl_setopt($ch, CURLOPT_TIMEOUT, 10); + /** @noinspection CurlSslServerSpoofingInspection */ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + /** @noinspection CurlSslServerSpoofingInspection */ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); @@ -395,6 +439,7 @@ public function run() $downloadDir = $this->WORK_DIR . '/' . $this->POOL_NAME; if (!is_dir($this->WORK_DIR . '/' . $this->POOL_NAME)) { + /** @noinspection MkdirRaceConditionInspection */ mkdir($this->WORK_DIR . '/' . $this->POOL_NAME); } @@ -415,16 +460,16 @@ public function run() foreach ($posts as &$post) { $fileCount++; $this->LINE_BUFFER = 'Fetching image #' . $fileCount . '...'; - print($this->LINE_BUFFER); + print($this->LINE_BUFFER); - $post = $this->getPost($post['id']); - if ($post === null) { + $post = $this->getPost($post['id']); + if ($post === null) { print(" post does not exist!\n"); - continue; + continue; } - if (empty($post['file']['url'])) { - print(" missing image url!\n"); + if (empty($post['file']['url'])) { + print(" missing image url - authentication might be required!\n"); continue; } @@ -440,7 +485,7 @@ public function run() $file = fopen($downloadDir . '/' . $fileName, 'wb'); fwrite($file, $contents); fclose($file); - print("\r" . $this->LINE_BUFFER . " done\n"); + print("\r" . $this->LINE_BUFFER . " done\n"); } else { print("\r" . $this->LINE_BUFFER . " fail\n"); } @@ -477,6 +522,7 @@ public function run() } if (!is_dir($downloadDir . '/deleted/')) { + /** @noinspection MkdirRaceConditionInspection */ mkdir($downloadDir . '/deleted/'); } diff --git a/src/config.cfg.example b/src/config.cfg.example new file mode 100644 index 0000000..bdc5546 --- /dev/null +++ b/src/config.cfg.example @@ -0,0 +1,5 @@ +; Your e621 login +LOGIN= + +; API key obtained in "e621 -> Account -> Manage API Access" +API_KEY=