Skip to content

Commit

Permalink
Fix await/async
Browse files Browse the repository at this point in the history
  • Loading branch information
johackim committed Mar 25, 2017
1 parent c7c65b1 commit fb84bdc
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 83 deletions.
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ build: ## Build with babel
run: ## Run with babel
@ ./node_modules/.bin/babel-node src/rdcli.js $(filter-out $@,$(MAKECMDGOALS))

dev: ## Run with babel
@ NODE_ENV=dev ./node_modules/.bin/babel-node src/rdcli.js $(filter-out $@,$(MAKECMDGOALS))

debug: ## Run with babel (with debug)
@ DEBUG=torrent,download,connect,unrestrict ./node_modules/.bin/babel-node src/rdcli.js $(filter-out $@,$(MAKECMDGOALS))

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"babel-polyfill": "^6.23.0",
"chai": "^3.5.0",
"chalk": "^1.1.3",
"co": "^4.6.0",
"co-prompt": "^1.0.0",
"co-sleep": "0.0.1",
"commander": "^2.9.0",
Expand Down
17 changes: 9 additions & 8 deletions src/connect.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import config from 'config';
import debug from 'debug';
import fetch from 'node-fetch';
import fetch from './fetch';

const log = debug('connect');

export default async function getToken(username, password) {
const getToken = async (username, password) => {
log('connect to real-debrid.com');

const url = `${config.apiBaseUrl}/oauth/v2/token`;
const res = await fetch(url, {
const data = await fetch(`${config.apiBaseUrl}/oauth/v2/token`, {
method: 'POST',
body: JSON.stringify({
body: {
username,
password,
client_id: config.clientId,
grant_type: 'password',
}),
},
});

return (await res.json()).access_token;
}
return data.access_token;
};

export default getToken;
23 changes: 23 additions & 0 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fetch from 'node-fetch';
import querystring from 'querystring';
import handleErrorMessage from './utils';

export default async function (url, opts = { method: 'GET' }) {
const res = await fetch(url, {
method: opts.method,
body: querystring.stringify(opts.body),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
});

let data = await res.text();

if (!data) return null;

data = JSON.parse(data);

if (res.status !== 200) {
return handleErrorMessage(data);
}

return data;
}
20 changes: 9 additions & 11 deletions src/rdcli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'babel-polyfill';
import program from 'commander';
import prompt from 'co-prompt';
import ora from 'ora';
import co from 'co';
import chalk from 'chalk';
import fs from 'fs';
import { download, waitDuringScan } from './download';
Expand All @@ -17,26 +16,25 @@ program
.version(pjson.version)
.description('Download links, magnets and torrent files.')
.usage('<url|magnet|torrent>')
.action(arg => co(function* action() {
.action(async (arg) => {
try {
const username = process.env.REALDEBRID_USERNAME || (yield prompt('Username: '));
const password = process.env.REALDEBRID_PASSWORD || (yield prompt.password('Password: '));
const token = yield getToken(username, password);
const username = process.env.REALDEBRID_USERNAME || (await prompt('Username: '));
const password = process.env.REALDEBRID_PASSWORD || (await prompt.password('Password: '));
const token = await getToken(username, password);

let link;
if (arg.match(/^(https?:\/\/)([\da-z.-]+).([a-z.]{2,6})([/\w? .-]*)*\/?$/)) {
link = arg;
} else if (arg.match(/^magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{20,50}/i)
|| fs.existsSync(arg)) {
link = yield convertTorrent(arg, token);
} else if (arg.match(/^magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{20,50}/i) || fs.existsSync(arg)) {
link = await convertTorrent(arg, token);
} else {
console.log('Usage: rdcli <url|magnet|torrent>');
process.exit();
}

const unrestrictLink = yield unrestrict(link, token);
const unrestrictLink = await unrestrict(link, token);
console.log(`Start download : ${unrestrictLink}`);
yield waitDuringScan(link);
await waitDuringScan(link);

const spinner = ora('Download: 0.0% Speed: 0Mbps').start();
download(unrestrictLink, (res) => {
Expand All @@ -54,7 +52,7 @@ program
console.error(`\n${chalk.red(e)}`);
process.exit();
}
}))
})
.parse(process.argv);

if (!process.argv.slice(2).length) {
Expand Down
71 changes: 24 additions & 47 deletions src/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,56 @@ import debug from 'debug';
import fs from 'fs';
import ora from 'ora';
import sleep from 'co-sleep';
import fetch from 'node-fetch';
import handleErrorMessage from './utils';
import fetch from './fetch';

const log = debug('torrent');

export const getInfosTorrent = async (idTorrent, token) => {
log(`get infos torrent ${idTorrent}`);

try {
const res = await fetch(`${config.apiEndpoint}/torrents/info/${idTorrent}?auth_token=${token}`);
const data = await res.json();
return data;
} catch (e) {
return handleErrorMessage(e);
}
const data = await fetch(`${config.apiEndpoint}/torrents/info/${idTorrent}?auth_token=${token}`);
return data;
};

export const getTorrentList = async (token) => {
log('get torrent list');

try {
const res = await fetch(`${config.apiEndpoint}/torrents?auth_token=${token}`);
const data = await res.json();
return data;
} catch (e) {
return handleErrorMessage(e);
}
const data = await fetch(`${config.apiEndpoint}/torrents?auth_token=${token}`);
return data;
};

export const selectFile = async (idTorrent, token, files = 'all') => {
log(`select file ${idTorrent}`);

try {
await fetch(`${config.apiEndpoint}/torrents/selectFiles/${idTorrent}?auth_token=${token}`, {
method: 'POST',
body: JSON.stringify({ files }),
});
} catch (e) {
handleErrorMessage(e);
}
await fetch(`${config.apiEndpoint}/torrents/selectFiles/${idTorrent}?auth_token=${token}`, {
method: 'POST',
body: { files },
});
};

export const addMagnet = async (magnet, token) => {
log(`add magnet ${magnet}`);

try {
const res = await fetch(`${config.apiEndpoint}/torrents/addMagnet?auth_token=${token}`, {
method: 'POST',
body: JSON.stringify({
magnet: encodeURI(magnet),
host: 'uptobox.com',
}),
});
const data = (await res.json()).id;
return data;
} catch (e) {
return handleErrorMessage(e);
}
const data = await fetch(`${config.apiEndpoint}/torrents/addMagnet?auth_token=${token}`, {
method: 'POST',
body: {
magnet: encodeURI(magnet),
host: 'uptobox.com',
},
});

return data.id;
};

export const addTorrent = async (torrent, token) => {
log(`add torrent ${torrent}`);

try {
const res = await fetch(`${config.apiEndpoint}/torrents/addTorrent?auth_token=${token}`, {
method: 'PUT',
body: await fs.createReadStream(torrent),
});
const data = (await res.json()).id;
return data;
} catch (e) {
return handleErrorMessage(e);
}
const data = await fetch(`${config.apiEndpoint}/torrents/addTorrent?auth_token=${token}`, {
method: 'PUT',
body: await fs.createReadStream(torrent),
});

return data.id;
};

export const convertTorrent = async (torrent, token) => {
Expand Down
22 changes: 7 additions & 15 deletions src/unrestrict.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import config from 'config';
import debug from 'debug';
import fetch from 'node-fetch';
import handleErrorMessage from './utils';
import fetch from './fetch';

const log = debug('unrestrict');

export default async (link, token) => {
log(`unrestrict link ${link}`);

try {
const url = `${config.apiEndpoint}/unrestrict/link?auth_token=${token}`;
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({
link,
}),
headers: { 'Content-Type': 'application/json' },
});
return (await res.json()).download;
} catch (e) {
return handleErrorMessage(e);
}
const data = await fetch(`${config.apiEndpoint}/unrestrict/link?auth_token=${token}`, {
method: 'POST',
body: { link },
});

return data.download;
};
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function handleErrorMessage(error) {
const errorCode = error.error.error_code;
const errorCode = error.error_code;

switch (errorCode) {
case 1:
Expand Down

0 comments on commit fb84bdc

Please sign in to comment.