-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
463 additions
and
584 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{ "presets": ["es2015"] } | ||
{ "presets": ["es2015", "es2017"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import config from 'config'; | ||
import debug from 'debug'; | ||
import { request } from './utils'; | ||
import fetch from 'node-fetch'; | ||
|
||
const log = debug('connect'); | ||
|
||
export default function* getToken(username, password) { | ||
export default async function getToken(username, password) { | ||
log('connect to real-debrid.com'); | ||
|
||
const options = { | ||
const url = `${config.apiBaseUrl}/oauth/v2/token`; | ||
const res = await fetch(url, { | ||
method: 'POST', | ||
uri: `${config.apiBaseUrl}/oauth/v2/token`, | ||
form: { | ||
body: JSON.stringify({ | ||
username, | ||
password, | ||
client_id: config.clientId, | ||
grant_type: 'password', | ||
}, | ||
json: true, | ||
}; | ||
}), | ||
}); | ||
|
||
return (yield request(options)).access_token; | ||
return (await res.json()).access_token; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,93 @@ | ||
import rp from 'request-promise'; | ||
import config from 'config'; | ||
import debug from 'debug'; | ||
import fs from 'fs'; | ||
import ora from 'ora'; | ||
import sleep from 'co-sleep'; | ||
import { handleErrorMessage } from './utils'; | ||
import fetch from 'node-fetch'; | ||
|
||
const log = debug('torrent'); | ||
|
||
export function* getInfosTorrent(idTorrent, token) { | ||
export const getInfosTorrent = async (idTorrent, token) => { | ||
log(`get infos torrent ${idTorrent}`); | ||
|
||
const options = { | ||
uri: `${config.apiEndpoint}/torrents/info/${idTorrent}?auth_token=${token}`, | ||
json: true, | ||
}; | ||
|
||
let data; | ||
yield rp(options).then((body) => { | ||
data = body; | ||
}).catch(handleErrorMessage); | ||
const res = await fetch(`${config.apiEndpoint}/torrents/info/${idTorrent}?auth_token=${token}`); | ||
const data = await res.json(); | ||
|
||
return data; | ||
} | ||
}; | ||
|
||
export function* getTorrentList(token) { | ||
export const getTorrentList = async (token) => { | ||
log('get torrent list'); | ||
|
||
const options = { | ||
uri: `${config.apiEndpoint}/torrents?auth_token=${token}`, | ||
json: true, | ||
}; | ||
|
||
let data; | ||
yield rp(options).then((body) => { | ||
data = body; | ||
}).catch(handleErrorMessage); | ||
const res = await fetch(`${config.apiEndpoint}/torrents?auth_token=${token}`); | ||
const data = await res.json(); | ||
|
||
return data; | ||
} | ||
}; | ||
|
||
export function* selectFile(idTorrent, token, files = 'all') { | ||
export const selectFile = async (idTorrent, token, files = 'all') => { | ||
log(`select file ${idTorrent}`); | ||
|
||
const options = { | ||
await fetch(`${config.apiEndpoint}/torrents/selectFiles/${idTorrent}?auth_token=${token}`, { | ||
method: 'POST', | ||
uri: `${config.apiEndpoint}/torrents/selectFiles/${idTorrent}?auth_token=${token}`, | ||
json: true, | ||
form: { | ||
files, | ||
}, | ||
}; | ||
|
||
let data; | ||
yield rp(options).then((body) => { | ||
data = body; | ||
}).catch(handleErrorMessage); | ||
|
||
return data; | ||
} | ||
body: JSON.stringify({ files }), | ||
}); | ||
}; | ||
|
||
export function* addMagnet(magnet, token) { | ||
export const addMagnet = async (magnet, token) => { | ||
log(`add magnet ${magnet}`); | ||
|
||
const options = { | ||
const res = await fetch(`${config.apiEndpoint}/torrents/addMagnet?auth_token=${token}`, { | ||
method: 'POST', | ||
uri: `${config.apiEndpoint}/torrents/addMagnet?auth_token=${token}`, | ||
json: true, | ||
form: { | ||
body: JSON.stringify({ | ||
magnet: encodeURI(magnet), | ||
host: 'uptobox.com', | ||
}, | ||
}; | ||
}), | ||
}); | ||
|
||
let data; | ||
yield rp(options).then((body) => { | ||
data = body; | ||
}).catch(handleErrorMessage); | ||
const data = (await res.json()).id; | ||
|
||
return data.id; | ||
} | ||
return data; | ||
}; | ||
|
||
export function* addTorrent(torrent, token) { | ||
export const addTorrent = async (torrent, token) => { | ||
log(`add torrent ${torrent}`); | ||
|
||
const options = { | ||
uri: `${config.apiEndpoint}/torrents/addTorrent?auth_token=${token}`, | ||
json: true, | ||
}; | ||
|
||
let data; | ||
yield fs.createReadStream(torrent).pipe(rp.put(options)).then((body) => { | ||
data = body; | ||
const res = await fetch(`${config.apiEndpoint}/torrents/addTorrent?auth_token=${token}`, { | ||
method: 'PUT', | ||
body: await fs.createReadStream(torrent), | ||
}); | ||
|
||
return data.id; | ||
} | ||
const data = (await res.json()).id; | ||
|
||
return data; | ||
}; | ||
|
||
export function* convertTorrent(torrent, token) { | ||
export const convertTorrent = async (torrent, token) => { | ||
log(`convert torrent ${torrent}`); | ||
|
||
let idTorrent; | ||
if (torrent.match(/^magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{20,50}/i)) { | ||
idTorrent = yield addMagnet(torrent, token); | ||
idTorrent = await addMagnet(torrent, token); | ||
} else { | ||
idTorrent = yield addTorrent(torrent, token); | ||
idTorrent = await addTorrent(torrent, token); | ||
} | ||
yield selectFile(idTorrent, token); | ||
await selectFile(idTorrent, token); | ||
|
||
let link = []; | ||
let status = 'wait'; | ||
let progressConvert = 0; | ||
const spinner = ora(`Convert torrent progress: ${progressConvert}% (${status})`).start(); | ||
while (!link.length) { | ||
const infos = yield getInfosTorrent(idTorrent, token); | ||
const infos = await getInfosTorrent(idTorrent, token); // eslint-disable-line | ||
status = infos.status; | ||
link = infos.links; | ||
progressConvert = Number(infos.progress); | ||
spinner.text = `Convert torrent progress: ${progressConvert}% (${status})`; | ||
yield sleep(config.requestDelay); | ||
await sleep(config.requestDelay); // eslint-disable-line | ||
} | ||
spinner.stop(); | ||
|
||
console.log(`Convert finish: ${link.toString()}`); | ||
return link.toString(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import config from 'config'; | ||
import debug from 'debug'; | ||
import { request } from './utils'; | ||
import fetch from 'node-fetch'; | ||
|
||
const log = debug('unrestrict'); | ||
|
||
export default function* unrestrict(link, token) { | ||
export default async (link, token) => { | ||
log(`unrestrict link ${link}`); | ||
|
||
const options = { | ||
const url = `${config.apiEndpoint}/unrestrict/link?auth_token=${token}`; | ||
const res = await fetch(url, { | ||
method: 'POST', | ||
uri: `${config.apiEndpoint}/unrestrict/link?auth_token=${token}`, | ||
form: { | ||
body: JSON.stringify({ | ||
link, | ||
}, | ||
json: true, | ||
}; | ||
}), | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
|
||
return (yield request(options)).download; | ||
} | ||
return (await res.json()).download; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.