Skip to content

Commit

Permalink
✨ Can watch a file with list of user
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoax017 committed Oct 19, 2024
1 parent fb28635 commit 0bc1c52
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

- name: Setup Deno
# uses: denoland/setup-deno@v1
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2
with:
deno-version: v1.x

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.vscode/
.idea
lives/
lives.list
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ deno install --allow-net --allow-write -n tlr https://raw.githubusercontent.com/
OPTIONS:
-u, --user USER
Record a livestream from the username
-w, --watch "USER1 USER2 ..."
-w, --watch "USER1 USER2 ...", -i, --input INPUT_FILE (one username per line)
Automatic live recording when a user from the provided list is in live
-o, --output OUTPUT
Output directory
Expand All @@ -43,3 +43,9 @@ Monitor users and start recording when they start streaming
```
tlr -w "username1 username2"
```

Monitor users from a file and start recording when they start streaming

```
tlr -i lives.list
```
2 changes: 2 additions & 0 deletions lives.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
username1
username2
49 changes: 43 additions & 6 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ if (import.meta.main) {
validateArgs(args, parsedArgs);

const user = parsedArgs.u || parsedArgs.user;
const users = parsedArgs.w || parsedArgs.watch;
const inputFile = parsedArgs.i || parsedArgs.input;
const rawUsers = parsedArgs.w || parsedArgs.watch;
const output = parsedArgs.o || parsedArgs.output;
if (user && users) {
let users: string[] = [];

if (user && rawUsers) {
usage(`"-u" and "-w" should not be used at the same time`);
Deno.exit();
}

// parse user file
if (typeof inputFile === "string") {
try {
const rawFileContent = new TextDecoder().decode(
await Deno.readFile(inputFile),
);
users = rawFileContent.split("\n").filter((e) => e && e.charAt(0) !== "#")
.map((e) => e.trim());
} catch (e) {
console.error(`could not read input file: ${inputFile}`, e);
Deno.exit();
}
}

if (output && typeof output !== "string") {
usage("-o: output dir is not provided");
Deno.exit();
Expand All @@ -31,12 +49,20 @@ if (import.meta.main) {
await recordUser(user, recording, output);
}

if (users) {
if (typeof users !== "string") {
if (rawUsers) {
if (typeof rawUsers === "string") {
users = rawUsers.split(" ");
} else {
usage("-w: user list is not provided");
}
}

if (users) {
if (!Array.isArray(users) || !users.length) {
usage("user list is empty");
Deno.exit();
}
await watchUsers(users, recording, output);
watchUsers(users, recording, output);
}
}

Expand All @@ -48,7 +74,18 @@ function validateArgs(
_: (string | number)[];
},
) {
const allowedOpts = ["h", "help", "u", "user", "w", "watch", "o", "output"];
const allowedOpts = [
"h",
"help",
"u",
"user",
"w",
"watch",
"o",
"output",
"i",
"input",
];

if (args.length == 0) {
usage();
Expand Down
4 changes: 3 additions & 1 deletion src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default function usage(error?: string) {
console.info("OPTIONS:");
console.info(" -u, --user USER");
console.info(` Record a livestream from the username`);
console.info(' -w, --watch "USER1 USER2 ..."');
console.info(
' -w, --watch "USER1 USER2 ...", -i, --input INPUT_FILE (one username per line)',
);
console.info(
` Automatic live recording when a user from the provided list is in live`,
);
Expand Down
3 changes: 1 addition & 2 deletions src/watchUsers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import recordUser from "./recordUser.ts";

export default function watchUsers(
usersString: string,
users: string[],
recording: Recording,
output?: string,
) {
const watchUsersIter = () => {
console.log("Checking users...");
const users = usersString.split(" ");

const checkUser = async (user: string) => {
console.log(`Checking ${user}`);
Expand Down

0 comments on commit 0bc1c52

Please sign in to comment.