识别豆瓣观影的链接并解析
This commit is contained in:
浪子 2024-07-27 09:13:57 +08:00
parent 52f8e45de0
commit 1c18ec0593
3 changed files with 112 additions and 39 deletions

26
db.php Normal file
View File

@ -0,0 +1,26 @@
<?php
$movie_id = $_GET['id'];
$api_url = "https://api.loliko.cn/movies/{$movie_id}";
$context = stream_context_create([
'http' => [
'ignore_errors' => true,
'timeout' => 10 // 设置10秒超时
]
]);
$response = file_get_contents($api_url, false, $context);
if ($response === FALSE) {
header("HTTP/1.1 500 Internal Server Error");
echo json_encode(['error' => 'Failed to fetch data from API']);
} else {
$responseData = json_decode($response, true);
if (json_last_error() === JSON_ERROR_NONE) {
header('Content-Type: application/json');
echo $response;
} else {
header("HTTP/1.1 500 Internal Server Error");
echo json_encode(['error' => 'Invalid JSON response from API']);
}
}

View File

@ -235,45 +235,6 @@
border-color: #3f3f46
}
@font-face {
font-family: open sans;
src: url(../fonts/OpenSans-Regular.woff2)format("woff2"), url(../fonts/OpenSans-Regular.woff)format("woff");
font-weight: 400;
font-style: normal;
font-display: swap
}
@font-face {
font-family: open sans;
src: url(../fonts/OpenSans-Bold.woff2)format("woff2"), url(../fonts/OpenSans-Bold.woff)format("woff");
font-weight: 700;
font-style: normal;
font-display: swap
}
@font-face {
font-family: open sans;
src: url(../fonts/OpenSans-Light.woff2)format("woff2"), url(../fonts/OpenSans-Light.woff)format("woff");
font-weight: 200;
font-style: normal;
font-display: swap
}
@font-face {
font-family: open sans;
src: url(../fonts/OpenSans-Medium.woff2)format("woff2"), url(../fonts/OpenSans-Medium.woff)format("woff");
font-weight: 500;
font-style: normal;
font-display: swap
}
@font-face {
font-family: mrs_saint_delafieldregular;
src: url(../fonts/mrssaintdelafield-regular-webfont.woff2)format("woff2"), url(../fonts/mrssaintdelafield-regular-webfont.woff)format("woff");
font-weight: 400;
font-style: normal
}
/*!normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css*/
html {
line-height: 1.15;

View File

@ -228,6 +228,92 @@ document.addEventListener('DOMContentLoaded', (event) => {
currentList.appendChild(item);
});
});
</script>
<script>
function fetchWithRetry(url, retries = 3) {
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.text(); // 首先获取文本响应
})
.then(text => {
try {
return JSON.parse(text); // 尝试解析 JSON
} catch (e) {
console.error('Invalid JSON:', text);
throw new Error('Invalid JSON response');
}
})
.catch(error => {
if (retries > 0) {
console.log(`Retrying... (${retries} attempts left)`);
return new Promise(resolve => setTimeout(resolve, 1000)) // 等待1秒
.then(() => fetchWithRetry(url, retries - 1));
}
throw error;
});
}
document.addEventListener('DOMContentLoaded', function() {
const doubanLinks = document.querySelectorAll('a[href^="https://movie.douban.com/subject/"]');
doubanLinks.forEach(link => {
const url = link.href;
const movieId = url.match(/subject\/(\d+)/)[1];
link.innerHTML += ' <span class="loading">(加载中...)</span>';
fetchWithRetry(`<?php $this->options->themeUrl('db.php'); ?>?id=${movieId}`)
.then(data => {
const movieInfo = createMovieInfoHTML(data, url);
const wrapper = document.createElement('div');
wrapper.innerHTML = movieInfo;
link.parentNode.replaceChild(wrapper, link);
})
.catch(error => {
console.error('Error fetching movie data:', error);
// 显示错误消息给用户
link.innerHTML = `<span style="color: red;">加载失败</span> <a href="${url}" target="_blank">查看豆瓣电影详情</a>`;
})
.finally(() => {
const loadingSpan = link.querySelector('.loading');
if (loadingSpan) {
loadingSpan.remove();
}
});
});
});
function createMovieInfoHTML(data, originalUrl) {
if (!data || data.error || Object.keys(data).length === 0) {
return `<a href="${originalUrl}" target="_blank">查看豆瓣电影详情</a>`;
}
return `
<div class=doulist-item>
<div class=doulist-subject>
<div class=doulist-post>
<img decoding=async referrerpolicy=no-referrer src=${data.img}>
</div>
<div class=doulist-content>
<div class=doulist-title>
<a href="${originalUrl}" class=cute target="_blank" rel="external nofollow"> ${data.name} </a>
</div>
<div class=rating>
<span class=rating_nums>豆瓣评分 : ${data.rating}</span>
</div>
<div class=abstract>
${data.year} · ${data.country} · ${data.genre} · 导演: ${data.director} · 演员 : ${data.actor}
</div>
</div>
</div>
</div>
`;
}
</script>
<?php $this->need('footer.php'); ?>