-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
299 lines (270 loc) · 10.1 KB
/
index.js
File metadata and controls
299 lines (270 loc) · 10.1 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/******************************************************************************
*
* Spin Rewriter API for JavaScript and Node.js (npm)
*
* The only article spinner that truly understands the meaning of your content.
*
* With ENL technology, Spin Rewriter is the perfect tool for SEO specialists
* that need unique, human-quality content to rank higher on Google.
*
* Note: Spin Rewriter API server is using a 120-second timeout.
* Client scripts should use a 150-second timeout to allow for HTTP connection
* overhead.
*
* SDK Version : v1.1
* Language : JavaScript (Node.js)
* Dependencies : form-data, node-fetch
* Website : https://www.spinrewriter.com/
* Contact email : info@spinrewriter.com
*
* JS SDK Author : Bartosz Wójcik (https://www.pelock.com)
*
*****************************************************************************/
// ES module style imports
import FormData from 'form-data';
import fetch from 'node-fetch';
export class SpinRewriterAPI {
/**
* Spin Rewriter API constructor, complete with authentication.
* @param {string} email_address
* @param {string} api_key
*/
constructor(email_address, api_key) {
// make this an object (NOT an Array)
this.data = {};
this.data['email_address'] = email_address;
this.data['api_key'] = api_key;
this.api_url = "https://www.spinrewriter.com/action/api";
}
/**
* Parse a boolean value encoded either as a boolean type, string of "true" / "false" or a number 0/1
* @param {boolean|string|number} boolean_number_or_text
* @param {boolean} convert_to_string_bool
* @return {boolean|string} output boolean value in preferred format
*/
parseBool(boolean_number_or_text, convert_to_string_bool = true) {
let result = false;
switch(typeof(boolean_number_or_text)) {
case "string": result = boolean_number_or_text.toLowerCase() === "true"; break;
case "boolean": result = boolean_number_or_text === true; break;
case "number": result = Number(boolean_number_or_text) === 1; break;
}
if (convert_to_string_bool === true) {
return result ? "true" : "false";
}
return result;
}
/**
* Returns the API Quota (the number of made and remaining API calls for the 24-hour period).
* @return {Promise}
*/
getQuota() {
this.data['action'] = "api_quota";
return this.makeRequest();
}
/**
* Returns the processed text with the {first option|second option} spinning syntax.
* @param {string} text
* @return {Promise}
*/
getTextWithSpintax(text) {
this.data['action'] = "text_with_spintax";
this.data['text'] = text;
return this.makeRequest();
}
/**
* Returns one of the possible unique variations of the processed text.
* @param {string} text
* @return {Promise}
*/
getUniqueVariation(text) {
this.data['action'] = "unique_variation";
this.data['text'] = text;
return this.makeRequest();
}
/**
* Returns one of the possible unique variations of given text that already contains valid spintax. No additional processing is done.
* @param {string} text
* @return {Promise}
*/
getUniqueVariationFromSpintax(text) {
this.data['action'] = "unique_variation_from_spintax";
this.data['text'] = text;
return this.makeRequest();
}
/**
* Sets the list of protected keywords and key phrases.
* @param {Array|string} protected_terms (array of words, comma separated list, newline separated list)
* @return {boolean} boolean
*/
setProtectedTerms(protected_terms) {
this.data['protected_terms'] = "";
if (Array.isArray(protected_terms)) {
// array of words
for (let protected_term of protected_terms) {
protected_term = protected_term.trim();
if ((typeof protected_term === "string") && (protected_term.length > 2)) {
this.data['protected_terms'] += protected_term + "\n";
}
}
this.data['protected_terms'] = this.data['protected_terms'].trim();
return true;
} else if (protected_terms.includes(",") !== false) {
// comma separated list
let protected_terms_explode = protected_terms.split(",");
for (let protected_term of protected_terms_explode) {
protected_term = protected_term.trim();
if (protected_term && protected_term.length > 2) {
this.data['protected_terms'] += protected_term + "\n";
}
}
this.data['protected_terms'] = this.data['protected_terms'].trim();
return true;
} else if (protected_terms.trim().includes("\n") !== false) {
// newline separated list (the officially supported format)
protected_terms = protected_terms.trim();
if (protected_terms.length > 0) {
this.data['protected_terms'] = protected_terms;
return true;
} else {
return false;
}
} else if (typeof(protected_terms) === "string" && protected_terms.trim() && protected_terms.trim().length > 2 && protected_terms.trim().length < 50) {
// a single word or phrase (the officially supported format)
this.data['protected_terms'] = protected_terms.trim();
return true;
} else {
// invalid format
return false;
}
}
/**
* Sets whether the One-Click Rewrite process automatically protects Capitalized Words outside the article's title.
* @param {boolean|string|number} auto_protected_terms boolean
* @return {boolean} boolean
*/
setAutoProtectedTerms(auto_protected_terms) {
this.data['auto_protected_terms'] = this.parseBool(auto_protected_terms);
return true;
}
/**
* Sets the confidence level of the One-Click Rewrite process.
* @param {('low', 'medium', 'high')} confidence_level
* @return {boolean} boolean
*/
setConfidenceLevel(confidence_level) {
this.data['confidence_level'] = confidence_level;
return true;
}
/**
* Sets whether the One-Click Rewrite process uses nested spinning syntax (multi-level spinning) or not.
* @param {boolean|string|number} nested_spintax boolean
* @return {boolean} boolean
*/
setNestedSpintax(nested_spintax) {
this.data['nested_spintax'] = this.parseBool(nested_spintax);
return true;
}
/**
* Sets whether Spin Rewriter rewrites complete sentences on its own.
* @param {boolean|string|number} auto_sentences boolean
* @return {boolean} boolean
*/
setAutoSentences(auto_sentences) {
this.data['auto_sentences'] = this.parseBool(auto_sentences);
return true;
}
/**
* Sets whether Spin Rewriter rewrites entire paragraphs on its own.
* @param {boolean|string|number} auto_paragraphs boolean
* @return {boolean} boolean
*/
setAutoParagraphs(auto_paragraphs) {
this.data['auto_paragraphs'] = this.parseBool(auto_paragraphs);
return true;
}
/**
* Sets whether Spin Rewriter writes additional paragraphs on its own.
* @param {boolean|string|number} auto_new_paragraphs boolean
* @return {boolean} boolean
*/
setAutoNewParagraphs(auto_new_paragraphs) {
this.data['auto_new_paragraphs'] = this.parseBool(auto_new_paragraphs);
return true;
}
/**
* Sets whether Spin Rewriter changes the entire structure of phrases and sentences.
* @param {boolean|string|number} auto_sentence_trees boolean
* @return {boolean} boolean
*/
setAutoSentenceTrees(auto_sentence_trees) {
this.data['auto_sentence_trees'] = this.parseBool(auto_sentence_trees);
return true;
}
/**
* Sets whether Spin Rewriter should only use synonyms (where available) when generating spun text.
* @param {boolean|string|number} use_only_synonyms
* @return {boolean}
*/
setUseOnlySynonyms(use_only_synonyms) {
this.data['use_only_synonyms'] = this.parseBool(use_only_synonyms);
return true;
}
/**
* Sets whether Spin Rewriter should intelligently randomize the order of paragraphs and lists when generating spun text.
* @param {boolean|string|number} reorder_paragraphs
* @return {boolean}
*/
setReorderParagraphs(reorder_paragraphs) {
this.data['reorder_paragraphs'] = this.parseBool(reorder_paragraphs);
return true;
}
/**
* Sets whether Spin Rewriter should automatically enrich generated articles with headings, bullet points, etc.
* @param {boolean|string|number} add_html_markup
* @return {boolean}
*/
setAddHTMLMarkup(add_html_markup) {
this.data['add_html_markup'] = this.parseBool(add_html_markup);
return true;
}
/**
* Sets whether Spin Rewriter should automatically convert line-breaks to HTML tags.
* @param {boolean|string|number} use_html_linebreaks
* @return {boolean}
*/
setUseHTMLLinebreaks(use_html_linebreaks) {
this.data['use_html_linebreaks'] = this.parseBool(use_html_linebreaks);
return true;
}
/**
* Sets the desired spintax format to be used with the returned spun text.
* @param {('{|}', '{~}', '[|]', '[spin]')} spintax_format ('{|}', '{~}', '[|]', '[spin]')
* @return {boolean}
*/
setSpintaxFormat(spintax_format) {
this.data['spintax_format'] = spintax_format;
return true;
}
/**
* Sends a request to the Spin Rewriter API and return a Promise with JSON encoded response.
* @return {Promise}
*/
makeRequest() {
// prepare a form to make a POST request
const form = new FormData();
// built the form entry
for (let item in this.data) {
if (this.data.hasOwnProperty(item)) {
form.append(item, this.data[item]);
}
}
// send the request and return a Promise (return JSON by default)
return fetch(this.api_url, {
method: 'POST',
body: form,
headers: form.getHeaders()
})
.then(response => response.json());
}
}