-
Notifications
You must be signed in to change notification settings - Fork 6
/
vk.js
199 lines (139 loc) · 4.99 KB
/
vk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var url = require('url')
var request = require('request')
var _ = require('lodash')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var phantom = require('phantom')
url.extend = function(url1, url2) {
if (typeof url1 === 'string') url1 = url.parse(url1)
if (typeof url2 === 'string') url2 = url.parse(url2)
return url.format(_.extend(url1, url2));
}
var production = (process.env.NODE_ENV === 'production')
var VK = function(_options) {
var self = this
self.options = _options
if (self.options.proxy) {
request = request.defaults({ proxy: self.options.proxy })
}
self._authorizeUrl = 'https://oauth.vk.com/authorize'
self._blankUrl = 'https://oauth.vk.com/blank.html'
self._apiUrl = 'https://api.vk.com/method/'
self.authUrl = function (scope) {
return url.extend(self._authorizeUrl, {
query: {
client_id: self.options.appID,
scope: scope,
redirect_uri: self._blankUrl,
response_type: 'token',
display: 'mobile',
v: '5.21'
}
})
}
self.tokenFromUrl = function (resultUrl) {
if (!resultUrl.match(/access_token=/)) return console.log('auth href:', resultUrl)
var result = _.object(resultUrl.split('#')[1].split('&').map(function (i) {
return i.split('=')
}))
return result
}
self.checkToken = function () {
if (!self.token) {
console.log('ERROR: no token!')
return false
}
if (self.token.expires < Date.now()) {
console.log('ERROR: token expired!')
return false
}
return true
}
self.request = function(_method, _params) {
var url = self._apiUrl + _method + '?' + 'access_token=' + self.token.value
for (var key in _params) {
url += ('&' + key + '=' + encodeURIComponent(_params[key]))
}
!production && console.log('vk request:', _method, _params)
request({ url: url, json: true }, function (error, response, body) {
if (error) {
console.log('vk error:', _method, body, error);
} else {
!production && console.log('vk:', _method, body, error)
self.emit('done:' + _method, body, error);
}
});
}
self.setToken = function (_param) {
if (_param.expires_in) {
_param.expires = Date.now() + (parseInt(_param.expires_in) - 500) * 1000
delete _param.expires_in
}
if (!_param.expires) {
_param.expires = Date.now() + 20 * 60 * 60 * 1000
}
if (_param.access_token) {
_param.value = _param.access_token
delete _param.access_token
}
self.token = _param
}
var lastUpdateToken = 0
var pageLoadTimeout = 15 * 1000
self.updateToken = function (callback) {
if (Date.now() - lastUpdateToken < 60 * 1000) {
return
}
var result = null
var _url = self.authUrl(self.options.scope)
var auth = {
login: self.options.login,
password: self.options.password
}
phantom.create('--load-images=no', '--proxy=' + self.options.proxy.split('//')[1], function (ph) {
!production && console.log('phantom create')
// phantom.create(function (ph) {
ph.createPage(function (page) {
!production && console.log('phantom page create')
page.set('settings.userAgent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B350 Safari/8536.25')
page.set('onUrlChanged', function(url) {
!production && console.log("phantom page new URL: "+url)
if (url.indexOf('access_token') > -1) {
result = true
self.setToken(self.tokenFromUrl(url))
callback(self.token)
!production && console.log('we got token', self.token)
ph.exit();
}
})
page.open(_url, function (status) {
// if (status === 'success')
!production && console.log('phantom page opened')
setTimeout(function () {
!production && console.log('phantom page load timeout')
page.evaluate(function (auth) {
var login = document.querySelector('input[name="email"]')
if (login) login.value = auth.login
var pass = document.querySelector('input[name="pass"]')
if (pass) pass.value = auth.password
var form = document.querySelector('form')
if (form) form.submit()
return { }
}, function (result) {
!production && console.log('phantom page script end')
// !production && page.render('page.png');
setTimeout(function () {
if (!result) {
console.log('ERROR: we cannot get token')
ph.exit();
}
}, pageLoadTimeout)
}, auth)
}, pageLoadTimeout)
})
})
})
}
}
util.inherits(VK, EventEmitter);
module.exports = VK