Skip to content

Commit

Permalink
feat: cli
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed May 13, 2024
1 parent 1f1eb2b commit 7724645
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-shrimps-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"baha-anime-dl-cli": patch
---

cli tool
10 changes: 10 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Bahamut Anime Downloader CLI

```
baha-anime-dl <sn> [options]
```

## Options

- `-o, --output <path>`: Output path
- `-c, --cookie <cookie>`: Cookie string `KEY=VALUE`
35 changes: 35 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "baha-anime-dl-cli",
"description": "This tool is used to download low quality Anime from Bahamut.",
"version": "0.0.0",
"author": "JacobLinCool <jacoblincool@gmail.com> (https://jacoblin.cool)",
"license": "AGPL-3.0",
"keywords": [],
"bin": {
"baha-anime-dl": "dist/index.js"
},
"files": [
"dist"
],
"scripts": {
"build": "tsup"
},
"dependencies": {
"baha-anime-dl": "workspace:*",
"baha-anime-dl-ext": "workspace:*",
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/yargs": "^17.0.32",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"repository": {
"type": "git",
"url": "https://github.com/JacobLinCool/baha-anime-dl.git"
},
"bugs": {
"url": "https://github.com/JacobLinCool/baha-anime-dl/issues"
},
"homepage": "https://jacoblincool.github.io/baha-anime-dl"
}
61 changes: 61 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Downloader, default_config } from "baha-anime-dl";
import { build, merge } from "baha-anime-dl-ext";
import fs from "node:fs";
import path from "node:path";
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";

main();

async function main() {
const argv = await yargs(hideBin(process.argv))
.command("<sn>", "Download media with the given serial number or ID", (yargs) => {
yargs.positional("sn", {
type: "string",
describe: "The serial number or ID of the media to download",
});
})
.option("output", {
alias: "o",
type: "string",
describe: "Output path for the downloaded media",
})
.option("cookie", {
alias: "c",
type: "array",
describe: "Cookie to be set. This will be set to the cookie jar at the beginning.",
})
.demandCommand(1).argv;

const sn = parseInt(argv._[0].toString());
if (isNaN(sn)) {
console.error("Invalid serial number");
process.exit(1);
}

const output = argv.output || `${sn}.mp4`;

console.log(`Downloading media with serial number ${sn} to ${output}`);

const config = {
...default_config(),
fetcher: build({
headers: {
"User-Agent": "Mozilla/5.0 Baha LowRes Anime Downloader",
},
cookies: argv.cookie
? Object.fromEntries(argv.cookie.map((c) => c.toString().split("=", 2)))
: {},
}),
};

const downloader = new Downloader(config);
const download = downloader.download(sn);
const merged = await merge(download);

const dir = path.dirname(output);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(output, Buffer.from(merged));
}
2 changes: 2 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import "./cli";
8 changes: 8 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
12 changes: 12 additions & 0 deletions packages/cli/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from "tsup";

export default defineConfig(() => ({
entry: ["src/index.ts"],
outDir: "dist",
target: "node16",
format: ["esm", "cjs"],
shims: true,
clean: true,
splitting: false,
dts: true,
}));
4 changes: 4 additions & 0 deletions packages/cli/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"includeVersion": true,
"entryPoints": ["src/index.ts"]
}

0 comments on commit 7724645

Please sign in to comment.