Last active
December 28, 2020 10:47
-
-
Save inonote/50b834fbd8eb7eba1bcf7461564f28f2 to your computer and use it in GitHub Desktop.
HTTP(S)で公開された連番ファイルを一括ダウンロードするスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /**************************************************************************** | |
| cntdl v0.1 | |
| (c) 2020 inonote | |
| HTTP(S) で公開された連番ファイルを一括ダウンロードするスクリプト | |
| ## 推奨動作環境 | |
| * PHP 7.2.8 以上 | |
| * curl 7.55.1 以上 | |
| ## 使い方 | |
| 1. このスクリプト内の`$config`を弄る。 | |
| 2. 端末を開いて、`php cntdl.php`を実行。 | |
| 3. あとは放置するだけ。 | |
| ※エラーが発生しても無視されるので、エラーページがダウンロードされているのを | |
| 発見した場合はそのファイルのMD5を調べて`$config['error_page_md5']`に格納し、 | |
| `php cntdl.php -r` を何回か実行する必要がある。 | |
| このソフトウェアは MIT License のもとで公開されています。 | |
| ***************************************************************************** | |
| Copyright 2020 inonote | |
| Permission is hereby granted, free of charge, to any person obtaining a | |
| copy of this software and associated documentation files (the "Software"), | |
| to deal in the Software without restriction, including without limitation | |
| the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
| and/or sell copies of the Software, and to permit persons to whom the | |
| Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included | |
| in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
| DEALINGS IN THE SOFTWARE. | |
| ****************************************************************************/ | |
| $options = [ | |
| 'retry' => false | |
| ]; | |
| // 引数解析 | |
| for($i = 1; $i < $argc; $i++) { | |
| $prefix = substr($argv[$i], 0, 2); | |
| if ($prefix === '--') { // long | |
| $prm = substr($argv[$i], 2); | |
| if ($prm === 'retry') $options['retry'] = true; | |
| } | |
| else if ($prefix[0] === '-') { // short | |
| $len = strlen($argv[$i]); | |
| for($j = 1; $j < $len; $j++) { | |
| $p = $argv[$i][$j]; | |
| if ($p === 'r') $options['retry'] = true; | |
| } | |
| } | |
| } | |
| $config = [ | |
| // 連番 開始 | |
| 'begin' => 1, | |
| // 連番 終了 | |
| 'end' => 100, | |
| // 保存するファイルの名前 <index>は数字に置き換えられる | |
| 'file_name' => '<index>.txt', | |
| // エラーページのmd5 | |
| 'error_page_md5' => '', | |
| // 待機秒数 | |
| 'wait' => 1, | |
| // User Agent | |
| 'ua' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36', | |
| // Referer | |
| 'rf' => '', | |
| // Cookie | |
| 'ck' => '', | |
| // URL <index>は数字に置き換えられる | |
| 'url' => '' | |
| ]; | |
| $download_list = []; | |
| if ($options['retry']) { | |
| for($i = $config['begin']; $i <= $config['end']; $i++) { | |
| $fpath = './'.str_replace('<index>', $i, $config['file_name']); | |
| if (file_exists($fpath) && md5_file($fpath) === $config['error_page_md5']) { | |
| array_push($download_list, $i); | |
| } | |
| } | |
| } | |
| else | |
| $download_list = range($config['begin'], $config['end']); | |
| foreach($download_list as $item) { | |
| echo 'dl: '.$item.PHP_EOL; | |
| system(sprintf('curl -o "%s" -e "%s" -b "%s" -A "%s" "%s"', | |
| str_replace('<index>', $item, $config['file_name']), | |
| $config['rf'], | |
| $config['ck'], | |
| $config['ua'], | |
| str_replace('<index>', $item, $config['url']) | |
| )); | |
| sleep($config['wait']); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment