forked from netconstructor/form-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-scraper.js
110 lines (90 loc) · 2.95 KB
/
form-scraper.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
var _ = require("underscore");
var when = require("when");
var cheerio = require("cheerio");
var nodeUrl = require("url");
var ScrapingFormProvider = function(options) {this.options = options; };
_.extend(ScrapingFormProvider, {
provideForm: function (formId, url, pRequest) {
var $;
var that = this;
return pRequest.get(url)
.then(fetchFormDataFromHttpResponse);
function fetchFormDataFromHttpResponse(response) {
$ = cheerio.load(response.body);
var formDom = $(formId);
if (isFormAbsent(formDom))
return when.reject(C.ERROR_FORM_IS_ABSENT);
return when.resolve(fetchFormDataFrom(formDom));
}
function fetchFormDataFrom(formDom) {
var formData = {};
provideActionData.apply(this, [formDom, formData]);
provideInputData(formDom, formData);
return formData;
}
function isFormAbsent(formDom) {
return formDom.length < 1;
}
function provideActionData(formDom, formData) {
formData.action = nodeUrl.resolve(url, formDom.attr('action'));
}
function provideInputData(formDom, formData) {
formData.data = {};
formDom.find("input").each( function () {
formData.data[$(this).attr("name")] = $(this).val();
});
}
}
})
_.extend(ScrapingFormProvider.prototype, {
updateOptions: function (options) {
this.options = _.extend({}, this.options, options);
return this;
},
provideForm: function () {
return ScrapingFormProvider.provideForm(this.options.formId, this.options.url, this.options.pRequest);
}
});
var FormSubmitter = function (options) { this.options = options ? options : {}; };
_.extend(FormSubmitter, {
submitForm: function (formValues, formProvider, pRequest) {
return formProvider.provideForm().then( function (formData) {
return pRequest.post(formData.action, {form: _.extend(formData.data, formValues) });
});
}
});
_.extend(FormSubmitter.prototype, {
updateOptions: function (options) {
this.options = _.extend({}, this.options, options);
return this;
},
submitForm: function (formValues) {
return FormSubmitter.submitForm(formValues, this.options.formProvider, this.options.pRequest);
}
})
var C = {
ERROR_FORM_IS_ABSENT: "ERROR_FORM_IS_ABSENT"
}
exports.ScrapingFormProvider = ScrapingFormProvider;
exports.FormSubmitter = FormSubmitter;
exports.fetchForm = ScrapingFormProvider.provideForm;
exports.submitForm = FormSubmitter.submitForm;
exports.provideForm = function (promiseForForm) {
return {
provideForm: function () { return promiseForForm }
}
}
function createScrapingFormProvider (formId, url, pRequest) {
return new ScrapingFormProvider({
formId: formId,
url: url,
pRequest: pRequest
});
}
exports.createScrapingFormProvider = createScrapingFormProvider;
exports.createFormSubmitter = function (formId, url, pRequest) {
return new FormSubmitter({
pRequest: pRequest,
formProvider: createScrapingFormProvider(formId, url, pRequest)
});
}