Skip to content

Commit

Permalink
refactor: extract encWbi and generateRemainderScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatpandac committed Sep 16, 2024
1 parent 229cd79 commit 6d5246d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 88 deletions.
52 changes: 17 additions & 35 deletions src/components/video.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { formatUrl } from "../utils";
import { formatUrl, generateRemainderScript } from "../utils";

import { Action, ActionPanel, Color, Icon, Image, List, showToast, Toast } from "@raycast/api";
import { ConclusionView } from "./conslusionView";
import { runAppleScript } from "run-applescript";

const { Metadata } = List.Item.Detail;

export function Video(props: {
title: string;
cover: string;
Expand All @@ -25,27 +27,7 @@ export function Video(props: {
}) {
async function addWatchLaterReminder() {
try {
await runAppleScript(`
tell application "Reminders"
try
get list "Raycast Bilibili"
set mylist to list "Raycast Bilibili"
tell mylist
make new reminder with properties {name:"${props.title} - ${props.uploader.name}", body:"${formatUrl(
props.url
)}"}
end tell
on error
make new list with properties {name:"Raycast Bilibili"}
set mylist to list "Raycast Bilibili"
tell mylist
make new reminder with properties {name:"${props.title} - ${props.uploader.name}", body:"${formatUrl(
props.url
)}"}
end tell
end try
end tell
`);
await runAppleScript(generateRemainderScript(props.title, props.uploader.name, props.url));

await showToast({
style: Toast.Style.Success,
Expand All @@ -65,36 +47,36 @@ export function Video(props: {
<List.Item.Detail
markdown={`<img src="${formatUrl(props.cover)}" center width="300" />`}
metadata={
<List.Item.Detail.Metadata>
<List.Item.Detail.Metadata.Label title={props.title} />
<List.Item.Detail.Metadata.Label
<Metadata>
<Metadata.Label title={props.title} />
<Metadata.Label
title="Uploader"
text={props.uploader.name}
icon={{
source: formatUrl(props.uploader.face),
mask: Image.Mask.Circle,
}}
/>
<List.Item.Detail.Metadata.Label title="Duration" text={String(props.duration)} />
<List.Item.Detail.Metadata.Label title="Time" text={new Date(props.pubdate * 1000).toLocaleString()} />
<List.Item.Detail.Metadata.TagList title="Stat">
<Metadata.Label title="Duration" text={String(props.duration)} />
<Metadata.Label title="Time" text={new Date(props.pubdate * 1000).toLocaleString()} />
<Metadata.TagList title="Stat">
{props.stat.highlight && (
<List.Item.Detail.Metadata.TagList.Item text={props.stat.highlight} color={"#FB7299"} />
<Metadata.TagList.Item text={props.stat.highlight} color={"#FB7299"} />
)}
{props.stat.view && (
<List.Item.Detail.Metadata.TagList.Item text={`Play: ${props.stat.view}`} color={Color.Green} />
<Metadata.TagList.Item text={`Play: ${props.stat.view}`} color={Color.Green} />
)}
{props.stat.coin && (
<List.Item.Detail.Metadata.TagList.Item text={`Coin: ${props.stat.coin}`} color={Color.Orange} />
<Metadata.TagList.Item text={`Coin: ${props.stat.coin}`} color={Color.Orange} />
)}
{props.stat.view && (
<List.Item.Detail.Metadata.TagList.Item text={`View: ${props.stat.view}`} color={Color.Purple} />
<Metadata.TagList.Item text={`View: ${props.stat.view}`} color={Color.Purple} />
)}
{props.stat.danmaku && (
<List.Item.Detail.Metadata.TagList.Item text={`Danmaku: ${props.stat.danmaku}`} color={Color.Blue} />
<Metadata.TagList.Item text={`Danmaku: ${props.stat.danmaku}`} color={Color.Blue} />
)}
</List.Item.Detail.Metadata.TagList>
</List.Item.Detail.Metadata>
</Metadata.TagList>
</Metadata>
}
/>
}
Expand Down
1 change: 0 additions & 1 deletion src/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export default async function Command() {

const unNotifies = newNotifications.slice(0, startNotifyIndex);
for (const unNotify of unNotifies) {
console.log(unNotify);
items.map(async (item) => {
if (item.id_str === unNotify) {
notify(item);
Expand Down
2 changes: 0 additions & 2 deletions src/searchVideos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default function Command() {
}}
>
{videoResults?.map((item) => {
console.log(item)
return (
<Video
title={removeEmHTMLTag(item.title)}
Expand All @@ -50,7 +49,6 @@ export default function Command() {
}}
/>
)

})}
</List>
</CheckLogin>
Expand Down
51 changes: 51 additions & 0 deletions src/utils/encWbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import got from "got";
import crypto from "crypto";
import { Cache } from "@raycast/api";

function getMixinKey(ae: string) {
const oe = [
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38,
41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 36,
20, 34, 44, 52,
];
const le = oe.reduce((s, i) => s + ae[i], "");

return le.slice(0, 32);
}

function md5(s: string) {
return crypto.createHash("md5").update(s, "utf8").digest("hex");
}

function split(s: string) {
const parts = s.split("/");
return parts[parts.length - 1].split(".")[0];
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function encWbi(params: any) {
const cache = new Cache();
const cookie = cache.get("cookie") || "{}";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const resp: any = await got("https://api.bilibili.com/x/web-interface/nav", {
headers: {
cookie,
},
}).json();
const wbi_img = resp.data.wbi_img;
const img_url = wbi_img.img_url || "";
const sub_url = wbi_img.sub_url || "";
const img_value = split(img_url);
const sub_value = split(sub_url);
const me = getMixinKey(img_value + sub_value);
const wts = Math.floor(Date.now() / 1000);

params.wts = wts;

const Ae = Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
.join("&");
const w_rid = md5(Ae + me);

return { w_rid, wts };
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./tools";
export * from "./encWbi";
69 changes: 19 additions & 50 deletions src/utils/tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import got from "got";
import crypto from "crypto";
import { Cache } from "@raycast/api";

export function formatUrl(url: string) {
return url.replace("http://", "https://").replace(/^\/\//, "https://");
}
Expand All @@ -18,52 +14,25 @@ export function formatNumber(number: number | undefined) {
return number ? (number > 9999 ? `${(number / 1000).toFixed(1)}K` : String(number)) : "";
}

function getMixinKey(ae: string) {
const oe = [
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38,
41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 36,
20, 34, 44, 52,
];
const le = oe.reduce((s, i) => s + ae[i], "");

return le.slice(0, 32);
}

function md5(s: string) {
return crypto.createHash("md5").update(s, "utf8").digest("hex");
}

function split(s: string) {
const parts = s.split("/");
return parts[parts.length - 1].split(".")[0];
}

export async function encWbi(params: any) {
const cache = new Cache();
const cookie = cache.get("cookie") || "{}";
const resp: any = await got("https://api.bilibili.com/x/web-interface/nav", {
headers: {
cookie,
},
}).json();
const wbi_img = resp.data.wbi_img;
const img_url = wbi_img.img_url || "";
const sub_url = wbi_img.sub_url || "";
const img_value = split(img_url);
const sub_value = split(sub_url);
const me = getMixinKey(img_value + sub_value);
const wts = Math.floor(Date.now() / 1000);

params.wts = wts;

const Ae = Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
.join("&");
const w_rid = md5(Ae + me);

return { w_rid, wts };
}

export function removeEmHTMLTag(str: string) {
return str.replace(/<em class=["']keyword["']>(.+?)<\/em>/g, "$1");
}

export function generateRemainderScript(title: string, uploaderName: string, url: string) {
return `
tell application "Reminders"
try
get list "Raycast Bilibili"
set mylist to list "Raycast Bilibili"
tell mylist
make new reminder with properties {name:"${title} - ${uploaderName}", body:"${formatUrl(url)}"}
end tell
on error
make new list with properties {name:"Raycast Bilibili"}
set mylist to list "Raycast Bilibili"
tell mylist
make new reminder with properties {name:"${title} - ${uploaderName}", body:"${formatUrl(url)}"}
end tell
end try
end tell`
}

0 comments on commit 6d5246d

Please sign in to comment.