-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (94 loc) · 3.42 KB
/
index.js
File metadata and controls
118 lines (94 loc) · 3.42 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
const _ = require('lodash');
const axios = require('axios');
const { getShortAnswers, getParagraphs, getMultipleChoice,getDropDown, getCheckBoxes, getLinearScale } = require('./src/extractData.js');
const { vueSubmitForm } = require('./src/vueFns.js');
const getHtml = async (formUrl) => {
return await axios.get(formUrl).then(rsp => rsp.data);
}
const extractFbPublicLoadData = (html) => {
let data = html.split('FB_PUBLIC_LOAD_DATA_ = ')[1];
data = data.substring(0, data.indexOf(';'));
return JSON.parse(data);
}
const getBasicData = (form) => {
// Title
const formTitle = form[8] || '';
//Form description
const formDescription = form[0] || '';
return {
formTitle,
formDescription,
}
};
const getByCategory = async (formUrl) => {
const html = await getHtml(formUrl);
const fBData = extractFbPublicLoadData(html);
// Extract form that stores description and questions
const form = fBData[1] || null;
const { formTitle, formDescription } = getBasicData(form);
//Form questions
const formQuestions = form[1] || null;
//Extract all form data
let shortAnswers = getShortAnswers(formQuestions);
let paragraphs = getParagraphs(formQuestions);
let multipleChoice = getMultipleChoice(formQuestions);
let dropDown = getDropDown(formQuestions);
let checkBoxes = getCheckBoxes(formQuestions);
let linearScale = getLinearScale(formQuestions);
return {
formAction: `https://docs.google.com/forms/u/0/d/${fBData[14]}/formResponse`,
formTitle,
formDescription,
questions: {
shortAnswers,
paragraphs,
multipleChoice,
dropDown,
checkBoxes,
linearScale,
}
}
};
const get = async (formUrl) => {
const html = await getHtml(formUrl);
const fBData = extractFbPublicLoadData(html);
// Extract form that stores description and questions
const form = fBData[1] || null;
const { formTitle, formDescription } = getBasicData(form);
//Form questions
const formQuestions = form[1] || null;
let extractedQuestions = [];
formQuestions.forEach(q => {
switch(q[3]){
case 0: extractedQuestions.push(getShortAnswers(formQuestions)); break;
case 1: extractedQuestions.push(getParagraphs(formQuestions)); break;
case 2: extractedQuestions.push(getMultipleChoice(formQuestions)); break;
case 3: extractedQuestions.push(getDropDown(formQuestions)); break;
case 4: extractedQuestions.push(getCheckBoxes(formQuestions)); break;
case 5: extractedQuestions.push(getLinearScale(formQuestions)); break;
default: break;
}
})
//Hacky way below to get unique entries without writing separate functions
extractedQuestions = _.flatten(extractedQuestions);
//Obtain unique entries
let allEntries = new Set(extractedQuestions.map(i => i.entry));
//Convert back to array for mapping
allEntries = [...allEntries]
extractedQuestions = allEntries.map(i => {
const found = extractedQuestions.find(q => q.entry === i);
return {...found}
})
return {
formAction: `https://docs.google.com/forms/u/0/d/${fBData[14]}/formResponse`,
formTitle,
formDescription,
questions: extractedQuestions
};
}
module.exports = {
get,
getBasicData,
getByCategory,
vueSubmitForm,
}