yt-cli


Logs | Files | README | README | LICENSE | GitLab


1
commit 2411c362649f3aa6d63d9bc532487ba27146d8ed
2
Author: Connor Etherington <[email protected]>
3
Date:   Tue May 9 01:27:24 2023 +0200
4
5
    Update.
6
---
7
 PKGBUILD                                 |  37 +++++++++++
8
 usr/bin/yt-mpv                           |   2 +
9
 usr/lib/node_modules/yt-cli/bin/yt-cli   | 111 +++++++++++++++++++++++++++++++
10
 usr/lib/node_modules/yt-cli/package.json |  20 ++++++
11
 usr/share/doc/yt-mpv/.gitkeep            |   0
12
 usr/share/licenses/yt-mpv/LICENSE        |  24 +++++++
13
 yt-cli.install                           |  15 +++++
14
 7 files changed, 209 insertions(+)
15
16
diff --git a/PKGBUILD b/PKGBUILD
17
new file mode 100644
18
index 0000000..8897d44
19
--- /dev/null
20
+++ b/PKGBUILD
21
@@ -0,0 +1,37 @@
22
+# Maintainer: Connor Etherington <[email protected]>
23
+# ---
24
+pkgname=yt-cli
25
+pkgver=0.1.0
26
+pkgrel=1
27
+pkgdesc="An easy to use CLI YouTube client"
28
+arch=('any')
29
+url="https://gitlab.com/a4to/${pkgname}"
30
+license=('MIT')
31
+depends=(mpv npm)
32
+install="yt-cli.install"
33
+source=("git+$url.git")
34
+sha512sums=("SKIP")
35
+
36
+
37
+build() {
38
+
39
+  [ -d "${srcdir}/${pkgname}/usr/share/${pkgname}" ] &&
40
+    cd "${srcdir}/${pkgname}/usr/share/${pkgname}" ||
41
+    cd "${srcdir}/usr/share/${pkgname}"
42
+
43
+  which yarn >/dev/null 2>&1 &&
44
+    yarn install || npm install
45
+
46
+}
47
+
48
+package() {
49
+  cd ${pkgname}
50
+
51
+  [ -d "${srcdir}/${pkgname}" ] && cd "${srcdir}/${pkgname}" || cd "${srcdir}"
52
+
53
+  install -dm0755 "${pkgdir}"/usr/lib/node_modules/${pkgname}
54
+  install -Dm0755 usr/bin/${pkgname} "${pkgdir}/usr/bin/${pkgname}"
55
+  install -Dm0644 usr/share/licenses/${pkgname}/LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
56
+  cp -r usr/lib/node_modules/${pkgname}/* "${pkgdir}"/usr/lib/node_modules/${pkgname}
57
+
58
+}
59
diff --git a/usr/bin/yt-mpv b/usr/bin/yt-mpv
60
new file mode 100755
61
index 0000000..d8df8ee
62
--- /dev/null
63
+++ b/usr/bin/yt-mpv
64
@@ -0,0 +1,2 @@
65
+#!/usr/bin/env node
66
+require('/usr/lib/node_modules/yt-cli/bin/yt-cli');
67
diff --git a/usr/lib/node_modules/yt-cli/bin/yt-cli b/usr/lib/node_modules/yt-cli/bin/yt-cli
68
new file mode 100755
69
index 0000000..4fcf640
70
--- /dev/null
71
+++ b/usr/lib/node_modules/yt-cli/bin/yt-cli
72
@@ -0,0 +1,111 @@
73
+#!/usr/bin/env node
74
+
75
+const fs = require('fs');
76
+const beautifier = require('html-formatter');
77
+const axios = require('axios');
78
+const prompts = require('prompts');
79
+
80
+const beautify = async (data) => {
81
+  const result = await beautifier.render(data);
82
+  return result;
83
+};
84
+
85
+const sh = (cmd) => {
86
+  const { execSync } = require('child_process');
87
+  try { execSync(cmd, { stdio: 'inherit' }) }
88
+  catch { /*console.log(e)*/ }
89
+};
90
+
91
+const vids=[];
92
+
93
+const mkVidList = async (data) => {
94
+  await JSON.parse(data).contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents.forEach(c => {
95
+    if(c.videoRenderer) vids.push({
96
+      title: c.videoRenderer.title.runs[0].text,
97
+      url: 'https://www.youtube.com/watch?v=' + c.videoRenderer.videoId
98
+    })
99
+  });
100
+  return vids;
101
+}
102
+
103
+const getData = async (searchTerm) => {
104
+  const sanitizedSearchTerm = searchTerm.replace(/ /g, '+');
105
+  const url = `https://www.youtube.com/results?search_query=${sanitizedSearchTerm}`;
106
+
107
+  let data = await axios.get(url).then(res => { return res.data });
108
+  data = await beautify(data);
109
+  data = data.match(/ytInitialData\s=\s(.*)/);
110
+  data = data.toString().replace('ytInitialData = ', '');
111
+  data = data.replace(/;<\/script>.*/, '');
112
+
113
+  const json = JSON.parse(JSON.stringify(data));
114
+  const vidList = mkVidList(json);
115
+  return vidList;
116
+}
117
+
118
+
119
+const main = async (search) => {
120
+
121
+  const response = search ? { searchTerm: search } : await prompts({
122
+    type: 'text',
123
+    name: 'searchTerm',
124
+    message: 'Search: '
125
+  });
126
+
127
+  if(!response.searchTerm) process.exit(0);
128
+
129
+  if(search) {
130
+    if(search.match(/^-h\s*?/) || search.match(/\s*?-h\s*?/) ||
131
+      search.match(/^--help\s*?/) || search.match(/\s*?--help\s*?/)) {
132
+
133
+      const sleep = function(ms) {
134
+        return new Promise(resolve => setTimeout(resolve, ms));
135
+      };
136
+
137
+      const exe = async (cmd, cb) => await require('child_process').exec( cmd, async (e, so, se) => cb(so));
138
+      const cl = (x) => console.log(x);
139
+
140
+      const c = `echo -e "\n  Usage: yt-cli [options] [search-term]\n\n    -a|--auto: auto play first result\n    -h|--help: display this message"`
141
+
142
+      await exe(c, cl);
143
+      await sleep(0.1);
144
+
145
+      process.exit(0);
146
+
147
+    } else if(search.match(/^-a\s*?/) || search.match(/\s*?-a\s*?/) ||
148
+      search.match(/^--auto\s*?/) || search.match(/\s*?--auto\s*?/)) {
149
+
150
+      response.searchTerm = search.replace(/^-a\s?/, '')
151
+        .replace(/\s?-a\s?$/, '') .replace(/\^--auto\s?/, '')
152
+        .replace(/\s?--auto\s?$/, '');
153
+
154
+      var auto = true;
155
+    } else {
156
+      var auto = false;
157
+    }
158
+  } else {
159
+    var auto = false;
160
+  }
161
+
162
+  const data = await getData(response.searchTerm);
163
+  const list = data.map((d, i) => {
164
+    return `${i + 1}. ${d.title}`;
165
+  });
166
+
167
+  const response2 = auto ? { video: 0 } : await prompts({
168
+    type: 'select',
169
+    name: 'video',
170
+    message: 'Select video: ',
171
+    choices: list,
172
+    initial: 0
173
+  });
174
+
175
+  const selectedVideo = data[response2.video];
176
+
177
+  sh(`mpv ${selectedVideo.url}`);
178
+  process.exit(0);
179
+
180
+};
181
+
182
+process.argv[2] ? main(process.argv.splice(2).join(' ')) : main();
183
+
184
diff --git a/usr/lib/node_modules/yt-cli/package.json b/usr/lib/node_modules/yt-cli/package.json
185
new file mode 100644
186
index 0000000..7a81477
187
--- /dev/null
188
+++ b/usr/lib/node_modules/yt-cli/package.json
189
@@ -0,0 +1,20 @@
190
+{
191
+  "name": "yt-cli",
192
+  "version": "0.1.0",
193
+  "description": "An easy to use CLI YouTube client",
194
+  "author": "Connor Etherington <[email protected]>",
195
+  "license": "MIT",
196
+  "main": "./bin/yt-cli",
197
+  "scripts": {
198
+    "start": "./bin/yt-cli"
199
+  },
200
+  "bin": {
201
+    "yt-cli": "./bin/yt-cli",
202
+    "ytcli": "./bin/yt-cli"
203
+  },
204
+  "dependencies": {
205
+    "axios": "^1.4.0",
206
+    "html-formatter": "^0.1.9",
207
+    "prompts": "^2.4.2"
208
+  }
209
+}
210
diff --git a/usr/share/doc/yt-mpv/.gitkeep b/usr/share/doc/yt-mpv/.gitkeep
211
new file mode 100644
212
index 0000000..e69de29
213
diff --git a/usr/share/licenses/yt-mpv/LICENSE b/usr/share/licenses/yt-mpv/LICENSE
214
new file mode 100644
215
index 0000000..af7d39d
216
--- /dev/null
217
+++ b/usr/share/licenses/yt-mpv/LICENSE
218
@@ -0,0 +1,24 @@
219
+MIT/X Consortium License
220
+
221
+Creator /Maintainer :
222
+© Connor Etherington <[email protected]>
223
+
224
+
225
+Permission is hereby granted, free of charge, to any person obtaining a copy
226
+of this software and associated documentation files (the "Software"), to deal
227
+in the Software without restriction, including without limitation the rights
228
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
229
+copies of the Software, and to permit persons to whom the Software is
230
+furnished to do so, subject to the following conditions:
231
+
232
+THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN ALL
233
+COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE.
234
+
235
+
236
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
238
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
239
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
240
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
241
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
242
+SOFTWARE.
243
diff --git a/yt-cli.install b/yt-cli.install
244
new file mode 100644
245
index 0000000..35a32f5
246
--- /dev/null
247
+++ b/yt-cli.install
248
@@ -0,0 +1,15 @@
249
+post_install(){
250
+  cd /usr/lib/node_modules/yt-cli
251
+  which yarn >/dev/null 2>&1 && yarn install || npm install
252
+  echo -e "[+] Thank you for using yt-cli!\n"
253
+}
254
+
255
+post_upgrade(){
256
+  cd /usr/lib/node_modules/yt-cli
257
+  which yarn >/dev/null 2>&1 && yarn install || npm install
258
+}
259
+
260
+pre_remove(){
261
+  rm -rf /usr/lib/node_modules/yt-cli
262
+}
263
+