Skip to content

Commit

Permalink
Merge pull request #427 from caorushizi/fix/bilibili-name
Browse files Browse the repository at this point in the history
fix: bilibili video name
  • Loading branch information
caorushizi authored Mar 2, 2025
2 parents 166b72d + 398f17a commit 3c1f837
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 25 deletions.
43 changes: 43 additions & 0 deletions packages/main/src/controller/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { glob } from "glob";
import i18n from "../i18n/index.ts";
import ElectronLogger from "../vendor/ElectronLogger.ts";
import ElectronUpdater from "../vendor/ElectronUpdater.ts";
import axios from "axios";

@injectable()
export default class HomeController implements Controller {
Expand Down Expand Up @@ -426,4 +427,46 @@ export default class HomeController implements Controller {
async getVideoFolders() {
return this.videoRepository.getVideoFolders();
}

@handle("get-page-title")
async getPageTitle(
event: IpcMainEvent,
url: string,
): Promise<{ data: string }> {
try {
console.log("Getting title for URL:", url);

const response = await axios.get(url, {
timeout: 10000,
maxRedirects: 5,
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
});

const html = response.data;
let title = "无标题";

const patterns = [
/<meta\s+property="og:title"\s+content="([^"]*)"/i,
/<meta\s+name="title"\s+content="([^"]*)"/i,
/<title[^>]*>([^<]+)<\/title>/i,
];

for (const pattern of patterns) {
const match = html.match(pattern);
if (match && match[1]) {
title = match[1].trim();
console.log("Found title:", title);
break;
}
}

return { data: title };
} catch (error) {
console.error("Error fetching page title:", error);
return { data: "无标题" };
}
}
}
13 changes: 8 additions & 5 deletions packages/main/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,14 @@ const electronApi = {
return ipcRenderer.invoke("get-video-folders");
},
// ipc with main process to get url params
// onUrlParams(callback: (url: string) => void): void {
// ipcRenderer.on("url-params", (event, url) => {
// callback(url);
// });
// },
// onUrlParams(callback: (url: string) => void): void {
// ipcRenderer.on("url-params", (event, url) => {
// callback(url);
// });
// },
getPageTitle(url: string): Promise<any> {
return ipcRenderer.invoke("get-page-title", url);
},
};

contextBridge.exposeInMainWorld(apiKey, electronApi);
3 changes: 3 additions & 0 deletions packages/renderer/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ const apis: ElectronApi = {
// Implement the logic for handling URL parameters here
// For now, you can leave it as a placeholder
},
getPageTitle: async () => {
return api.post("get-page-title");
},
};

export default apis;
2 changes: 1 addition & 1 deletion packages/renderer/src/components/DownloadForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export default forwardRef<DownloadFormRef, DownloadFormProps>(
label={t("videoName")}
rules={[
{
required: true,
required: form.getFieldsValue().type !== "bilibili",
message: t("pleaseEnterCorrectFomeInfo"),
},
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export function DownloadItem({
});

const renderTitle = useMemoizedFn((item: VideoStat): ReactNode => {
// console.log("====item", item);
return (
<div
className={cn("truncate text-sm dark:text-[#B4B4B4]", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function DownloadList({

const confirmAddItem = useMemoizedFn(
async (values: DownloadFormType, now?: boolean) => {
const { id, name, url, headers, type, folder } = values;
const { id, name = "", url, headers, type, folder } = values;
const item = {
id,
name: name || randomName(),
Expand Down
31 changes: 15 additions & 16 deletions packages/renderer/src/pages/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
downloadItemsNow,
downloadNow,
getLocalIP,
getPageTitle,
addIpcListener,
removeIpcListener,
} = useElectron();
Expand Down Expand Up @@ -139,41 +140,39 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
const {
batch,
batchList = "",
name,
name = "",
headers,
type,
url,
folder,
} = values;

if (batch) {
/**
* Here you need to parse the batchList
* The format is batchList
* url1 name1\n
* url2 name2\n
* url3
* ...
*/
const items: Omit<DownloadItem, "id">[] = batchList
.split("\n")
.map((line: string) => {
const [url, name, folder] = line.trim().split(" ");
const items: Omit<DownloadItem, "id">[] = await Promise.all(
batchList.split("\n").map(async (line: string) => {
const [url, customName, folder] = line.trim().split(" ");
const { data } = await getPageTitle(url.trim());
const pageTitle = data;
return {
url: url.trim(),
name: name || randomName(),
name: customName?.trim() || pageTitle || randomName(),
headers,
type,
folder,
};
});
}),
);

if (now) {
await downloadItemsNow(items);
} else {
await addDownloadItems(items);
}
} else {
const { data } = await getPageTitle(url);
const pageTitle = data;
const item: Omit<DownloadItem, "id"> = {
name,
name: name || pageTitle || randomName(),
url,
headers,
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ export function BrowserView() {

const confirmDownload = useMemoizedFn(async (now?: boolean) => {
try {
const { name, url, headers, type, folder } =
downloadForm.current.getFieldsValue();
const {
name = "",
url,
headers,
type,
folder,
} = downloadForm.current.getFieldsValue();
const item = {
name: name || randomName(),
url,
Expand Down

0 comments on commit 3c1f837

Please sign in to comment.