Revert Duolingo JWT friend streak implementation

This commit is contained in:
User
2026-03-12 16:17:56 +08:00
parent 0387c7d9e5
commit d1bf6be19d
5 changed files with 15 additions and 87 deletions

View File

@@ -5246,17 +5246,20 @@ function argon_get_duolingo_streak() {
}
// 获取多邻国完整数据(包含今日是否完成)
function argon_get_duolingo_data() {
function argon_get_duolingo_data($force_refresh = false) {
if (get_option('argon_show_duolingo_streak', 'false') != 'true') {
return false;
}
$username = get_option('argon_duolingo_username', '');
if (empty($username)) {
return false;
}
$cache_key = 'argon_duolingo_v2_' . md5($username);
$cache_key = 'argon_duolingo_data_' . md5($username);
$cached = get_transient($cache_key);
// 如果有缓存
if ($cached !== false) {
if (!$force_refresh && $cached !== false) {
// 如果今日已完成直接返回缓存到第二天0点前不会变
if (isset($cached['today']) && $cached['today']) {
// 检查是否还是同一天
@@ -5270,28 +5273,16 @@ function argon_get_duolingo_data() {
}
}
$url = 'https://www.duolingo.com/2017-06-30/users?username=' . urlencode($username) . '&fields=id,streak,streakData%7BcurrentStreak,previousStreak%7D,quests,friendStreaks,friendships,following';
$url = 'https://www.duolingo.com/2017-06-30/users?username=' . urlencode($username) . '&fields=streak,streakData%7BcurrentStreak,previousStreak%7D';
$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' => 15,
'headers' => $headers
'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'
)
));
// 如果返回了诸如 401/403 错误,说明可能遭到了 Cloudflare 拦截或者是 JWT 无效
// 我们直接返回缓存,而不是剥离 JWT 去重试,因为当前服务器 IP 很可能就是被封的
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) !== 200) {
return $cached !== false ? $cached : false;
}
if (is_wp_error($response)) {
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
// 请求失败时返回旧缓存(如果有)
return $cached !== false ? $cached : false;
}
@@ -5317,48 +5308,10 @@ 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,
'friend_streak' => $friend_streak > 0 ? $friend_streak : ''
'date' => $today
);
// 如果今日已完成缓存到明天0点否则缓存15分钟