Skip to content

Commit 4b5e725

Browse files
Copilotmarkets
andauthored
Add YouTube video duration filtering with --duration option (#20)
Co-authored-by: Marc Anguera Insa <srmarc.ai@gmail.com>
1 parent 3713d6e commit 4b5e725

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The CLI allows you to search videos without leaving the terminal:
1919

2020
> suchtube funny cats
2121
> suchtube football top goals --random --open
22+
> suchtube trending videos --duration=short
23+
> suchtube documentary --duration=long --random
2224

2325
Or start the server:
2426

@@ -55,19 +57,23 @@ The server listens by default on port 3333, if you want to change this, you can
5557

5658
Options while using the CLI are available in the following formats: `--time=10` or `--time 10`. For the server, you should pass the options along with the query, inside the `q` paramater, ie: `?q=funny+cats+--time=10`.
5759

58-
- `--time=10`, `-t=10`
60+
#### `--time=10`, `-t=10`
5961

6062
Starts the video at the given time in seconds.
6163

62-
- `--random`, `-r`
64+
#### `--random`, `-r`
6365

6466
Returns a random video taking into account the given topic.
6567

66-
- `--open`, `-o` *(CLI only)*
68+
#### `--duration=short`, `-d=short`
69+
70+
Filters videos by duration. Available values: `any` (default), `short`, `medium`, `long`. Uses YouTube's videoDuration parameter to filter results directly from the API.
71+
72+
#### `--open`, `-o` *(CLI only)*
6773

6874
Opens the video in your browser.
6975

70-
- `--full`, `-f` *(CLI only)*
76+
#### `--full`, `-f` *(CLI only)*
7177

7278
Displays full video's information. It corresponds to hit `GET /search.json?q=` against the server.
7379

@@ -78,7 +84,7 @@ You can use the SuchTube search as a library:
7884
```js
7985
import { search } from 'suchtube'
8086

81-
search('funny cats', { random: true }).then(video => {
87+
search('funny cats', { random: true, duration: 'short' }).then(video => {
8288
console.log(video.title)
8389
console.log(video.link)
8490
console.log(video.publishedAt)

src/cli.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const yargsInstance = yargs(hideBin(process.argv))
1010
.example('suchtube funny cats')
1111
.example('suchtube football top goals --open')
1212
.example('suchtube top summer songs --random')
13+
.example('suchtube trending videos --duration=short')
1314
.example('suchtube --server')
1415
.alias('version', 'v')
1516
.alias('help', 'h')
@@ -25,6 +26,11 @@ const yargsInstance = yargs(hideBin(process.argv))
2526
description: 'Open the video in your browser',
2627
alias: 'o'
2728
})
29+
.option('duration', {
30+
description: 'Filter videos by duration',
31+
alias: 'd',
32+
choices: ['any', 'short', 'medium', 'long']
33+
})
2834
.option('full', {
2935
description: 'Display full information',
3036
alias: 'f'

src/search.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const youtubeAPI = async (query, options) => {
88
part: 'snippet'
99
}
1010

11+
if (options.duration && options.duration !== 'any') {
12+
params.videoDuration = options.duration
13+
params.type = 'video'
14+
}
15+
1116
if (!params.key || params.key == "") {
1217
console.error('Whoops! You should setup your YouTube Data API key')
1318
}

0 commit comments

Comments
 (0)