Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Automatically Accept last.fm Friends
var page = require('webpage').create();
var credentials = [
{ username: 'username1', password: 'password1' },
{ username: 'username2', password: 'password2' }
// etc
];
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});
casper.start('https://www.last.fm');
var step=1;
var captureDir = 'output\\'
casper.clickWhileSelector = function(selector, username) {
return this.then(function() {
this.echo('checking selector: ' + selector);
if (this.exists(selector)) {
this.capture(captureDir + step++ + '-' + username + '-friendselector-before.png');
this.echo('found link: ' + this.getElementInfo(selector).tag);
this.click(selector);
this.wait(5000); // Wait for AJAX call to complete
this.capture(captureDir + step++ + '-' + username + '-friendselector-after.png');
return this.clickWhileSelector(selector, username);
}
else {
this.echo('could not find link for selector: ' + selector);
}
});
}
var addSteps = function(username, password) {
// Log in
casper.thenOpen('https://www.last.fm/login', function() {
console.log('Logging in ' + username);
this.capture(captureDir + step++ + '-' + username + '-login-before.png');
this.fill('form[action="/login"]', { username: username, password: password }, true);
this.capture(captureDir + step++ + '-' + username + '-login-after.png');
});
// Wait for login
casper.waitForUrl(/home$/, function() {
console.log('Waited for login of ' + username);
this.capture(captureDir + step++ + '-' + username + '-logged-in.png');
}, function() {}, 20000);
// Accept friends
casper.thenOpen('http://www.last.fm/inbox/friendrequests', function() {
console.log('Accepting friends for ' + username);
this.capture(captureDir + step++ + '-' + username + '-friendreqs-before.png');
})
casper.clickWhileSelector("form:not([action='/ajax/inbox/friendrequest']) input[name='accept']", username);
casper.then(function() {
console.log('Accepted friends for ' + username);
this.capture(captureDir + step++ + '-' + username + '-friendreqs-after.png');
})
// Log out
casper.then(function() {
console.log('Logging out ' + username);
this.capture(captureDir + step++ + '-' + username + '-logout-before.png');
this.fill('form[action="/login/logout"]', { }, true);
this.capture(captureDir + step++ + '-' + username + '-logout-after.png');
});
// Wait for logout
casper.waitForUrl("http://www.last.fm/", function() {
console.log('Waited for logout of ' + username);
this.capture(captureDir + step++ + '-' + username + '-logged-out.png');
}, function() {}, 20000);
}
for (var i=0; i < credentials.length; i++) {
addSteps(credentials[i].username, credentials[i].password);
}
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment