Skip to content

Commit

Permalink
fix: 替换imdb获取豆瓣id的方法 豆瓣需要在已登陆状态
Browse files Browse the repository at this point in the history
  • Loading branch information
techmovie committed Apr 26, 2023
1 parent c1fddda commit ec46ca1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
CURRENT_SITE_INFO, PIC_URLS,
DOUBAN_API_URL, DOUBAN_SEARCH_API, CURRENT_SITE_NAME,
DOUBAN_API_URL, DOUBAN_SEARCH_API, CURRENT_SITE_NAME, DOUBAN_SUGGEST_API,
} from './const';
const isChinese = (title) => {
return /[\u4e00-\u9fa5]+/.test(title);
Expand Down Expand Up @@ -89,6 +89,33 @@ const getDoubanId = async (imdbId) => {
console.log(error);
}
};
const getDoubanIdByIMDB = async (imdbId) => {
try {
const url = DOUBAN_SUGGEST_API.replace('{query}', imdbId);
const options = {
cookie: '',
anonymous: false,
responseType: undefined,
};
const data = await fetch(url, options);
const doc = new DOMParser().parseFromString(data, 'text/html');
const linkDom = doc.querySelector('.result-list .result h3 a');
if (!linkDom) {
throw new Error('豆瓣ID获取失败');
} else {
const { href, textContent } = linkDom;
const season = textContent?.match(/第(.+?)季/)?.[1] ?? '';
const doubanId = decodeURIComponent(href).match(/subject\/(\d+)/)?.[1] ?? '';
return ({
id: doubanId,
season,
title: textContent || '',
});
}
} catch (error) {
throw new Error(error.message);
}
};
const getTvSeasonData = (data) => {
const torrentTitle = getTorrentTitle();
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -300,4 +327,5 @@ export {
getDoubanId,
createDoubanDom,
getTvSeasonData,
getDoubanIdByIMDB,
};
2 changes: 2 additions & 0 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const CURRENT_SITE_INFO = siteInfo;
const CURRENT_SITE_NAME = CURRENT_SITE_INFO?.siteName ?? '';
const DOUBAN_API_URL = 'https://movie.douban.com/subject/{doubanId}';
const DOUBAN_SEARCH_API = 'https://movie.douban.com/j/subject_suggest?q={query}';
const DOUBAN_SUGGEST_API = 'https://www.douban.com/search?cat=1002&q={query}';
const PIC_URLS = {
border: 'https://ptpimg.me/zz4632.png',
icon2x: 'https://ptpimg.me/n74cjc.png',
Expand All @@ -25,4 +26,5 @@ export {
DOUBAN_API_URL,
DOUBAN_SEARCH_API,
PIC_URLS,
DOUBAN_SUGGEST_API,
};
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
CURRENT_SITE_INFO, CURRENT_SITE_NAME,
} from './const';
import {
getImdbId, getDoubanId, getTvSeasonData, createDoubanDom,
getDoubanInfo, addToPtpPage,
getImdbId, getTvSeasonData, createDoubanDom,
getDoubanInfo, addToPtpPage, getDoubanIdByIMDB,
} from './common';
import './style.js';
(async () => {
Expand All @@ -17,7 +17,7 @@ import './style.js';
if (!savedIds[imdbId] ||
(savedIds[imdbId] && savedIds[imdbId].updateTime && Date.now() - savedIds[imdbId].updateTime >= 30 * 24 * 60 * 60 * 1000)) {
let doubanId = '';
const movieData = await getDoubanId(imdbId);
const movieData = await getDoubanIdByIMDB(imdbId);
if (!movieData) {
throw new Error('没有找到豆瓣条目');
}
Expand Down

2 comments on commit ec46ca1

@ningsibuqu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JayXon/MoreMovieRatings@3d48729

这个解决方法不需要登录,可以看看能不能参考。

@techmovie
Copy link
Owner Author

@techmovie techmovie commented on ec46ca1 Apr 28, 2023 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.