Skip to content

Commit

Permalink
j
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobby233Liu committed Mar 4, 2023
1 parent d2027a2 commit 90c657d
Show file tree
Hide file tree
Showing 12 changed files with 958 additions and 215 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
env: {
es2021: true,
node: true,
},
extends: "eslint:recommended",
overrides: [],
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
rules: {},
};
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

## 特别鸣谢

* 本项目灵感来源于 [TwitFix](https://github.com/dylanpdx/BetterTwitFix),我也对其代码做了参考。
* 与 B 站 API 交互的代码参考了 [哔哩哔哩-API收集整理](https://github.com/SocialSisterYi/bilibili-API-collect)
* [@狸花十七](https://space.bilibili.com/5490502) 的视频——确切来说是上图中的那则视频——导致我产生了开坑这个项目的想法。(十七宝宝就是狸花界的颜值担当!)
* [BEMYPET](https://space.bilibili.com/1677731862) 产生的内容在本项目开发过程中对我造成了一定心理创伤。(误)
* 我还可以加人吗?
- 本项目灵感来源于 [TwitFix](https://github.com/dylanpdx/BetterTwitFix),我也对其代码做了参考。
- 与 B 站 API 交互的代码参考了 [哔哩哔哩-API 收集整理](https://github.com/SocialSisterYi/bilibili-API-collect)
- [@狸花十七](https://space.bilibili.com/5490502) 的视频——确切来说是上图中的那则视频——导致我产生了开坑这个项目的想法。(十七宝宝就是狸花界的颜值担当!)
- [BEMYPET](https://space.bilibili.com/1677731862) 产生的内容在本项目开发过程中对我造成了一定心理创伤。(误)
- 我还可以加人吗?

## 许可证

Expand Down
2 changes: 1 addition & 1 deletion api/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import handler from "../src/index.js";
export default handler;
export default handler;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"version": "0.1.0",
"license": "MIT",
"dependencies": {
"@types/node-fetch": "^2.6.2",
"@vercel/fetch": "^6.2.0",
"date-fns": "^2.29.3",
"ejs": "^3.1.8",
Expand All @@ -14,6 +13,9 @@
},
"devDependencies": {
"@types/ejs": "^3.1.2",
"@types/xml2js": "^0.4.11"
"@types/node-fetch": "^2.6.2",
"@types/xml2js": "^0.4.11",
"eslint": "^8.35.0",
"prettier": "2.8.4"
}
}
5 changes: 3 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export const PROVIDER_NAME = `哔哩哔哩(${MY_NAME})`;
export const PROVIDER_URL = "https://www.bilibili.com";

export const PROJECT_URL = "https://github.com/Dobby233Liu/bembedfix";
export const PROJECT_ISSUES_URL = "https://github.com/Dobby233Liu/bembedfix/issues/new";
export const PROJECT_ISSUES_URL =
"https://github.com/Dobby233Liu/bembedfix/issues/new";

export const CRAWLER_UAS = [
"facebookexternalhit/1.1",
Expand All @@ -17,7 +18,7 @@ export const CRAWLER_UAS = [
"Mozilla/5.0 (Windows; U; Windows NT 10.0; en-US; Valve Steam Client/default/1596241936; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",
"Mozilla/5.0 (Windows; U; Windows NT 10.0; en-US; Valve Steam Client/default/0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; Valve Steam FriendsUI Tenfoot/0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
"BembedfixMetaTagDebugging (like test)"
"BembedfixMetaTagDebugging (like test)",
];

export const ERROR_TEMPLATE = `<!DOCTYPE HTML>
Expand Down
44 changes: 37 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { sendOembed, sendTemplate, sendError, getRequestedURL, getMyBaseURL, stripTrailingSlashes, isUAEndUser, shouldLieAboutPlayerContentType, oembedAddExtraMetadata } from "./utils.js";
import { getRequestedInfo, getVideoData, loadOembedDataFromQuerystring } from "./utils_bilibili.js";
import {
sendOembed,
sendTemplate,
sendError,
getRequestedURL,
getMyBaseURL,
stripTrailingSlashes,
isUAEndUser,
shouldLieAboutPlayerContentType,
oembedAddExtraMetadata,
} from "./utils.js";
import {
getRequestedInfo,
getVideoData,
loadOembedDataFromQuerystring,
} from "./utils_bilibili.js";
import { PROJECT_URL, PROVIDER_NAME } from "./constants.js";

export default async function handler(req, res) {
Expand All @@ -21,11 +35,17 @@ export default async function handler(req, res) {

let doOembed = false;
let responseType = "html";
if (stripTrailingSlashes(requestedURL.pathname) == "/oembed" || requestedURL.pathname == "/oembed.json" || requestedURL.pathname == "/oembed.xml") {
if (
stripTrailingSlashes(requestedURL.pathname) == "/oembed" ||
requestedURL.pathname == "/oembed.json" ||
requestedURL.pathname == "/oembed.xml"
) {
doOembed = true;
let isXMLRequested = false;
if (!requestedURL.pathname.endsWith(".json"))
isXMLRequested = requestedURL.pathname.endsWith(".xml") || req.query.format == "xml";
isXMLRequested =
requestedURL.pathname.endsWith(".xml") ||
req.query.format == "xml";
responseType = isXMLRequested ? "xml" : "json";
}

Expand All @@ -36,7 +56,10 @@ export default async function handler(req, res) {

let info, data;
try {
info = await getRequestedInfo((!doOembed ? requestedURL : new URL(req.query.url)).pathname, requestedURL.searchParams);
info = await getRequestedInfo(
(!doOembed ? requestedURL : new URL(req.query.url)).pathname,
requestedURL.searchParams
);
} catch (e) {
sendError(res, req, "解析请求的 URL 时发生错误", e, responseType);
return;
Expand All @@ -49,13 +72,20 @@ export default async function handler(req, res) {
}

if (doOembed) {
sendOembed(res, oembedAddExtraMetadata(data.oembedData, req.query), responseType);
sendOembed(
res,
oembedAddExtraMetadata(data.oembedData, req.query),
responseType
);
return;
}

try {
if (isUAEndUser(req)) {
res.setHeader("Cache-Control", "private, max-age=1, stale-while-revalidate");
res.setHeader(
"Cache-Control",
"private, max-age=1, stale-while-revalidate"
);
// redirect the client to the real video URL
res.redirect(302, data.url);
return;
Expand Down
143 changes: 82 additions & 61 deletions src/templates/video.html
Original file line number Diff line number Diff line change
@@ -1,63 +1,84 @@
<!DOCTYPE HTML>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8" />
<title><%= title %></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Refresh" content="0; url='<%= url %>'" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta property="application-name" content="<%= provider %>" />
<meta property="og:site_name" content="<%= provider %>" />
<meta name="theme-color" content="#de698c" />
<link rel="canonical" href="<%= url %>" />
<meta name="title" content="<%= title %>" />
<meta name="language" content="zh_CN" />
<meta name="description" content="<%= compat_description %>">
<link rel="alternate" href="<%= oembed_json %>" type="application/json+oembed" title="<%= title %>" />
<link rel="alternate" href="<%= oembed_xml %>" type="text/xml+oembed" title="<%= title %>" />
<meta property="og:type" content="video.other" />
<meta property="og:title" content="<%= title %>" />
<meta property="og:url" content="<%= url %>" />
<meta property="og:author" content="<%= author %>" />
<meta property="og:image" content="<%= thumbnail %>">
<meta property="og:description" content="<%= compat_description %>">
<meta property="og:image:width" content="<%= thumbnail_width %>">
<meta property="og:image:height" content="<%= thumbnail_height %>">
<meta property="og:video:url" content="<%= embed_url %>" />
<meta property="og:video:secure_url" content="<%= embed_url %>" />
<meta property="og:video:type" content="<%= lie_about_embed_player ? 'video/mp4' : 'text/html'%>">
<meta property="og:video:width" content="<%= width %>" />
<meta property="og:video:height" content="<%= height %>" />
<meta property="twitter:card" content="player" />
<meta property="twitter:url" content="<%= url %>" />
<meta property="twitter:title" content="<%= title %>" />
<meta name="twitter:description" content="<%= compat_description %>">
<meta property="twitter:image" content="<%= thumbnail %>" />
<meta property="twitter:player" content="<%= embed_url %>" />
<meta property="twitter:player:width" content="<%= width %>" />
<meta property="twitter:player:height" content="<%= height %>" />
<div itemscope itemid="" itemtype="http://schema.org/VideoObject">
<link itemprop="url" href="<%= url %>">
<meta itemprop="name" content="<%= title %>">
<meta itemprop="description" content="<%= compat_description %>">
<meta itemprop="duration" content="<%= duration %>">
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<link itemprop="url" href="<%= author_url %>">
<link itemprop="name" content="<%= author %>">
</span>
<link itemprop="thumbnailUrl" href="<%= thumbnail %>">
<span itemprop="thumbnail" itemscope itemtype="http://schema.org/ImageObject">
<link itemprop="url" href="<%= thumbnail %>">
<meta itemprop="width" content="<%= thumbnail_width %>">
<meta itemprop="height" content="<%= thumbnail_height %>">
</span>
<link itemprop="embedUrl" href="<%= embed_url %>">
<meta itemprop="width" content="<%= width %>">
<meta itemprop="height" content="<%= height %>">
<meta itemprop="playerType" content="HTML5">
</div>
</head>
<body>
<p><a href="<%= url %>">正在重定向到 <%= url %></a></p>
</body>
<head>
<meta charset="UTF-8" />
<title><%= title %></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Refresh" content="0; url='<%= url %>'" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta property="application-name" content="<%= provider %>" />
<meta property="og:site_name" content="<%= provider %>" />
<meta name="theme-color" content="#de698c" />
<link rel="canonical" href="<%= url %>" />
<meta name="title" content="<%= title %>" />
<meta name="language" content="zh_CN" />
<meta name="description" content="<%= compat_description %>" />
<link
rel="alternate"
href="<%= oembed_json %>"
type="application/json+oembed"
title="<%= title %>"
/>
<link
rel="alternate"
href="<%= oembed_xml %>"
type="text/xml+oembed"
title="<%= title %>"
/>
<meta property="og:type" content="video.other" />
<meta property="og:title" content="<%= title %>" />
<meta property="og:url" content="<%= url %>" />
<meta property="og:author" content="<%= author %>" />
<meta property="og:image" content="<%= thumbnail %>" />
<meta property="og:description" content="<%= compat_description %>" />
<meta property="og:image:width" content="<%= thumbnail_width %>" />
<meta property="og:image:height" content="<%= thumbnail_height %>" />
<meta property="og:video:url" content="<%= embed_url %>" />
<meta property="og:video:secure_url" content="<%= embed_url %>" />
<meta
property="og:video:type"
content="<%= lie_about_embed_player ? 'video/mp4' : 'text/html'%>"
/>
<meta property="og:video:width" content="<%= width %>" />
<meta property="og:video:height" content="<%= height %>" />
<meta property="twitter:card" content="player" />
<meta property="twitter:url" content="<%= url %>" />
<meta property="twitter:title" content="<%= title %>" />
<meta name="twitter:description" content="<%= compat_description %>" />
<meta property="twitter:image" content="<%= thumbnail %>" />
<meta property="twitter:player" content="<%= embed_url %>" />
<meta property="twitter:player:width" content="<%= width %>" />
<meta property="twitter:player:height" content="<%= height %>" />
<div itemscope itemid="" itemtype="http://schema.org/VideoObject">
<link itemprop="url" href="<%= url %>" />
<meta itemprop="name" content="<%= title %>" />
<meta itemprop="description" content="<%= compat_description %>" />
<meta itemprop="duration" content="<%= duration %>" />
<span
itemprop="author"
itemscope
itemtype="http://schema.org/Person"
>
<link itemprop="url" href="<%= author_url %>" />
<link itemprop="name" content="<%= author %>" />
</span>
<link itemprop="thumbnailUrl" href="<%= thumbnail %>" />
<span
itemprop="thumbnail"
itemscope
itemtype="http://schema.org/ImageObject"
>
<link itemprop="url" href="<%= thumbnail %>" />
<meta itemprop="width" content="<%= thumbnail_width %>" />
<meta itemprop="height" content="<%= thumbnail_height %>" />
</span>
<link itemprop="embedUrl" href="<%= embed_url %>" />
<meta itemprop="width" content="<%= width %>" />
<meta itemprop="height" content="<%= height %>" />
<meta itemprop="playerType" content="HTML5" />
</div>
</head>
<body>
<p><a href="<%= url %>">正在重定向到 <%= url %></a></p>
</body>
</html>
Loading

0 comments on commit 90c657d

Please sign in to comment.