Fix blur effect, implement skeleton loading, and add Duolingo JWT friend streak feature

This commit is contained in:
User
2026-03-12 15:53:23 +08:00
parent 5c0abfd5da
commit 3eaef5b06a
6 changed files with 4835 additions and 3754 deletions

View File

@@ -5270,15 +5270,31 @@ function argon_get_duolingo_data() {
}
}
$url = 'https://www.duolingo.com/2017-06-30/users?username=' . urlencode($username) . '&fields=streak,streakData%7BcurrentStreak,previousStreak%7D%7D';
$url = 'https://www.duolingo.com/2017-06-30/users?username=' . urlencode($username) . '&fields=id,streak,streakData%7BcurrentStreak,previousStreak%7D,quests,friendStreaks,friendships,following';
$headers = array(
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
);
$jwt = get_option('argon_duolingo_jwt', '');
if (!empty($jwt)) {
$headers['Authorization'] = 'Bearer ' . $jwt;
}
$response = wp_remote_get($url, array(
'timeout' => 10,
'headers' => array(
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
)
'timeout' => 15,
'headers' => $headers
));
// 如果返回了诸如 401/403 错误JWT 过期或无效),尝试移除 JWT 重新获取,确保基础连胜可见
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) !== 200 && !empty($jwt)) {
unset($headers['Authorization']);
$response = wp_remote_get($url, array(
'timeout' => 15,
'headers' => $headers
));
$jwt = ''; // 标记为无效,后续不请求私有好友连胜数据
}
if (is_wp_error($response)) {
// 请求失败时返回旧缓存(如果有)
return $cached !== false ? $cached : false;
@@ -5305,10 +5321,48 @@ function argon_get_duolingo_data() {
$end_date = isset($user['streakData']['currentStreak']['endDate']) ? $user['streakData']['currentStreak']['endDate'] : '';
$is_today_done = ($end_date === $today);
// 尝试解析好友连胜 (Friend Streak)
$friend_streak = 0;
// 只有在 JWT 有效时才继续拉取私密接口
if (!empty($jwt)) {
// 请求额外的好友连胜数据
$user_id = isset($user['id']) ? $user['id'] : 0;
if ($user_id) {
$friend_url = 'https://www.duolingo.com/2017-06-30/friends/users/' . $user_id . '?fields=friendStreaks';
$friend_resp = wp_remote_get($friend_url, array('timeout' => 15, 'headers' => $headers));
if (!is_wp_error($friend_resp)) {
$friend_body = wp_remote_retrieve_body($friend_resp);
$friend_data = json_decode($friend_body, true);
// 写出诊断日志以便开发者后续找字段
$debug_info = array('user_api' => $user, 'friend_api' => $friend_data);
file_put_contents(get_template_directory() . '/duolingo_debug.json', json_encode($debug_info, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// 暂时尝试猜测常用名字
if (isset($user['friendStreaks'])) {
// 如果是在 user 对象里
foreach ((array)$user['friendStreaks'] as $fs) {
if (isset($fs['streakLength']) && $fs['streakLength'] > $friend_streak) $friend_streak = $fs['streakLength'];
if (isset($fs['length']) && $fs['length'] > $friend_streak) $friend_streak = $fs['length'];
}
}
if (isset($friend_data['friendStreaks'])) {
// 如果是在 friends 对象里
foreach ((array)$friend_data['friendStreaks'] as $fs) {
if (isset($fs['streakLength']) && $fs['streakLength'] > $friend_streak) $friend_streak = $fs['streakLength'];
if (isset($fs['length']) && $fs['length'] > $friend_streak) $friend_streak = $fs['length'];
}
}
}
}
}
$result = array(
'streak' => $streak,
'today' => $is_today_done,
'date' => $today
'date' => $today,
'friend_streak' => $friend_streak > 0 ? $friend_streak : ''
);
// 如果今日已完成缓存到明天0点否则缓存15分钟