Skip to content

Commit

Permalink
Add magnet URI and info hash support
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay-borzov committed Jan 9, 2019
1 parent 2703c0d commit 30862af
Show file tree
Hide file tree
Showing 4 changed files with 1,015 additions and 21 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ gets files' paths from `nature-pack.torrent` and compares them with files from `

## Arguments

`--torrent` (or `-t`) - Path to torrent file
`--torrent` (or `-t`) - Torrent id (as described in [webtorrent api](https://github.com/webtorrent/webtorrent/blob/master/docs/api.md#clientaddtorrentid-opts-function-ontorrent-torrent-))
- Magnet URI (e.g. `magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36`)
- Info Hash (e.g. `d2474e86c95b19b8bcfdb92bc12c9d44667cfa36`)
- http/https URL to a torrent file
- Filesystem path to a torrent file

`--dir` (or `-d`) - Path to directory with downloaded files

`--verbose` - Output all outdated filenames. By default only first 20 filenames are displayed

## Known bugs

- Torrent files with names containing unicode characters (e.g. 𝗚𝗪𝗔 1.txt) cannot be parsed correctly.

### Build with

- [parse-torrent](https://github.com/webtorrent/parse-torrent)
- [webtorrent](https://github.com/webtorrent/webtorrent)
- [minimist](https://github.com/substack/minimist)
- [enquirer](https://github.com/enquirer/enquirer)
- [recursive-readdir](https://github.com/jergason/recursive-readdir)
Expand Down
35 changes: 28 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const os = require('os')
const path = require('path')
const util = require('util')
const recursive = require('recursive-readdir')
const parseTorrent = util.promisify(require('parse-torrent').remote)
const unlink = util.promisify(fs.unlink)
const deleteEmpty = require('delete-empty')
const { Confirm } = require('enquirer')
const chalk = require('chalk')
const WebTorrent = require('webtorrent')
const memoryChunkStore = require('memory-chunk-store')

const infoLog = chalk.green
const errorLog = chalk.bgRed
Expand All @@ -18,12 +19,32 @@ const fileLog = chalk.grey
const IGNORE_GLOBS = ['~uTorrentPartFile*']
const FILES_LIST_LIMIT = 20

async function parseTorrentFile(torrentPath) {
return parseTorrent(torrentPath).catch(error => {
async function getTorrentMetadata(torrentId) {
return new Promise(resolve => {
parseTorrent(torrentId, resolve)
}).catch(error => {
console.log(errorLog('Unable to parse torrent file'), error)
})
}

function parseTorrent(torrentId, onDone) {
const client = new WebTorrent()

// Use memory-chunk-store to avoid creating directories inside tmp/webtorrent(https://github.com/webtorrent/webtorrent/issues/1562)
const torrent = client.add(torrentId, {
store: memoryChunkStore
})

torrent.on('metadata', function() {
onDone({
name: torrent.name,
files: torrent.files.map(file => file.path)
})

client.destroy()
})
}

async function deleteFiles(filenames) {
try {
const deletePromises = filenames.map(filename => unlink(filename))
Expand Down Expand Up @@ -73,13 +94,13 @@ if (!argv.torrent) {
return
}

const torrentPath = argv.torrent
const torrentId = argv.torrent
const directoryPath = path.resolve(argv.dir)
const verbose = argv.verbose

console.log(infoLog('Parsing torrent file...'))
Promise.all([
parseTorrentFile(torrentPath),
getTorrentMetadata(torrentId),
recursive(directoryPath, IGNORE_GLOBS)
]).then(async ([parseResult, dirFiles]) => {
if (!parseResult) {
Expand All @@ -89,10 +110,10 @@ Promise.all([
const { name, files } = parseResult

console.log(`Parsed ${chalk.bold(name)}.`, os.EOL)
const rootDir = `${name}${path.sep}`

const rootDir = `${name}${path.sep}`
const torrentFiles = files.map(file =>
path.join(directoryPath, file.path.replace(rootDir, ''))
path.join(directoryPath, file.replace(rootDir, ''))
)

const outdated = dirFiles.reduce((result, filename) => {
Expand Down
Loading

0 comments on commit 30862af

Please sign in to comment.