Skip to content

Commit

Permalink
添加 阿里巴巴、百度、今日头条 图床接口;删除已被封锁的 京东 图床接口;优化更新日期判断机制,给 pixiv 排行榜的奇葩更新机制擦屁股
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyjay committed Feb 6, 2020
1 parent 4580ac3 commit d71d69d
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 28 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Pixiv每日排行榜Top50小部件
![Pixiv每日排行榜Top50小部件效果图](https://i.loli.net/2019/05/04/5ccd5293141b4.jpg)
## Pixiv每日排行榜小挂件
![Pixiv每日排行榜小挂件效果图](https://i.loli.net/2019/05/04/5ccd5293141b4.jpg)
## 简介
骚年,你是`ACG`或绘画爱好者吗?你希望在你的博客或网站中添加一个 **`Pixiv`每日排行榜Top50** 的展示功能吗?现在,无需在茫茫互联网中寻找适合自己站点的插件了,只需要几个文件或是一行代码即可实现!
骚年,你是`ACG`或绘画爱好者吗?你希望在你的博客或网站中添加一个 **`Pixiv`每日排行榜** 的展示功能吗?现在,无需在茫茫互联网中寻找适合自己站点的插件了,只需要几个文件或是一行代码即可实现!

## 特色
- 一行`HTML`代码即可调用,方便快捷
Expand All @@ -10,10 +10,10 @@
- 点击图片可跳转到对应作品详情页
- 每日自动更新,无需人工干预
- 内置多图床支持、按需加载图片,极低资源消耗
- 提供API服务,含有更新日期、缩略图url及详情页url
- 提供API服务,含有排行榜更新日期、缩略图url及详情页url

## 开源地址
[Github](https://github.com/mokeyjay/Pixiv-daily-top50-widget)
[Github](https://github.com/mokeyjay/Pixiv-daily-ranking-widget)

## 如何部署
### 方案一:使用[超能小紫](https://www.mokeyjay.com)提供的服务
Expand All @@ -39,7 +39,7 @@
你还可以通过`limit`参数限制图片数量
例如`https://cloud.mokeyjay.com/pixiv/?color=f00&limit=10`
则可以得到背景为红色的Top10画册
> `limit`参数的范围必须在`1-50` 之间
> `limit`参数的范围必须在`1-500` 之间
#### API服务
[图床缩略图URL+详情页URL](https://cloud.mokeyjay.com/pixiv/storage/app/pixiv.json)(推荐)
Expand All @@ -51,7 +51,7 @@
适用于动手能力较强或需要深度自定义的用户
> 需要PHP版本 >= 5.4
首先[下载源代码](https://github.com/mokeyjay/Pixiv-daily-top50-widget/releases/latest),解压
首先[下载源代码](https://github.com/mokeyjay/Pixiv-daily-ranking-widget/releases/latest),解压
使用专业编辑器(例如`Sublime``Notepad++`等,切忌使用记事本)编辑`config.php`,根据实际情况修改相应配置
> 由于Pixiv已经被墙,如果你想要将此项目部署在国内,请务必配置 `proxy` 配置项
Expand All @@ -65,6 +65,18 @@
- 本项目免费开源,仅供学习交流。请勿用于任何商业用途,作者不承担任何责任

## 更新日志
### 4.4
- 改用官方 ajax 接口获取排行数据
- 添加 阿里巴巴、百度、今日头条 图床接口
- 更新 smms 图床接口到 v2 版本
- 删除已被废弃的 img.sb 图床接口
- 删除已被封锁的 京东 图床接口
- 删除 loading 页面的统计代码
- 改用综合排行榜数据,而非仅限于插画
- 图片数量限制扩充到 500,达到官方上限
- service 配置项的取值不再影响 limit
- 其他优化、bug 修复

### 4.3
- 修复Pixiv排行榜页面代码改版导致的无法更新
- 跳转至详情页时使用 https
Expand Down
2 changes: 1 addition & 1 deletion app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function run()
}

$pixivJson = Storage::getJson('pixiv');
if ($pixivJson === false || $pixivJson['date'] != date('Y-m-d')) {
if ($pixivJson === false || !Pixiv::checkDate($pixivJson)) {
if (!Lock::check('refresh')) {
Tools::runRefreshThread();
}
Expand Down
39 changes: 39 additions & 0 deletions app/ImageHosting/Alibaba.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace app\ImageHosting;

use app\Libs\Curl;
use app\Libs\Tools;

/**
* 阿里巴巴图床
* 鸣谢:[@metowolf](https://github.com/metowolf)
* Class Alibaba
* @package app\ImageHosting
* @url https://www.alibaba.com/
*/
class Alibaba extends ImageHosting
{
public function upload($path)
{
$data = [
'file' => Curl::getCurlFile($path),
'scene' => 'aeMessageCenterV2ImageRule',
'name' => 'image.jpg'
];
$result = Curl::post('https://kfupload.alibaba.com/mupload', $data, [
CURLOPT_USERAGENT => 'iAliexpress/6.22.1 (iPhone; iOS 12.1.2; Scale/2.00)',
]);

Tools::log('[阿里巴巴图床]上传:' . json_encode($data));
Tools::log('[阿里巴巴图床]返回:' . $result);

$json = json_decode($result, true);
if(isset($json['code']) && $json['code'] == 0 && isset($json['url'])){
return $json['url'];
}

Tools::log('[阿里巴巴图床]上传失败', 'ERROR');
return false;
}
}
45 changes: 45 additions & 0 deletions app/ImageHosting/Baidu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace app\ImageHosting;

use app\Libs\Curl;
use app\Libs\Tools;

/**
* 百度图床
* 鸣谢:[@metowolf](https://github.com/metowolf)
* Class Baidu
* @package app\ImageHosting
* @url https://baijiahao.baidu.com/
*/
class Baidu extends ImageHosting
{
public function upload($path)
{
$data = [
'media' => Curl::getCurlFile($path),
'no_compress' => '1',
'id' => 'WU_FILE_0',
'is_avatar' => '0',
'type' => 'image',
'name' => pathinfo($path, PATHINFO_FILENAME) . '.jpg'
];
$result = Curl::post('https://baijiahao.baidu.com/builderinner/api/content/file/upload', $data, [
CURLOPT_HTTPHEADER => [
'Origin: https://baijiahao.baidu.com',
'Referer: https://baijiahao.baidu.com/builder/app/register?type=individual'
],
]);

Tools::log('[百度图床]上传:' . json_encode($data));
Tools::log('[百度图床]返回:' . $result);

$json = json_decode($result, true);
if(isset($json['errno']) && $json['errno'] == 0 && isset($json['ret']['org_url'])){
return $json['ret']['org_url'];
}

Tools::log('[百度图床]上传失败', 'ERROR');
return false;
}
}
40 changes: 40 additions & 0 deletions app/ImageHosting/Toutiao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace app\ImageHosting;

use app\Libs\Curl;
use app\Libs\Tools;

/**
* 今日头条图床
* 鸣谢:[@metowolf](https://github.com/metowolf)
* Class Toutiao
* @package app\ImageHosting
* @url https://mp.toutiao.com/
*/
class Toutiao extends ImageHosting
{
public function upload($path)
{
$data = [
'photo' => Curl::getCurlFile($path),
];
$result = Curl::post('https://mp.toutiao.com/upload_photo/?type=json', $data, [
CURLOPT_HTTPHEADER => [
'Origin: https://mp.toutiao.com',
'Referer: https://mp.toutiao.com/profile_register_v3/register/register-normal/2'
],
]);

Tools::log('[今日头条图床]上传:' . json_encode($data));
Tools::log('[今日头条图床]返回:' . $result);

$json = json_decode($result, true);
if(isset($json['message']) && $json['message'] == 'success' && isset($json['web_url'])){
return $json['web_url'];
}

Tools::log('[今日头条图床]上传失败', 'ERROR');
return false;
}
}
12 changes: 9 additions & 3 deletions app/Jobs/Refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

/**
* 刷新任务
*
* 以下 3 种情况需要刷新排行榜
* 1今日的排行榜已经出来了
* 2今日的排行榜还没出但没有 pixiv.json 文件这可能是第一次新安装当然需要刷新
* 3今日的排行榜还没出 pixiv.json 已经过期 2 天及以上
*
* Class Refresh
* @package app\Jobs
*/
Expand All @@ -24,9 +30,9 @@ public function run()
}

try {
// 在线上排行榜没更新且已有 pixiv 旧缓存的情况下不更新 pixiv 缓存
// 这样判断是为了防止新安装用户在当天排行榜未更新之前一直无法正常使用的问题
if(Pixiv::checkRankingUpdate() === false && Storage::getJson('pixiv')){
$pixivJson = Storage::getJson('pixiv');

if(!Pixiv::checkRankingUpdate() && $pixivJson && Pixiv::checkDate($pixivJson)){
Tools::log('排行榜尚未更新,半小时后再试');
Lock::forceCreate('refresh', 1800);
return true;
Expand Down
26 changes: 24 additions & 2 deletions app/Libs/Pixiv.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public static function checkRankingUpdate()
$json = self::getRanking();

if ($json && isset($json['date']) && preg_match('|^\d{8}$|', $json['date'])) {

// 一般情况下 rank_total 都是 500但不排除 pixiv 抽风某天排行榜最大数量不足 500
// 因此在这里处理下 limit 值防止 getImages 在某些情况下报错
if(isset($json['rank_total'])){
Config::$limit = $json['rank_total'] < Config::$limit ? $json['rank_total'] : Config::$limit;
}

return date('Y-m-d', strtotime($json['date'])) === date('Y-m-d');
}

Expand All @@ -54,8 +61,8 @@ public static function checkRankingUpdate()
*/
public static function getImages()
{
$source = Storage::getJson('source', true);
if (is_array($source)) {
$source = Storage::getJson('source');
if (is_array($source) && self::checkDate($source)) {
return $source;
}

Expand Down Expand Up @@ -116,4 +123,19 @@ public static function downloadImage($url)
}
return false;
}

/**
* 检查传入数组的 date 值是否有效返回 true 为未过期
* @param array $data
* @return bool
*/
public static function checkDate(array $data)
{
if(isset($data['date'])){
$yesterday = date('Y-m-d', strtotime('-1 day'));
return $data['date'] >= $yesterday;
}

return false;
}
}
11 changes: 2 additions & 9 deletions app/Libs/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,22 @@ public static function deleteFile($path)
*/
public static function saveJson($file, array $data)
{
if (!isset($data['date'])) {
$data['date'] = date('Y-m-d');
}
return self::save("app/{$file}.json", json_encode($data));
}

/**
* 获取json数组内容
* @param string $file 文件名无需后缀名
* @param bool $checkDate 检查日期如果文件已过期则返回false
* @param string $file 文件名无需后缀名
* @return mixed|false
*/
public static function getJson($file, $checkDate = false)
public static function getJson($file)
{
$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;
}
}
7 changes: 3 additions & 4 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@

/**
* 图床名称
* 可多选jdneteasesmmssuninglocaltietuku
* jd=京东netease=网易suning=苏宁smms=Sm.ms图床local=服务器本地tietuku=贴图库
* 可多选alibababaidutoutiaoneteasesuningsmmslocaltietuku
* alibaba=阿里巴巴baidu=百度toutiao=今日头条netease=网易suning=苏宁smms=Sm.ms图床local=服务器本地tietuku=贴图库
*推荐度按照顺序从高到低
*
* 推荐填写多个图床如果其中一个图床上传失败则将按照顺序继续尝试其他图床
*因jd图床对图片尺寸有最小限制部分较小的缩略图会上传失败因此强烈不建议单独使用jd图床
*/
'image_hosting' => ['jd', 'netease', 'suning', 'smms', 'local'],
'image_hosting' => ['alibaba', 'baidu', 'toutiao', 'netease', 'suning', 'smms', 'local'],

/**
* 图床扩展配置信息
Expand Down
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* 项目Pixiv每日排行榜Top50小部件
* 版本4.3
* 版本4.4
* 作者超能小紫(mokeyjay)
* 博客https://www.mokeyjay.com
* 源码https://github.com/mokeyjay/Pixiv-daily-top50-widget
* 源码https://github.com/mokeyjay/Pixiv-daily-ranking-widget
* 可随意修改二次发布但请保留上方版权声明及注明出处
*/

Expand Down

0 comments on commit d71d69d

Please sign in to comment.