Skip to content

Commit

Permalink
格式化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyjay committed Apr 16, 2017
1 parent 478d03b commit cf66fea
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
4 changes: 1 addition & 3 deletions Conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* 作者:超能小紫(mokeyjay)
* 博客:https://www.mokeyjay.com
* 源码:https://github.com/mokeyjay/Pixiv-daily-top50-widget
*
* 可随意修改、二次发布。但请保留上方版权声明及注明出处
*/

Expand Down Expand Up @@ -82,7 +81,6 @@ class Conf
public static $enable_smms = FALSE;



/**
* 初始化
*/
Expand Down Expand Up @@ -112,6 +110,6 @@ public static function init()
}
}

if(self::$limit < 1) exit('Conf::$limit can not less than 1');
if (self::$limit < 1) exit('Conf::$limit can not less than 1');
}
}
53 changes: 26 additions & 27 deletions Func.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* 作者:超能小紫(mokeyjay)
* 博客:https://www.mokeyjay.com
* 源码:https://github.com/mokeyjay/Pixiv-daily-top50-widget
*
* 可随意修改、二次发布。但请保留上方版权声明及注明出处
*/

Expand Down Expand Up @@ -74,9 +73,9 @@ public static function getPixivImages(&$image, &$url)
/**
* 从缓存文件获取。这样就不必总是去P站获取排行榜了
*/
if(Conf::$download){
if (Conf::$download){
$source = self::get('source.json');
if(!empty($source['date']) && $source['date'] == date('Y-m-d') && !empty($source['image']) && !empty($source['url'])){
if ( !empty($source['date']) && $source['date'] == date('Y-m-d') && !empty($source['image']) && !empty($source['url'])){
$image = $source['image'];
$url = $source['url'];
return TRUE;
Expand All @@ -96,7 +95,7 @@ public static function getPixivImages(&$image, &$url)
/**
* 缓存起来。这样就不必总是去P站获取排行榜了
*/
if(Conf::$download){
if (Conf::$download){
$data = array(
'date' => date('Y-m-d'),
'image' => $image,
Expand All @@ -113,13 +112,13 @@ public static function getPixivImages(&$image, &$url)
*/
public static function clearOverdue()
{
if(Conf::$download && Conf::$clear_overdue){
if (Conf::$download && Conf::$clear_overdue){
$time = strtotime(date('Ymd')); // 获取今日0点的时间戳,早于此时间戳的文件都得死
if($dh = opendir(Conf::$image_path)){
while(($file = readdir($dh)) !== FALSE){
if($file == '.' || $file == '..') continue;
$file = Conf::$image_path.$file;
if(filemtime($file) < $time) @unlink($file);
if ($dh = opendir(Conf::$image_path)){
while (($file = readdir($dh)) !== FALSE){
if ($file == '.' || $file == '..') continue;
$file = Conf::$image_path . $file;
if (filemtime($file) < $time) @unlink($file);
}
}
}
Expand All @@ -131,13 +130,13 @@ public static function clearOverdue()
*/
public static function checkImage()
{
if(Conf::$download && Conf::$limit){
if($dh = opendir(Conf::$image_path)){
while(($file = readdir($dh)) !== FALSE){
if($file == '.' || $file == '..') continue;
if (Conf::$download && Conf::$limit){
if ($dh = opendir(Conf::$image_path)){
while (($file = readdir($dh)) !== FALSE){
if ($file == '.' || $file == '..') continue;

$file = Conf::$image_path.$file;
if(@getimagesize($file) === FALSE){
$file = Conf::$image_path . $file;
if (@getimagesize($file) === FALSE){
@unlink($file);
return FALSE;
}
Expand All @@ -158,28 +157,28 @@ public static function download(&$images)
{
// 如果设置了对外服务并且图片缓存的话,则强制缓存50张
// 避免服务方设置了limit=10而用户请求limit=50时的问题
if(Conf::$service && Conf::$download) Conf::$limit = 50;
if (Conf::$service && Conf::$download) Conf::$limit = 50;

if(Conf::$download && Conf::$limit){
foreach ($images[0] as $k=>$v){
if(Conf::$service == FALSE && $k >= Conf::$limit) break;
if (Conf::$download && Conf::$limit){
foreach ($images[0] as $k => $v){
if (Conf::$service == FALSE && $k >= Conf::$limit) break;

/**
* 下载P站缩略图
* 并根据配置判断是否使用sm.ms图床
*/
$file = Conf::$image_path.$images[1][$k];
$file = Conf::$image_path . $images[1][$k];
$data = self::curlGet($v);
if(@file_put_contents($file, $data) !== FALSE && Conf::$enable_smms){
if (@file_put_contents($file, $data) !== FALSE && Conf::$enable_smms){
// 上传到sm.ms图床
for ($i=0; $i<3; $i++){ // 最多尝试3次
if($i > 0) sleep(3); // 等待3秒重试
for ($i = 0; $i < 3; $i++){ // 最多尝试3次
if ($i > 0) sleep(3); // 等待3秒重试

$sm = self::smmsUpload($file);
if($sm !== FALSE) break;
if ($sm !== FALSE) break;
}

if($sm !== FALSE){
if ($sm !== FALSE){
$images[0][$k] = $sm;
continue;
}
Expand All @@ -196,7 +195,7 @@ public static function download(&$images)
*/
public static function downloadThread()
{
$ch = curl_init(Conf::$url.'download.php');
$ch = curl_init(Conf::$url . 'download.php');
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
Expand Down
19 changes: 9 additions & 10 deletions download.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* 作者:超能小紫(mokeyjay)
* 博客:https://www.mokeyjay.com
* 源码:https://github.com/mokeyjay/Pixiv-daily-top50-widget
*
* 可随意修改、二次发布。但请保留上方版权声明及注明出处
*/

Expand All @@ -14,28 +13,28 @@
error_reporting(0);

// 判断锁
$lock_file = PX_PATH.'.updatelock';
if(file_exists($lock_file)){
$lock_file = PX_PATH . '.updatelock';
if (file_exists($lock_file)){
// 如果是半小时前的锁,可能是下载线程挂了,判定为下载失败,继续尝试
// 如果是半小时内的锁,则判定下载进行中,退出此线程
$mtime = filemtime($lock_file);
if($mtime !== FALSE && time() - $mtime < 1800) exit;
if ($mtime !== FALSE && time() - $mtime < 1800) exit;
}

require PX_PATH.'Conf.php';
require PX_PATH . 'Conf.php';
Conf::init();
require PX_PATH.'Func.php';
require PX_PATH . 'Func.php';

// 开始下载缓存缩略图
if(Conf::$download){
if (Conf::$download){
file_put_contents($lock_file, time()); // 创建锁

if(Func::getPixivImages($image, $url) && Func::download($image) && Func::checkImage()){
if (Func::getPixivImages($image, $url) && Func::download($image) && Func::checkImage()){
// 更新成功,保存pixiv.json
$data = array(
'date' => date('Y-m-d'),
'date' => date('Y-m-d'),
'image' => $image[0],
'url' => $url[0],
'url' => $url[0],
);
Func::set('pixiv.json', $data);

Expand Down

0 comments on commit cf66fea

Please sign in to comment.