-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpvi.ts
131 lines (107 loc) · 3.33 KB
/
mpvi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { spawn } from 'child_process';
import ytcm from '@freetube/yt-comment-scraper';
import url from 'url';
interface Comment {
authorThumb: any[];
author: string;
authorId: string;
commentId: string;
text: string;
likes: string;
numReplies: number;
isOwner: boolean;
isHearted: boolean;
isPinned: boolean;
hasOwnerReplied: boolean;
time: string;
edited: boolean;
replyToken: string;
isVerified: boolean;
isOfficialArtist: boolean;
}
interface CommentObject {
comments: Comment[]
}
if (process.argv.length <= 2) {
throw "need more commands";
}
console.log('\n\n')
const youtubeUrl = process.argv[process.argv.length - 1];
const startMPV = (url: string) => {
const command = "mpv";
const args = [url];
const child = spawn(command, args);
child.stdout.on('data', (data: any) => {
if (process.argv.includes('--verbose')) {
console.log(`stdout: ${data}`);
}
});
child.stderr.on('data', (data: any) => {
if (process.argv.includes('--verbose')) {
console.error(`stderr: ${data}`);
}
});
child.on('close', (code: any) => {
// console.log(`child process exited with code ${code}`);
});
}
const getDescription = (url: string) => {
const command = "youtube-dl";
const args = ['--skip-download', '--get-description', url];
const child = spawn(command, args);
child.stdout.on('data', (data: any) => {
console.log('\n\n############### start description ###############')
console.log(`${data}`);
console.log('############### end description ###############\n\n')
});
child.stderr.on('data', (data: any) => {
console.error(`stderr: ${data}`);
});
child.on('close', (code: any) => {
// console.log(`child process exited with code ${code}`);
});
}
const printComments = (comments: CommentObject) => {
console.log('\n\n~~~~~~~~~~~~~~~start comments~~~~~~~~~~~~~~~')
comments.comments.forEach((comment, commentIndex) => {
console.log(`[${comment.likes}]${comment.isHearted ? '♥' : ''}${comment.isOwner ? ' (owner)' : ''}${comment.isPinned ? ' (pinned)' : ''}`)
console.log(`\t${comment.text}`)
// add a separator between every comment, but not at the end.
if (commentIndex !== (comments.comments.length - 1)) {
console.log('----------------------------')
}
});
console.log('~~~~~~~~~~~~~~~end comments~~~~~~~~~~~~~~~\n\n')
}
const getComments = (videoUrl: string) => {
// get the youtube video ID from the videoUrl.
// since videoUrl could be a full url or just the code, we parse
// the string as a url if it's over 11 characters
var id: string | undefined = videoUrl;
if (videoUrl.length !== 11) { // 11 is length of all youtube video ids
const parsedVideoId = url.parse(videoUrl, true).query?.v;
if (typeof parsedVideoId !== 'string') {
console.log("[Comment parser] Could not get video ID from input");
return;
} else {
id = parsedVideoId
}
}
// get the youtube comments using ytcm
if (typeof id === 'string') {
ytcm.getComments({ videoId: id }).then((data: any) => {
printComments(data)
}).catch((error: any) => {
console.error(error);
});
}
}
if (!process.argv.includes('--no-video')) {
startMPV(youtubeUrl);
}
if (!process.argv.includes('--no-desc')) {
getDescription(youtubeUrl);
}
if (!process.argv.includes('--no-comments')) {
getComments(youtubeUrl);
}