Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Support logging in
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklul committed Mar 20, 2020
1 parent 335dff4 commit 538532f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
6 changes: 5 additions & 1 deletion build.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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';
Expand Down
86 changes: 66 additions & 20 deletions src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class App
*
* @var int
*/
private $VERSION = '1.2.0';
private $VERSION = '1.3.0';

/**
* App update URL
Expand Down Expand Up @@ -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 = '')
Expand All @@ -130,6 +145,7 @@ public function __construct($arg = '')
}
}

/** @noinspection TypeUnsafeComparisonInspection */
if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
$this->IS_LINUX = true;
}
Expand All @@ -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)
Expand Down Expand Up @@ -205,6 +233,7 @@ private function cURLProgress($resource = null, $download_size = 0, $downloaded
* Parse user input
*
* @param $string
*
* @return mixed
*/
private function parseInput($string)
Expand All @@ -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']);
Expand All @@ -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;
}
}
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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)
Expand All @@ -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
*/
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand All @@ -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");
}
Expand Down Expand Up @@ -477,6 +522,7 @@ public function run()
}

if (!is_dir($downloadDir . '/deleted/')) {
/** @noinspection MkdirRaceConditionInspection */
mkdir($downloadDir . '/deleted/');
}

Expand Down
5 changes: 5 additions & 0 deletions src/config.cfg.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; Your e621 login
LOGIN=

; API key obtained in "e621 -> Account -> Manage API Access"
API_KEY=

0 comments on commit 538532f

Please sign in to comment.