Skip to content

Commit

Permalink
docs(readme): update readme
Browse files Browse the repository at this point in the history
add new advance section and example
  • Loading branch information
nurrony committed Jan 7, 2024
1 parent e823133 commit a903b85
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
43 changes: 34 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ It is pretty straight forward

```sh
# using npm
npm install hlsdownloader
npm install --save hlsdownloader
# or with yarn
yarn add hlsdownloader
# or pnpm
Expand Down Expand Up @@ -75,26 +75,47 @@ const downloader = new HLSDownloader(options);
downloader.startDownload().then(response => console.log(response));
```

> ℹ️ Check [example.js](example.js) for working example
```js
//on success
// on success
{
"total": <number>,
message: 'Downloaded successfully',
total: <number>,
playlistURL: 'your playlist url'
message: 'Downloaded successfully',
}

//on partial download
// on partial download
{
message: 'Download done with some errors',
playlistURL: 'your playlist url',
total: <number>,
playlistURL: 'your playlist url',
message: 'Download done with some errors',
errors: [] // items url that is skipped or could not downloaded for error
}
```

## Advance Usage

TBD
HLSDownloader supports all [Ky API](https://github.com/sindresorhus/ky?tab=readme-ov-file#api) except these options given below

- uri
- url
- json
- form
- body
- method
- setHost
- isStream
- parseJson
- prefixUrl
- cookieJar
- playlistURL
- concurrency
- allowGetBody
- stringifyJson
- methodRewriting

It also disable retry failed request that you can easily override

## Run Tests

Expand Down Expand Up @@ -129,7 +150,11 @@ Contributions, issues and feature requests are welcome!<br />Feel free to check

## Show your support

Give a ⭐️ if this project helped you!
Give a ⭐️ if this project helped you!. I will be grateful if you all help me to improve this package by giving your suggestions, feature request and pull requests. I am all ears!!

## Special Thanks to

- [Ky Team](https://www.npmjs.com/package/ky)

## 📝 License

Expand Down
35 changes: 32 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import HLSDownloader from './build/index';

const downloader = new HLSDownloader({
// for fetching
let downloader = new HLSDownloader({
playlistURL: 'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8',
concurrency: 5,
destination: '/tmp/test',
});

const download = async () => downloader.startDownload();
console.log(await downloader.startDownload());

// download HLS resoruces
downloader = new HLSDownloader({
playlistURL: 'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8',
destination: '/tmp/tests',
});

// with 5 parallel download
downloader = new HLSDownloader({
concurrency: 5,
destination: '/tmp/tests',
playlistURL: 'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8',
});

// force overwrite
downloader = new HLSDownloader({
concurrency: 5,
overwrite: true,
destination: '/tmp/tests',
playlistURL: 'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8',
});

// passing ky option
downloader = new HLSDownloader({
concurrency: 5,
overwrite: true,
destination: '/tmp/tests',
playlistURL: 'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8',
retry: { limit: 0 },
});
9 changes: 8 additions & 1 deletion src/Downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import { URL } from 'url';
import { InvalidPlayList } from './exceptions';
import Utils from './utils';

/**
* HLS Playlist file extension
* @constant
* @type {string}
*/
const HLS_PLAYLIST_EXT = '.m3u8';

/**
* @class
* @memberof module:HLSDownloader
Expand Down Expand Up @@ -186,7 +193,7 @@ class Downloader {

let urls = this.parsePlaylist(url, playlistContent);
this.items = [...this.items, ...urls];
const playlists = urls.filter(url => url.toLowerCase().endsWith('.m3u8'));
const playlists = urls.filter(url => url.toLowerCase().endsWith(HLS_PLAYLIST_EXT));
const playlistContentPromiseResults = await Promise.allSettled(playlists.map(vUrl => this.fetchPlaylist(vUrl)));
const playlistContents = this.formatPlaylistContent(playlistContentPromiseResults);
urls = playlistContents.map(content => this.parsePlaylist(content?.url, content?.body)).flat();
Expand Down

0 comments on commit a903b85

Please sign in to comment.