Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Last active August 29, 2015 14:13
Show Gist options
  • Save DanielFGray/7cff3de64f2e839adde5 to your computer and use it in GitHub Desktop.
Save DanielFGray/7cff3de64f2e839adde5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict';
// todo:
// ignore user's snatched list
// ask user to confirm/change list
// download .torrents
var whatcd = require('whatcd');
var prompt = require('prompt');
var fs = require('fs');
var http = require('http');
var argv = require('minimist')(process.argv.slice(2), {
alias: {
h: 'help',
V: 'version',
v: 'verbose',
e: 'encoding',
u: 'username',
p: 'password'
}
});
if(argv.help) {
console.log(' Usage: snatch [options] [tags]');
console.log(' Options:');
console.log(' ');
console.log(' -h, --help print this help');
console.log(' -V, --version print version number');
console.log(' -v, --verbose enable verbose output');
console.log(' -e, --encoding specify an encoding of the following:');
console.log(' FLAC, 320, v0, V1, V2');
console.log(' -u, --username set your whatcd username');
console.log(' -p, --password set your whatcd password (I don\'t advise this)');
process.exit(0);
}
if(argv.version) {
console.log('wcdtopscraper 0.1');
process.exit(0);
}
var globals = {
client: null,
encoding: null,
grabbed: [],
results: []
};
var grab = function(tags, order) {
globals.client.browse({
taglist: tags,
tags_type: 0,
order_by: order,
order_way: 'desc',
group_results: 1,
encoding: globals.encoding
}, function(err, data) {
var rows = data.results;
if(argv.verbose) console.log('grabbed top '+order);
rows.forEach(function(e, i) {
globals.results.push(e);
});
globals.grabbed.push(order);
parse();
});
};
var uniq = function(array) {
if(argv.verbose) console.log('removing duplicates');
var foundIds = {};
return array.filter(function(e) {
if(foundIds[e.groupId]) {
return false;
}
foundIds[e.groupId] = true;
return true;
});
};
var parse = function() {
if(globals.grabbed.length === 2) {
if(argv.verbose) console.log('parsing data');
var file = fs.createWriteStream('torrents.txt');
uniq(globals.results).forEach(function(e, i) {
e.torrents.forEach(function(e) {
if(e.hasSnatched === false) {
var url = 'https://what.cd/torrents.php?action=download&id=';
file.write(url + e.torrentId + '\n');
}
//console.log(e);
});
//process.stderr.write(e.artist + ' - ' + e.groupName + ' (' + e.torrents.length + ')' + '\n');
});
file.end();
}
};
var main = function(userprompt) {
var tags = argv._.join(',');
globals.client = new whatcd('https://what.cd', userprompt.username, userprompt.password);
globals.client.login(function() {
console.log(globals.client.logged_in);
if(! globals.client.logged_in) {
console.log('invalid credentials');
process.exit(0);
}
});
globals.encoding = userprompt.encoding;
grab(tags, 'seeded');
grab(tags, 'snatched');
};
prompt.message = '';
prompt.delimiter = '';
prompt.override = argv;
prompt.start();
prompt.get([{
name: 'username',
type: 'string',
description: 'Username:',
required: true
}, {
name: 'password',
type: 'string',
description: 'Password:',
required: true,
hidden: true,
}, {
name: 'encoding',
type: 'string',
description: 'Encoding:',
pattern: /^(v[02]|320|flac)$/i,
default: 'FLAC',
message: 'Encoding must be FLAC, 320, V0 or V2',
required: true
}], function(err, userprompt) {
main(userprompt);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment