diff --git a/README.md b/README.md index c8ceb59..05b0984 100644 --- a/README.md +++ b/README.md @@ -37,18 +37,19 @@ 你还可以通过`limit`参数限制图片数量 例如`https://cloud.mokeyjay.com/pixiv/?color=f00&limit=10` 则可以得到背景为红色的Top10画册 -**请注意:** `limit`参数的范围为`1-50` +> `limit`参数的范围必须在`1-50` 之间 + #### API服务 [图床缩略图URL+详情页URL](https://cloud.mokeyjay.com/pixiv/storage/app/pixiv.json)(推荐) [Pixiv原始缩略图URL+详情页URL](https://cloud.mokeyjay.com/pixiv/storage/app/source.json) 内容很简单,相信大家看了就知道可以怎么用了,不再赘述 ---- + ### 方案二:自行架设服务 适用于动手能力较强或需要深度自定义的用户 > 需要PHP版本 >= 5.4 -首先[下载源代码](https://github.com/mokeyjay/Pixiv-daily-top50-widget/archive/master.zip),解压 +首先[下载源代码](https://github.com/mokeyjay/Pixiv-daily-top50-widget/releases/latest),解压 使用专业编辑器(例如`Sublime`、`Notepad++`等,切忌使用记事本)编辑`config.php`,根据实际情况修改相应配置 > 由于Pixiv已经被墙,如果你想要将此项目部署在国内,请务必配置 `proxy` 配置项 @@ -56,10 +57,6 @@ 最后一步,给予`storage`目录读写权限 -> 为了更好的用户体验,你还可以设定一个如下的定时任务来主动触发刷新任务 -> `1 0 * * * php [本项目路径]/index.php -j refresh` -> 如果你这么做了,而且还使用了 `local` 图床,则务必设置 `url` 配置项。因为 cli 模式下无法获得当前url,如果 url 配置项依旧留空,则生成的图片url地址可能会有问题 - ### 注意事项 - 推荐使用方案一,由我本人维护,如有问题第一时间更新 - 方案二反馈问题之前,请先将 `log_level` 设为 `['ERROR', 'DEBUG']` ,并再次重现问题后,带着 `logs` 来反馈 diff --git a/app/App.php b/app/App.php index bf4b1bc..a2c233f 100644 --- a/app/App.php +++ b/app/App.php @@ -6,6 +6,7 @@ use app\Libs\Config; use app\Libs\Lock; use app\Libs\Pixiv; +use app\Libs\Storage; use app\Libs\Tools; class App @@ -34,12 +35,13 @@ public static function run() self::job(); } - // 加载首页或loading页 - $pixivJson = Pixiv::getJson('pixiv'); - if ($pixivJson === false) { + $pixivJson = Storage::getJson('pixiv'); + if ($pixivJson === false || $pixivJson['date'] != date('Y-m-d')) { if (!Lock::check('refresh')) { Pixiv::runRefreshThread(); } + } + if ($pixivJson === false) { include APP_PATH . 'Views/loading.php'; } else { include APP_PATH . 'Views/index.php'; @@ -60,9 +62,10 @@ protected static function job() throw new \Exception("任务 {$jobName} 加载失败"); } set_time_limit(0); - $reuslt = $job->run(); - if ($reuslt) { + $result = $job->run(); + if ($result) { Tools::log("任务 {$jobName} 执行完毕"); + echo "任务 {$jobName} 执行完毕"; } else { throw new \Exception("任务 {$jobName} 执行失败:{$job->getErrorMsg()}"); } diff --git a/app/Jobs/Refresh.php b/app/Jobs/Refresh.php index b877656..116ff60 100644 --- a/app/Jobs/Refresh.php +++ b/app/Jobs/Refresh.php @@ -27,7 +27,7 @@ public function run() try { $sourceJson = Pixiv::getImageList(); if (!$sourceJson) { - throw new \Exception('无法获取Pixiv排行榜图片列表'); + throw new \Exception('【致命错误】无法获取Pixiv排行榜图片列表'); } if (Config::$service) { @@ -37,7 +37,6 @@ public function run() } $pixivJson = [ - 'date' => date('Y-m-d'), 'image' => [], 'url' => [], ]; @@ -45,14 +44,14 @@ public function run() $enableCompress = Config::$compress && function_exists('imagecreatefromjpeg'); $imageHostingInstances = []; - foreach (Config::$image_hosting as $ihName){ + foreach (Config::$image_hosting as $ihName) { $imageHostingInstances[] = ImageHosting::make($ihName); } // 开始获取图片 foreach ($sourceJson['image'] as $i => $imageUrl) { // 缓存数量限制 - if(Config::$service === false && Config::$limit <= $i){ + if (Config::$service === false && Config::$limit <= $i) { break; } // 最多尝试下载3次 @@ -78,9 +77,9 @@ public function run() } } // 上传到图床 - foreach ($imageHostingInstances as $imageHosting){ + foreach ($imageHostingInstances as $imageHosting) { $url = $imageHosting->upload($tmpfile); - if($url != false){ + if ($url != false) { Storage::deleteFile($tmpfile); break; } @@ -89,7 +88,7 @@ public function run() $pixivJson['image'][] = $url ?: $sourceJson['image'][$i]; // 如上传失败则使用原图url(虽然原图url也显示不出来) $pixivJson['url'][] = $sourceJson['url'][$i]; } - Storage::save('pixiv.json', $pixivJson); + Storage::saveJson('pixiv', $pixivJson); Lock::remove('refresh'); Config::$clear_overdue && Storage::clearOverdueImages(); @@ -97,7 +96,9 @@ public function run() } catch (\Exception $e) { Lock::remove('refresh'); - throw new \Exception($e->getMessage()); + Tools::log($e->getMessage(), 'ERROR'); + $this->errorMsg = $e->getMessage(); + return false; } } } \ No newline at end of file diff --git a/app/Libs/Config.php b/app/Libs/Config.php index e9e13c5..32dfdd7 100644 --- a/app/Libs/Config.php +++ b/app/Libs/Config.php @@ -33,7 +33,7 @@ public static function init() } // 获取本项目url - if (self::$url == '') { + if (self::$url == '' && !IS_CLI) { $urlInfo = pathinfo(Tools::getCurrentURL()); self::$url = $urlInfo['dirname'] . '/'; if (!isset($urlInfo['extension'])) { @@ -62,9 +62,15 @@ public static function init() throw new \Exception('limit 配置项不得小于1'); } + if (IS_CLI && self::$url == '' && in_array('local', self::$image_hosting)) { + throw new \Exception('在cli模式下使用local本地图床时,必须配置url项,否则可能会生成错误的缩略图url'); + } + } catch (\Exception $e) { Tools::log($e->getMessage(), 'ERROR'); - echo '错误:' . $e->getMessage() . "\n"; + if (!IS_CLI) { + echo '错误:' . $e->getMessage(); + } die; } } diff --git a/app/Libs/Lock.php b/app/Libs/Lock.php index 6396f58..10515b3 100644 --- a/app/Libs/Lock.php +++ b/app/Libs/Lock.php @@ -16,18 +16,12 @@ class Lock */ public static function check($name) { - $lock = Storage::get(".{$name}lock"); - if (!$lock) { + $lock = Storage::get("app/{$name}.lock"); + if ($lock === false) { return false; } - if (!isset($lock['time'])) { - return false; - } - if ($lock['time'] > time() || $lock['time'] == 0) { - return true; - } - return false; + return ($lock > time() || $lock == 0); } /** @@ -38,8 +32,8 @@ public static function check($name) */ public static function create($name, $expire = 0) { - if (self::check($name) == false) { - return Storage::save(".{$name}lock", ['time' => $expire ? (time() + $expire) : 0]); + if (self::check($name) === false) { + return Storage::save("app/{$name}.lock", ($expire ? (time() + $expire) : 0)); } return false; } @@ -51,6 +45,6 @@ public static function create($name, $expire = 0) */ public static function remove($name) { - return Storage::remove(".{$name}lock"); + return Storage::remove("app/{$name}.lock"); } } \ No newline at end of file diff --git a/app/Libs/Pixiv.php b/app/Libs/Pixiv.php index 88a93b4..89c8719 100644 --- a/app/Libs/Pixiv.php +++ b/app/Libs/Pixiv.php @@ -9,29 +9,14 @@ */ class Pixiv { - /** - * 获取缓存文件。成功返回数组 - * @param string $name - * @return array|false - */ - public static function getJson($name) - { - $json = Storage::get($name . '.json'); - if ($json === false || !isset($json['date'])) { - return false; - } - - return $json['date'] == date('Y-m-d') ? $json : false; - } - /** * 获取Pixiv图片url列表 * @return array|false */ public static function getImageList() { - $imageList = self::getJson('source'); - if ($imageList) { + $imageList = Storage::getJson('source', true); + if (is_array($imageList)) { return $imageList; } @@ -44,11 +29,10 @@ public static function getImageList() return false; $json = [ - 'date' => date('Y-m-d'), 'image' => $image[0], 'url' => $url[0], ]; - Storage::save('source.json', $json); + Storage::saveJson('source', $json); return $json; } diff --git a/app/Libs/Storage.php b/app/Libs/Storage.php index a59d667..9c1d78f 100644 --- a/app/Libs/Storage.php +++ b/app/Libs/Storage.php @@ -10,35 +10,35 @@ class Storage { /** - * 保存文件内容 + * 保存到文件 * @param string $file * @param string|array $content * @return bool */ public static function save($file, $content) { - $file = STORAGE_PATH . 'app/' . $file; - return file_put_contents($file, json_encode($content)) !== false; + $file = STORAGE_PATH . $file; + return file_put_contents($file, $content) !== false; } /** * 获取文件内容 * @param string $file - * @return array|false + * @return mixed|false */ public static function get($file) { - $file = STORAGE_PATH . 'app/' . $file; - if (is_readable($file)) { - $file = file_get_contents($file); - if ($file === false) { - Tools::log('读取 ' . $file . ' 文件失败'); - return false; - } - - return json_decode($file, true); + $file = STORAGE_PATH . $file; + if (is_readable($file) === false) { + return false; } - return false; + $content = @file_get_contents($file); + if ($content === false) { + Tools::log("读取 {$file} 文件失败"); + return false; + } + + return $content; } /** @@ -48,7 +48,7 @@ public static function get($file) */ public static function remove($file) { - return self::deleteFile(STORAGE_PATH . 'app/' . $file); + return self::deleteFile(STORAGE_PATH . $file); } /** @@ -80,9 +80,9 @@ public static function clearOverdueImages() */ public static function getImage($name) { - $path = STORAGE_PATH . 'images/' . $name; - if (file_exists($path) && getimagesize($path)) { - return file_get_contents($path); + $path = 'images/' . $name; + if (file_exists(STORAGE_PATH . $path) && getimagesize(STORAGE_PATH . $path)) { + return self::get($path); } return false; } @@ -96,4 +96,37 @@ public static function deleteFile($path) { return unlink($path); } + + /** + * 保存数组到json文件 + * @param string $file 文件名。无需后缀名 + * @param array $data + * @return bool + */ + public static function saveJson($file, array $data) + { + $data['date'] = date('Y-m-d'); + $data = json_encode($data); + return self::save("app/{$file}.json", $data); + } + + /** + * 获取json数组内容 + * @param string $file 文件名。无需后缀名 + * @param bool $checkDate 检查日期。如果文件已过期则返回false + * @return mixed|false + */ + public static function getJson($file, $checkDate = false) + { + $content = self::get("app/{$file}.json"); + $content = json_decode($content, true); + if (!is_array($content)) { + return false; + } + + if ($checkDate && (!isset($content['date']) || $content['date'] != date('Y-m-d'))) { + return false; + } + return $content; + } } \ No newline at end of file diff --git a/app/Libs/Tools.php b/app/Libs/Tools.php index 404adae..b5080f2 100644 --- a/app/Libs/Tools.php +++ b/app/Libs/Tools.php @@ -19,11 +19,11 @@ public static function log($message, $level = 'DEBUG') { $level = strtoupper($level); if (is_array(Config::$log_level) && in_array($level, Config::$log_level)) { - $file = STORAGE_PATH . 'logs/' . date('Ymd') . '.log'; + $file = STORAGE_PATH . 'logs/' . date('Ymd') . (IS_CLI ? '-cli' : '') . '.log'; $message = is_array($message) ? json_encode($message) : $message; $content = "[{$level}] " . date('Y-m-d H:i:s') . " --> {$message}\n"; if (IS_CLI) { - echo "{$content}"; + echo $content; } return file_put_contents($file, $content, FILE_APPEND) !== false; }