Skip to content

Commit

Permalink
feat: able to configure random-http-useragent
Browse files Browse the repository at this point in the history
BREAKING CHANGE: bin parameters simplified + instance config options changed
  • Loading branch information
hfreire committed Oct 30, 2017
1 parent 21f1030 commit 89c176b
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 204 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '6.11.0'
- 6.11.3
cache:
directories:
- node_modules
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ npm install pollmommy -g
### How to use

#### Fetch the required parameters
Parameter | How to get it
:---:|:---:
`URL` | The poll's website URL, Polldaddy's poll website or the embedded poll website.
`Poll id` | The Polldaddy's poll identifier, inspect the website HTML code and search for this pattern PDI_container`NUMBER` - `NUMBER` will be the id.
`Poll option id` | The Polldaddy's poll option identifier, pick the desired option and inspect the website HTML code and search for this pattern PDI_answer`NUMBER` - `NUMBER` will be the id.
Parameter | Description | How to get it
:---:|:---:|:---:
Poll URL | The poll's website URL. | Polldaddy's poll website or the embedded poll website.
Poll id | The Polldaddy's poll identifier. | Inspect the website HTML code and search for this pattern PDI_container`NUMBER` - `NUMBER` will be the id.
Poll option id | The Polldaddy's poll option identifier. | Pick the desired option and inspect the website HTML code and search for this pattern PDI_answer`NUMBER` - `NUMBER` will be the id.


#### Use it in your terminal
```
pollmommy http://bbc.co.uk/should-trump-be-fired.html 324345 12939
pollmommy -u http://bbc.co.uk/should-trump-be-fired.html -i 324345 -o 12939
```

#### Use it in your app
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@
"prepublish": "npm run compile",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"semantic-release": "./node_modules/.bin/semantic-release pre && npm publish && node_modules/.bin/semantic-release post"
},
"release": {
"analyzeCommits": "simple-commit-message"
}
}
30 changes: 16 additions & 14 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@
* LICENSE file in the root directory of this source tree.
*/

process.on('uncaughtException', error => {
process.on('uncaughtException', (error) => {
console.error(error)

process.exit(1)
})

process.on('unhandledRejection', error => {
process.on('unhandledRejection', (error) => {
console.error(error)

process.exit(1)
})

const program = require('commander')

let pollUrl
let pollId
let pollOptionId
const { join } = require('path')

const { version } = require(join(__dirname, '../package'))

program
.version('1.0.0')
.arguments('<pollUrl> <pollId> <pollOptionId>')
.action((_pollUrl, _pollId, _pollOptionId) => {
pollUrl = _pollUrl
pollId = _pollId
pollOptionId = _pollOptionId
})
.version(version)
.option('-u, --poll-url <pollUrl>', 'The poll\'s website URL, Polldaddy\'s poll website or the embedded poll website.')
.option('-i, --poll-id <pollId>', 'The Polldaddy\'s poll identifier')
.option('-o, --poll-option-id <pollOptionId>', 'The Polldaddy\'s poll option identifier')
.parse(process.argv)

const { pollUrl, pollId, pollOptionId } = program

if (!pollUrl || !pollId || !pollOptionId) {
program.outputHelp()

Expand All @@ -44,7 +43,10 @@ if (!pollUrl || !pollId || !pollOptionId) {
const Pollmommy = require('../lib/pollmommy')
const pollmommy = new Pollmommy()

// noinspection JSUnusedAssignment
pollmommy.vote(pollUrl, pollId, pollOptionId)
.then(() => console.log('Voted successfully!'))
.catch((error) => console.error(error))
.catch((error) => {
console.error(`Error: ${error.message}`)

process.exit(1)
})
144 changes: 32 additions & 112 deletions src/pollmommy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable no-undef,no-useless-escape */

/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
Expand All @@ -11,131 +9,53 @@ const _ = require('lodash')
const Promise = require('bluebird')

const Nightmare = require('nightmare')
Nightmare.Promise = require('bluebird')
Nightmare.Promise = Promise

const RandomHttpUserAgent = require('random-http-useragent')

const path = require('path')
const { join } = require('path')

const jqueryPath = path.join(__dirname, '../share/jquery/jquery-3.1.0.min.js')
const jqueryPath = join(__dirname, '../share/jquery/jquery-3.1.0.min.js')

function vote (pollUrl, pollId, pollOptionId, evaluate) {
return RandomHttpUserAgent.get()
.then((userAgent) => {
const nightmare = Nightmare(this.options)

return nightmare
.useragent(userAgent)
.goto(pollUrl)
.inject('js', jqueryPath)
.evaluate(evaluate, pollId, pollOptionId)
.end()
.then()
})
}
const vote = require('./vote')

const defaultOptions = {
show: false,
webPreferences: {
webSecurity: false
}
nightmare: {
show: false,
webPreferences: {
webSecurity: false
}
},
'random-http-useragent': {}
}

class Pollmommy {
constructor (options = {}) {
this.options = _.defaultsDeep(options, defaultOptions)
this._options = _.defaultsDeep(options, defaultOptions)

RandomHttpUserAgent.configure(this._options[ 'random-http-useragent' ])
}

vote (pollUrl, pollId, pollOptionId) {
if (!pollUrl || !pollId || !pollOptionId) {
return Promise.reject(new Error('invalid parameters'))
}

const evaluate = function (pollId, pollOptionId) {
var p = pollId
var a = pollOptionId + ','
var b = 1
var o = ''
var va = 0
var cookie = 0
var url = encodeURIComponent(window.location.origin)
var now = new Date().getTime()

return new Promise(function (resolve, reject) {
$.ajax({
url: 'http://static.polldaddy.com/p/' + p + '.js',
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
var h
try {
h = data.match(/var PDV_h\d+ = \'(.*)\';/)[ 1 ]
} catch (error) {
return reject(error)
}

resolve(h)
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
})
.then(function (h) {
return new Promise(function (resolve, reject) {
$.ajax({
url: 'https://polldaddy.com/n/' + h + '/' + p + '?' + now,
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
var n
try {
n = data.match(/PDV_n\d+=\'(.*)\';.*/)[ 1 ]
} catch (error) {
return reject(error)
}

resolve(n)
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
})
})
.then(function (n) {
return new Promise(function (resolve, reject) {
$.ajax({
url: 'http://polls.polldaddy.com/vote-js.php?p=' + p + '&a=' + a + '&b=' + b + '&o=' + o + '&va=' + va + '&cookie=' + cookie + '&n=' + n + '&url=' + url,
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
resolve()
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
})
})
.then(function () {
return new Promise(function (resolve, reject) {
$.ajax({
url: 'http://ipinfo.io/json',
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
resolve(data)
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
return Promise.try(() => {
if (!pollUrl || !pollId || !pollOptionId) {
throw new Error('invalid arguments')
}
})
.then(() => RandomHttpUserAgent.get())
.then((userAgent) => {
const nightmare = Nightmare(this._options.nightmare)

return nightmare
.useragent(userAgent)
.goto(pollUrl)
.inject('js', jqueryPath)
.evaluate(vote, pollId, pollOptionId)
.end()
.catch(() => {
throw new Error('poll not available')
})
})
}

return vote.bind(this)(pollUrl, pollId, pollOptionId, evaluate)
})
}
}

Expand Down
94 changes: 94 additions & 0 deletions src/vote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable no-undef,no-useless-escape,promise/no-native */

/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

function evaluate (pollId, pollOptionId) {
var p = pollId
var a = pollOptionId + ','
var b = 1
var o = ''
var va = 0
var cookie = 0
var url = encodeURIComponent(window.location.origin)
var now = new Date().getTime()

return new Promise(function (resolve, reject) {
$.ajax({
url: 'http://static.polldaddy.com/p/' + p + '.js',
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
var h
try {
h = data.match(/var PDV_h\d+ = \'(.*)\';/)[ 1 ]
} catch (error) {
return reject(error)
}

resolve(h)
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
})
.then(function (h) {
return new Promise(function (resolve, reject) {
$.ajax({
url: 'https://polldaddy.com/n/' + h + '/' + p + '?' + now,
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
var n
try {
n = data.match(/PDV_n\d+=\'(.*)\';.*/)[ 1 ]
} catch (error) {
return reject(error)
}

resolve(n)
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
})
})
.then(function (n) {
return new Promise(function (resolve, reject) {
$.ajax({
url: 'http://polls.polldaddy.com/vote-js.php?p=' + p + '&a=' + a + '&b=' + b + '&o=' + o + '&va=' + va + '&cookie=' + cookie + '&n=' + n + '&url=' + url,
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
resolve()
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
})
})
.then(function () {
return new Promise(function (resolve, reject) {
$.ajax({
url: 'http://ipinfo.io/json',
type: 'GET',
crossDomain: true,
success: function (data, status, xhr) {
resolve(data)
},
error: function (jqXHR, textStatus, errorThrown) {
reject(errorThrown)
}
})
})
})
}

module.exports = evaluate
Loading

0 comments on commit 89c176b

Please sign in to comment.