Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyjay committed Apr 19, 2019
2 parents 00154d6 + d9ded82 commit bb82881
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 73 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,26 @@
你还可以通过`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` 配置项
> 每个配置项的说明都以注释的形式标注在文件内。如果你看不懂,那就说明你比较适合**方案一**
最后一步,给予`storage`目录读写权限

> 为了更好的用户体验,你还可以设定一个如下的定时任务来主动触发刷新任务
> `1 0 * * * php [本项目路径]/index.php -j refresh`
> 如果你这么做了,而且还使用了 `local` 图床,则务必设置 `url` 配置项。因为 cli 模式下无法获得当前url,如果 url 配置项依旧留空,则生成的图片url地址可能会有问题
### 注意事项
- 推荐使用方案一,由我本人维护,如有问题第一时间更新
- 方案二反馈问题之前,请先将 `log_level` 设为 `['ERROR', 'DEBUG']` ,并再次重现问题后,带着 `logs` 来反馈
Expand Down
13 changes: 8 additions & 5 deletions app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand All @@ -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()}");
}
Expand Down
17 changes: 9 additions & 8 deletions app/Jobs/Refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function run()
try {
$sourceJson = Pixiv::getImageList();
if (!$sourceJson) {
throw new \Exception('无法获取Pixiv排行榜图片列表');
throw new \Exception('【致命错误】无法获取Pixiv排行榜图片列表');
}

if (Config::$service) {
Expand All @@ -37,22 +37,21 @@ public function run()
}

$pixivJson = [
'date' => date('Y-m-d'),
'image' => [],
'url' => [],
];

$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次
Expand All @@ -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;
}
Expand All @@ -89,15 +88,17 @@ 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();
return true;

} catch (\Exception $e) {
Lock::remove('refresh');
throw new \Exception($e->getMessage());
Tools::log($e->getMessage(), 'ERROR');
$this->errorMsg = $e->getMessage();
return false;
}
}
}
10 changes: 8 additions & 2 deletions app/Libs/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
18 changes: 6 additions & 12 deletions app/Libs/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand All @@ -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");
}
}
22 changes: 3 additions & 19 deletions app/Libs/Pixiv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
69 changes: 51 additions & 18 deletions app/Libs/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions app/Libs/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit bb82881

Please sign in to comment.