Skip to content

Commit

Permalink
API is now working!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Buck committed Nov 16, 2016
1 parent f7b87c2 commit 4c0937a
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 234 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ coverage
.node_repl_history

.vscode
dist
dist
output
test
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pully",
"version": "2.0.0-alpha",
"version": "2.0.0-beta",
"description": "A simple CLI for downloading high quality YouTube videos!",
"main": "index.js",
"scripts": {
Expand Down
45 changes: 0 additions & 45 deletions src/debug.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Pully, Presets } from './index';

//const testVideo = 'https://www.youtube.com/watch?v=HQCZRm8QlPE'; // 22s with music (9MB)
const testVideo = 'https://www.youtube.com/watch?v=2PuFyjAs7JA'; // 11s with silence (2.5MB)

let p = new Pully();

p.download({
url: testVideo,
dir: './output',
template: '${author} -__- ${title}',
preset: Presets.HD,
verify: (format) => {
console.log('Verify: ' + format.info.downloadSize);

// Limit download to ~3MB...
return format.info.downloadSize < 3000000;
},
progress: (data) => {
if (data.indeterminate) {
console.log(`[${new Date().toUTCString()}] Working...`);
} else {
console.log(`Progress: ${data.percent}%`);
}
}
}).then((results) => {
console.log(`Download Complete: "${results.path}"`);
process.exit(0);
}, err => {
console.error('Uh oh!', err);
process.exit(1);
});
72 changes: 55 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,78 @@ import { parse, Url } from 'url';
import { Readable } from 'stream';
import { EventEmitter } from 'events';
import { createWriteStream } from 'fs';
import { resolve } from 'path';

import { PullyOptions, DownloadOptions, DownloadResults, MediaInfo, FormatInfo, ProgressData } from './lib/models';
import { Context } from './lib/context';
import { Presets } from './lib/presets';
const template = require('lodash.template');

import { Lookup, Preset, PullyConfiguration, DownloadOptions, DownloadResults, MediaInfo, FormatInfo, ProgressData } from './lib/models';
import { Download } from './lib/download';
import { Presets, DefaultPresets } from './lib/presets';

const DEFAULT_TEMPLATE = '${author}/${title}';

export class Pully {

private _ctx: Context;
private _presets: Lookup<Preset> = {};

constructor(private _options?: PullyOptions) {
this._options = this._options || {};
this._ctx = new Context(this._options.presets);
constructor(private _config?: PullyConfiguration) {
this._config = this._config || {};
this._registerPresets(DefaultPresets)._registerPresets(this._config.additionalPresets);
}

public download(url: string, preset?: string): Promise<DownloadResults>;
public download(options: DownloadOptions): Promise<DownloadResults>;
public download(options: string|DownloadOptions, preset?: string): Promise<DownloadResults> {
if (typeof options === 'string') {
options = {
url: options,
public download(options: { url?: string, preset?: string, dir?: string, template?: string, verify?: (info: FormatInfo) => boolean | Promise<boolean>, progress?: (data: ProgressData) => void }): Promise<DownloadResults>;
public download(input: any, preset?: string): Promise<DownloadResults> {
if (typeof input === 'string') {
input = {
url: input,
preset
} as DownloadOptions;
};
}

this._validateUrl(options.url);
this._validateUrl(input.url);

options.preset = options.preset || this._options.defaultPreset || Presets.HD;
let specifiedDir = input.dir || this._config.dir;

return this._ctx.fetch(options);
const options: DownloadOptions = {
url: input.url,
preset: input.preset || this._config.preset || Presets.HD,
dir: specifiedDir ? resolve(specifiedDir) : null,
template: template(input.template || DEFAULT_TEMPLATE),
verify: input.verify || this._config.verify || (() => true),
progress: input.progress
};

return this._beginDownload(options);
}

private _validateUrl(url: string): void {
// TODO: Check to make sure it is a valid URL...
}

private _registerPresets(presets: Array<Preset> | Lookup<Preset>): this {
if (!presets) {
return this;
}

if (Array.isArray(presets)) {
presets.forEach(preset => this._presets[preset.name] = preset);
} else {
Object.assign(this._presets, presets);
}

return this;
}

private _beginDownload(options: DownloadOptions): Promise<DownloadResults> {
let preset = this._presets[options.preset];

if (!preset) {
throw new Error(`NO PRESET "${options.preset}"!`);
}

return Download.initiate(options, preset);
}
}

export { PullyOptions, Presets, ProgressData };
export { PullyConfiguration, Presets, ProgressData };
41 changes: 0 additions & 41 deletions src/lib/context.ts

This file was deleted.

0 comments on commit 4c0937a

Please sign in to comment.