Skip to content

Commit

Permalink
feat: 增加 TMDB 影视页面搜索支持
Browse files Browse the repository at this point in the history
  • Loading branch information
ronggang committed Dec 27, 2024
1 parent c7ce5d4 commit c1a7c83
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 6 deletions.
19 changes: 19 additions & 0 deletions resource/publicSites/themoviedb.org/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "TMDB",
"ver": "0.0.1",
"plugins": [
{
"name": "影视详情页",
"pages": [
"\/movie\/\\d+(-.+)?",
"\/tv\/\\d+(-.+)?"
],
"scripts": [
"media.js"
]
}
],
"schema": "publicSite",
"host": "www.themoviedb.org",
"path": "themoviedb.org"
}
58 changes: 58 additions & 0 deletions resource/publicSites/themoviedb.org/media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(function ($, window) {
console.log("themoviedb.org media.js is loaded.");
class App {
async init() {
this.initButtons();
// 设置当前页面
PTService.pageApp = this;
}

/**
* 初始化按钮列表
*/
async initButtons() {
let IMDbId = await this.getIMDbId();
if (IMDbId) {
// 搜索
PTService.addButton({
title: "搜索当前电影",
icon: "search",
label: "搜索",
click: (success, error) => {
PTService.call(
PTService.action.openOptions,
`search-torrent/${IMDbId}`
);
success();
}
});
}
}

/**
* 获取 IMDb 编号
*/
async getIMDbId() {
const paths = location.pathname.split("/");
try {
const type = paths[1];
const id = parseInt(paths[2]);
// 获取IMDb ID
const result = await PTService.call(
PTService.action.getIMDbIdFromTMDB,
{
id,
type
}
);

console.log("getIMDbId", result);

return result;
} catch (error) {}

return "";
}
}
new App().init();
})(jQuery, window);
18 changes: 13 additions & 5 deletions src/background/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import { APP } from "@/service/api";
import URLParse from "url-parse";
import { User } from "./user";
import { MovieInfoService } from "@/service/movieInfoService";
import {remote as parseTorrentRemote} from "parse-torrent";
import {PPF} from "@/service/public";
import { remote as parseTorrentRemote } from "parse-torrent";
import { PPF } from "@/service/public";

type Service = PTPlugin;
export default class Controller {
Expand Down Expand Up @@ -919,9 +919,9 @@ export default class Controller {
}
} else {
reject(APP.createErrorMessage(
this.service.i18n.t("service.controller.invalidTorrent", {
link: EWikiLink.faq
})
this.service.i18n.t("service.controller.invalidTorrent", {
link: EWikiLink.faq
})
));
}
break
Expand Down Expand Up @@ -1134,6 +1134,14 @@ export default class Controller {
return this.movieInfoService.getRatings(IMDbId);
}

/**
* 根据指定的 TMDB ID 获取 IMDbId
* @param doubanId
*/
public getIMDbIdFromTMDB(source: Dictionary<any>): Promise<any> {
return this.movieInfoService.getIMDbIdFromTMDB(source);
}

/**
* 根据指定的 doubanId 获取 IMDbId
* @param doubanId
Expand Down
2 changes: 2 additions & 0 deletions src/interface/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export enum EAction {
getMovieRatings = "getMovieRatings",
// 根据指定的 doubanId 获取 IMDbId
getIMDbIdFromDouban = "getIMDbIdFromDouban",
// 根据指定的 TMDB ID 获取 IMDbId
getIMDbIdFromTMDB = "getIMDbIdFromTMDB",
// 从豆瓣查询影片信息
queryMovieInfoFromDouban = "queryMovieInfoFromDouban",
// 添加浏览器原生下载
Expand Down
51 changes: 50 additions & 1 deletion src/service/movieInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type MovieInfoCache = {
ratings: Dictionary<any>;
doubanToIMDb: Dictionary<any>;
search: Dictionary<any>;
tmdbToIMDb: Dictionary<any>;
};

/**
Expand Down Expand Up @@ -133,7 +134,8 @@ export class MovieInfoService {
base: {},
ratings: {},
doubanToIMDb: {},
search: {}
search: {},
tmdbToIMDb: {}
};

// 链接超时时间
Expand Down Expand Up @@ -326,6 +328,53 @@ export class MovieInfoService {
];
}

/**
* 根据TMDB ID获取 IMDb ID
* @param source
* @returns
*/
public async getIMDbIdFromTMDB(source: Dictionary<any>) {
const options = Object.assign({
id: 0,
type: 'movie'
}, source);

if (!options.id) {
return '';
}

const cacheKey = `${options.type}.${options.id}`
let cache = this.cache.tmdbToIMDb[cacheKey];
if (cache) {
return cache;
}
let url = `${this.omitApiURL}/movie/${options.id}/tmdb.${options.type}/imdb`;

if (this.requsetQueue[url]) {
return;
}

this.requsetQueue[url] = true;

try {
const response = await fetch(url);
delete this.requsetQueue[url];
if (response.ok) {
const result = await response.json();
if (result && result.data) {
this.cache.tmdbToIMDb[cacheKey] = result.data;
return result.data;
}
} else {
throw new Error(`HTTP 错误!状态码:${response.status}`);
}
} catch (error) {

}
delete this.requsetQueue[url];
return false;
}

/**
* 根据指定的 doubanId 获取 IMDbId
* @param doubanId
Expand Down

0 comments on commit c1a7c83

Please sign in to comment.