mirror of
https://github.com/kuaifan/dootask.git
synced 2026-02-19 15:04:15 +00:00
perf: 更换笑话和鸡汤接口
This commit is contained in:
parent
6320eaa3ac
commit
7dd5b082cf
@ -174,19 +174,16 @@ class Extranet
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机笑话接口
|
* 随机笑话接口
|
||||||
* @return array
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function randJoke(): array
|
public static function randJoke(): string
|
||||||
{
|
{
|
||||||
$jokeKey = env("JUKE_KEY_JOKE");
|
$data = self::curl("https://hmajax.itheima.net/api/randjoke");
|
||||||
if ($jokeKey) {
|
$data = Base::json2array($data);
|
||||||
$data = self::curl("http://v.juhe.cn/joke/randJoke.php?key=" . $jokeKey);
|
if ($data['message'] === '获取成功' && $text = trim($data['data'])) {
|
||||||
$data = Base::json2array($data);
|
return $text;
|
||||||
if ($data['reason'] === 'success') {
|
|
||||||
return $data['result'];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return [];
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -195,13 +192,10 @@ class Extranet
|
|||||||
*/
|
*/
|
||||||
public static function soups(): string
|
public static function soups(): string
|
||||||
{
|
{
|
||||||
$soupKey = env("JUKE_KEY_SOUP");
|
$data = self::curl("https://hmajax.itheima.net/api/ambition");
|
||||||
if ($soupKey) {
|
$data = Base::json2array($data);
|
||||||
$data = self::curl("https://apis.juhe.cn/fapig/soup/query?key=" . $soupKey);
|
if ($data['message'] === '获取成功' && $text = trim($data['data'])) {
|
||||||
$data = Base::json2array($data);
|
return $text;
|
||||||
if ($data['reason'] === 'success' && $text = trim($data['result']['text'])) {
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,13 +13,12 @@ use Carbon\Carbon;
|
|||||||
/**
|
/**
|
||||||
* 获取笑话、心灵鸡汤
|
* 获取笑话、心灵鸡汤
|
||||||
*
|
*
|
||||||
* 在.env添加笑话 JUKE_KEY_JOKE
|
* 每分钟采集1次
|
||||||
* 在.env添加鸡汤 JUKE_KEY_SOUP
|
|
||||||
*
|
|
||||||
* 每日小时采集1次
|
|
||||||
*/
|
*/
|
||||||
class JokeSoupTask extends AbstractTask
|
class JokeSoupTask extends AbstractTask
|
||||||
{
|
{
|
||||||
|
private $keyPrefix = "JokeSoupTask-v2";
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@ -27,27 +26,25 @@ class JokeSoupTask extends AbstractTask
|
|||||||
|
|
||||||
public function start()
|
public function start()
|
||||||
{
|
{
|
||||||
// 判断每小时执行一次
|
// 判断每分钟执行一次
|
||||||
if (Cache::get("JokeSoupTask:YmdH") == date("YmdH")) {
|
if (Cache::get("{$this->keyPrefix}:YmdHi") == date("YmdHi")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Cache::put("JokeSoupTask:YmdH", date("YmdH"), Carbon::now()->addDay());
|
Cache::put("{$this->keyPrefix}:YmdHi", date("YmdHi"), Carbon::now()->addDay());
|
||||||
//
|
//
|
||||||
$array = Base::json2array(Cache::get("JokeSoupTask:jokes"));
|
$array = Base::json2array(Cache::get("{$this->keyPrefix}:jokes"));
|
||||||
$data = Extranet::randJoke();
|
$data = Extranet::randJoke();
|
||||||
foreach ($data as $item) {
|
if ($data) {
|
||||||
if ($text = trim($item['content'])) {
|
$array[] = $data;
|
||||||
$array[] = $text;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Cache::forever("JokeSoupTask:jokes", Base::array2json(array_slice($array, -100)));
|
Cache::forever("{$this->keyPrefix}:jokes", Base::array2json(array_slice($array, -200)));
|
||||||
//
|
//
|
||||||
$array = Base::json2array(Cache::get("JokeSoupTask:soups"));
|
$array = Base::json2array(Cache::get("{$this->keyPrefix}:soups"));
|
||||||
$data = Extranet::soups();
|
$data = Extranet::soups();
|
||||||
if ($data) {
|
if ($data) {
|
||||||
$array[] = $data;
|
$array[] = $data;
|
||||||
}
|
}
|
||||||
Cache::forever("JokeSoupTask:soups", Base::array2json(array_slice($array, -24)));
|
Cache::forever("{$this->keyPrefix}:soups", Base::array2json(array_slice($array, -200)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function end()
|
public function end()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user