forked from ithinkdancan/node-semrush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (99 loc) · 3.71 KB
/
index.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
var csv = require('csv');
var extend = require('./extend');
var querystring = require('querystring');
var http = require('http');
var SEMRushAPI = function(apiKey, options){
this.configure({ apiKey: apiKey, options:options });
};
module.exports = SEMRushAPI;
(function() {
this.options = {
hostname : 'api.semrush.com',
database : 'us',
path : '/',
userAgent : 'node-semrush (https://github.com/ithinkdancan/node-semrush)',
apiKey: ''
};
this.defaultParams = {
export : 'api',
action : 'report',
display_limit : '10',
};
this.configure = function(options) {
extend(this.options, options);
};
this.domain_rank = function(domain, customParams, cb) {
this._domainRequest('domain_rank', domain, customParams, cb);
};
this.domain_organic = function(domain, customParams, cb) {
this._domainRequest('domain_organic', domain, customParams, cb);
};
this.domain_organic_organic = function(domain, customParams, cb) {
this._domainRequest('domain_organic_organic', domain, customParams, cb);
};
this.domain_adwords_adwords = function(domain, customParams, cb) {
this._domainRequest('domain_adwords_adwords', domain, customParams, cb);
};
this.domain_adwords = function(domain, customParams, cb) {
this._domainRequest('domain_adwords', domain, customParams, cb);
};
this.phrase_this = function(phrase, customParams, cb) {
this._keywordRequest('phrase_this', phrase, customParams, cb);
};
this.phrase_organic = function(phrase, customParams, cb) {
this._keywordRequest('phrase_organic', phrase, customParams, cb);
};
this.phrase_adwords = function(phrase, customParams, cb) {
this._keywordRequest('phrase_adwords', phrase, customParams, cb);
};
this.phrase_related = function(phrase, customParams, cb) {
this._keywordRequest('phrase_related', phrase, customParams, cb);
};
this.phrase_adwords_historical = function(phrase, customParams, cb) {
this._keywordRequest('phrase_adwords_historical', phrase, customParams, cb);
};
this.phrase_fullsearch = function(phrase, customParams, cb) {
this._keywordRequest('phrase_fullsearch', phrase, customParams, cb);
};
this._domainRequest = function (type, domain, customParams, cb) {
var params = {
type: type,
domain: domain,
key: this.options.apiKey,
database: this.options.database
};
this._doRequest(params, customParams, cb);
};
this._keywordRequest = function (type, phrase, customParams, cb) {
var params = {
type: type,
phrase: phrase,
key: this.options.apiKey,
database: this.options.database
};
this._doRequest(params, customParams, cb);
};
this._doRequest = function(params, customParams, cb) {
extend(params, this.defaultParams, customParams);
var path = 'http://' + this.options.hostname + '/?' + querystring.stringify(params);
var req = http.get(path, function(res) {
var data = [];
res
.on('data', function(chunk) { data.push(chunk); })
.on('end', function() {
var urldata = data.join('').trim();
if(urldata.indexOf('ERROR') === 0){
cb(new Error('response.error:' + urldata), null);
} else {
csv().from.string(urldata, {delimiter: ';', columns: true}).to.array(function(result){
cb(null, result);
});
}
})
.on('error', function (err) {
cb(new Error('response.error:' + err), null);
});
});
return req;
};
}).call(SEMRushAPI.prototype);