Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Jul 21, 2021
0 parents commit 1420979
Show file tree
Hide file tree
Showing 17 changed files with 709 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main.js
31 changes: 31 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
extends: ["prettier", "plugin:prettier/recommended"],
env: {
browser: true,
node: true,
},
plugins: [
"@typescript-eslint",
"jsdoc",
"prefer-arrow",
"simple-import-sort",
],
rules: {
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",
"prefer-arrow/prefer-arrow-functions": [
"warn",
{
disallowPrototype: true,
singleReturnOnly: false,
classPropertiesAllowed: false,
},
],
},
};
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Intellij
*.iml
.idea

# npm
node_modules
package-lock.json

# build
main.js
*.js.map
styles.css

# saved config
data.json
20 changes: 20 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "all",
"overrides": [
{
"files": ".prettierrc",
"options": {
"parser": "json"
}
},
{
"files": "*.yml",
"options": {
"tabWidth": 2,
"singleQuote": false
}
}
]
}
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Media Extended BiliBili Plugin

Add bilibili videos support for Media Extended plugin

## Intro

## How to use

## Compatibility

The required API feature is only available for Obsidian v0.9.12+.

## Installation

### From GitHub

1. Download the Latest Release from the Releases section of the GitHub Repository
2. Put files to your vault's plugins folder: `<vault>/.obsidian/plugins/mx-bili-plugin`
3. Reload Obsidian
4. If prompted about Safe Mode, you can disable safe mode and enable the plugin.
Otherwise, head to Settings, third-party plugins, make sure safe mode is off and
enable the plugin from there.

> Note: The `.obsidian` folder may be hidden. On macOS, you should be able to press `Command+Shift+Dot` to show the folder in Finder.
### From Obsidian

> Not yet available
1. Open `Settings` > `Third-party plugin`
2. Make sure Safe mode is **off**
3. Click `Browse community plugins`
4. Search for this plugin
5. Click `Install`
6. Once installed, close the community plugins window and the patch is ready to use.
10 changes: 10 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "mx-bili-plugin",
"name": "Media Extended BiliBili Plugin",
"version": "0.0.0",
"minAppVersion": "0.12.2",
"description": "Add bilibili videos support for Media Extended plugin",
"author": "AidenLx",
"authorUrl": "https://github.com/AidenLx/",
"isDesktopOnly": true
}
97 changes: 97 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "mx-bili-plugin",
"version": "0.0.0",
"description": "Add bilibili videos support for Media Extended plugin",
"main": "main.js",
"scripts": {
"dev": "sed -i '' \"s#return adapter;#return require('./adapters/http');#\" node_modules/axios/lib/defaults.js && rollup --config rollup.config.js -w",
"build": "sed -i '' \"s#return adapter;#return require('./adapters/http');#\" node_modules/axios/lib/defaults.js && rollup --config rollup.config.js",
"prettier": "prettier --write 'src/**/*.+(ts|tsx|json|html|css)'",
"eslint": "eslint . --ext .ts,.tsx --fix",
"release": "release-it"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@release-it/bumper": "^3.0.1",
"@release-it/conventional-changelog": "^3.0.1",
"@rollup/plugin-commonjs": "^19.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.2",
"@rollup/plugin-typescript": "^8.2.3",
"@types/express": "^4.17.13",
"@types/json-schema": "^7.0.8",
"@types/node": "^16.4.0",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"assert-never": "^1.2.1",
"axios": "^0.21.1",
"bili-api": "github:aidenlx/bili-api",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-jsdoc": "^35.5.0",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"express": "^4.17.1",
"http-proxy-middleware": "^2.0.1",
"json": "^11.0.0",
"obsidian": "^0.12.11",
"prettier": "^2.3.2",
"release-it": "^14.10.0",
"rollup": "^2.53.3",
"tslib": "^2.3.0",
"typescript": "^4.3.5",
"xmlbuilder2": "^2.4.1"
},
"release-it": {
"hooks": {
"before:init": [
"npm run prettier",
"npm run eslint"
],
"after:bump": [
"json -I -f manifest.json -e \"this.version='${version}'\"",
"json -I -f versions.json -e \"this['${version}']='$(cat manifest.json | json minAppVersion)'\"",
"sed -i '' \"s/available for Obsidian v.*$/available for Obsidian v$(cat manifest.json | json minAppVersion)+./\" README.md",
"git Add.",
"npm run build"
],
"after:release": "echo Successfully released ${name} v${version} to ${repo.repository}."
},
"git": {
"commitMessage": "chore: release v${version}",
"tagName": "${version}",
"tagAnnotation": "Release v${version}"
},
"npm": {
"publish": false
},
"github": {
"release": true,
"assets": [
"main.js",
"manifest.json",
"styles.css"
],
"proxy": "http://127.0.0.1:7890",
"releaseName": "${version}"
},
"plugins": {
"@release-it/bumper": {
"out": "manifest.json"
},
"@release-it/conventional-changelog": {
"preset": "angular",
"infile": "CHANGELOG.md"
}
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
26 changes: 26 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";

const isProd = process.env.BUILD === "production";

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/
`;

export default {
input: "src/mxbili-main.ts",
output: {
file: "main.js",
sourcemap: "inline",
sourcemapExcludeSources: isProd,
format: "cjs",
exports: "default",
banner,
},
external: ["obsidian"],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs(), json()],
};
63 changes: 63 additions & 0 deletions src/fake-bili/bili-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import axios from "axios";
import { returnBody } from "bili-api/player/general";
import * as Pagelist from "bili-api/player/pagelist";
import * as PlayUrl from "bili-api/player/playurl";

export const enum vidType {
avid,
bvid,
}

export const isBiliVId = (id: string | number): vidType | null => {
if (typeof id === "number" && Number.isInteger(id)) return vidType.avid;
else if (typeof id === "string" && /^bv/i.test(id)) return vidType.bvid;
else console.log("Invalid id: " + id);
return null;
};

export const isDash = (obj: PlayUrl.Data): obj is PlayUrl.DashData =>
(obj as PlayUrl.DashData).dash !== undefined;

export const isTrad = (obj: PlayUrl.Data): obj is PlayUrl.TradData =>
(obj as PlayUrl.TradData).durl !== undefined;

/**
* 获取视频流URL(web端)
* @param F = typeof args.fnval
*/
export const getPlayUrl = <F>(args: PlayUrl.Params, cookie?: string) => {
const url = "http://api.bilibili.com/x/player/playurl";

// @ts-ignore
const { avid, bvid } = args;

if (
(avid && isBiliVId(avid) === vidType.avid) ||
(bvid && isBiliVId(bvid) === vidType.bvid)
) {
let headers: any = {};
if (cookie) headers.Cookie = cookie;
return axios.get<
F extends PlayUrl.fetch_method.dash
? returnBody<PlayUrl.DashData>
: returnBody<PlayUrl.TradData>
>(url, { params: args, headers });
} else throw new TypeError(`Invalid avid ${avid}/bvid ${bvid}`);
};

/**
* 查询视频分P列表 (avID转CID)
*/
export const getPageList = async (arg: Pagelist.Params) => {
const url = "http://api.bilibili.com/x/player/pagelist";

// @ts-ignore
const { aid, bvid } = arg;

if (
(aid && isBiliVId(aid) === vidType.avid) ||
(bvid && isBiliVId(bvid) === vidType.bvid)
)
return axios.get<Pagelist.Return>(url, { params: arg });
else throw new TypeError(`Invalid aid ${aid}/bvid ${bvid}`);
};
56 changes: 56 additions & 0 deletions src/fake-bili/dash-tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { DashData } from "bili-api/player/playurl";
import { create } from "xmlbuilder2";

export const toMPD = (data: DashData) => {
const d = data.dash;

let videos = d.video
.filter((v) => v.codecs.startsWith("avc1"))
.map((v) => ({
BaseURL: v.baseUrl,
"@id": v.id,
"@mimeType": v.mimeType,
"@bandwidth": v.bandwidth,
"@codecs": v.codecs,
"@width": v.width,
"@height": v.height,
"@frameRate": v.frameRate,
"@sar": v.sar,
"@startWithSap": v.startWithSap,
SegmentBase: {
"@indexRange": v.SegmentBase.indexRange,
"@Initialization": v.SegmentBase.Initialization,
},
}));

let audios = d.audio.map((v) => ({
BaseURL: v.baseUrl,
"@id": v.id,
"@mimeType": v.mimeType,
"@bandwidth": v.bandwidth,
"@codecs": v.codecs,
"@startWithSap": v.startWithSap,
SegmentBase: {
"@indexRange": v.SegmentBase.indexRange,
"@Initialization": v.SegmentBase.Initialization,
},
}));

// prettier-ignore
const root = create({ version: '1.0' })
.ele("urn:mpeg:dash:schema:mpd:2011","MPD",{
profiles: "urn:mpeg:dash:profile:isoff-on-demand:2011,http://dashif.org/guidelines/dash264",
type:"static",
minBufferTime: `PT${d.minBufferTime}S`,
mediaPresentationDuration: `PT${d.duration}S`
})
.ele("Period", {duration: `PT${d.duration}S`})
.ele("AdaptationSet",{ contentType:"video", bitstreamSwitching:true })
.ele({Representation:videos}).up()
.up()
.ele("AdaptationSet",{ contentType:"audio", bitstreamSwitching:true })
.ele({Representation:audios}).up()

const xml = root.end({ prettyPrint: true });
return xml;
};
33 changes: 33 additions & 0 deletions src/fake-bili/fetch-poster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import axios from "axios";

const fetchBiliPoster = async (
...args: [aid: number] | [bvid: string]
): Promise<string | null> => {
const [id] = args;

const api = new URL("http://api.bilibili.com/x/web-interface/view");
if (typeof id === "string") api.searchParams.append("bvid", id);
else api.searchParams.append("aid", "av" + id);

return axios
.get(api.toString(), {
headers: {
Origin: "https://www.bilibili.com",
Referer: "https://www.bilibili.com",
"Content-Length": "0",
},
})
.then((response) => {
const json = response.data;
if (json.code !== 0) throw new Error(`${json.code}: ${json.message}`);
else {
return (json.data.pic as string) ?? null;
}
})
.catch((e) => {
console.error(e);
return null;
});
};

export default fetchBiliPoster;
Loading

0 comments on commit 1420979

Please sign in to comment.