-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
191 lines (162 loc) · 6.22 KB
/
content.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
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "clicked_browser_action" ) {
summarizer();
}
}
);
function summarizer() {
makeModal();
var propID = window.location.pathname.split('/').reverse()[0];
var {reviewCount, pageCount} = getReviewCount(propID);
const REVIEWERS = getAllReviews(propID, pageCount);
var averageAge = calcAverageAge(REVIEWERS);
var {males,females,other} = calcGenderRatio(REVIEWERS);
var {ageData} = calcAgeRatio(REVIEWERS);
var {countryData} = calcCountryRatios(REVIEWERS);
}
function getReviewCount(propID){
let results = {};
let $reviewCountStatus = $('#reviewCountStatus');
$reviewCountStatus.removeClass('hidden');
$.ajax({
type: "get",
async: false,
url: `https://www.hostelworld.com/properties/${propID}/reviews?sort=newest&page=1&monthCount=36`,
success: function(res){
results = {
total: res.pagination.totalNumberOfItems,
pageCount: res.pagination.numberOfPages,
}
$reviewCountStatus.html(`<i style='color:#3c763d' class='fa fa-check'></i> Reviews: <b>${results.total}</b>`);
}
});
return results;
}
//this will ajax every page sequentially
function getAllReviews(propID, pageCount){
var REVIEWS = [];
var i;
var $combineReviews = $('#combineReviews');
$combineReviews.removeClass('hidden')
for(i = 1; i <= pageCount; i++) {
$.ajax({
type: "get",
async: false,
url: `https://www.hostelworld.com/properties/${propID}/reviews?sort=newest&page=${i}&monthCount=36`,
success: function(res){
REVIEWS.push(res.reviews)
}
});
}
$combineReviews.html(`<i style='color:#3c763d' class='fa fa-check'></i> Combined <b>${pageCount}</b> Pages of Reviews`);
//flatten array of arrays
return [].concat.apply([],REVIEWS)
}
function calcAverageAge(REVIEWERS) {
var $averageAge = $('#averageAge');
var averageAge = 0;
var reviewCount = REVIEWERS.length;
$averageAge.removeClass('hidden')
REVIEWERS.forEach(function(reviewer,i){
let person = reviewer.reviewer
let age = person.ageGroup.includes('-') ?
//if range ex 18-24 then parse and get average
person.ageGroup.split('-').reduce(function(a,b){return +a+(+b)})/2 :
//other opts like 40+ just ParseInt()
parseInt(person.ageGroup)
averageAge += age
$averageAge.html(`Average Age ${Math.round(averageAge/reviewCount)} (<i>running</i>)`);
})
$averageAge.html(`<i style='color:#3c763d' class='fa fa-check'></i>Average Age: <b>${Math.round(averageAge/reviewCount)}</b>`);
return Math.round(averageAge/reviewCount)
}
function calcAgeRatio(REVIEWERS) {
var $ageRatio = $('#ageRatio');
var ageGroups = {'18-24':0, '25-30':0, '31-40':0, '41+': 0}
var reviewCount = REVIEWERS.length;
$ageRatio.removeClass('hidden');
REVIEWERS.forEach(function(reviewer,i){
let person = reviewer.reviewer
let ageGroup = person.ageGroup
ageGroups[ageGroup]++
})
var highest = Object.keys(ageGroups).reduce((a, b) => ageGroups[a] > ageGroups[b] ? a : b)
var content = ''
Object.keys(ageGroups).map((groupName) => {
content += `<p style='margin:0px; ${highest === groupName ? "font-weight:bold":"" }' >${groupName}: ${Math.round((ageGroups[groupName]/reviewCount)*100)}%</p>`
})
$ageRatio.html(`
<i style='color:#3c763d' class='fa fa-check'></i> Age Distribution Calculated </br>
<div style='margin-left: 17px; border-top: 1px solid whitesmoke; width: 181px;'>
${content}
</div>
`);
return ageGroups;
}
function calcGenderRatio(REVIEWERS) {
var $genderRatio = $('#genderRatio');
var {males, females, other} = {males:0, females:0, other:0};
var reviewCount = REVIEWERS.length;
$genderRatio.removeClass('hidden');
REVIEWERS.forEach(function(reviewer,i){
let person = reviewer.reviewer
let gender = person.gender.toLowerCase()
if( gender === 'male') {
males++
}else if(gender === 'female'){
females++
}else{
other++
}
$genderRatio.html(`Males${(males/reviewCount)*100} - Females{(females/reviewCount)*100} - Other{(other/reviewCount)*100} (<i>running</i>)`);
})
var highest = null;
if( males > females && males > other) {
highest = 'male'
}else if (females > males && females > other) {
highest = 'female'
}else if (other > males && other > females) {
highest = 'other'
}
$genderRatio.html(`
<i style='color:#3c763d' class='fa fa-check'></i> Gender Distribution Calculated </br>
<div style='margin-left: 17px; border-top: 1px solid whitesmoke; width: 181px;'>
<p style='margin:0px; ${highest === 'male'? "font-weight:bold":"" }' >Males: ${Math.round((males/reviewCount)*100)}%</p>
<p style='margin:0px; ${highest === 'female'? "font-weight:bold":"" }' >Females: ${Math.round((females/reviewCount)*100)}%</p>
<p style='margin:0px; ${highest === 'other'? "font-weight:bold":"" }' >Other: ${Math.round((other/reviewCount)*100)}%</p>
</div>
`);
return {males:males, females:females, other:other}
}
function calcCountryRatios(REVIEWERS) {
var $countryRatio = $('#countryRatio');
var countries = {}
var reviewCount = REVIEWERS.length;
$countryRatio.removeClass('hidden');
REVIEWERS.forEach(function(reviewer,i){
let person = reviewer.reviewer
let country = person.nationality
if ( countries[country] ) {
countries[country]++
} else {
countries[country] = 1
}
})
var sortedCountries = Object.keys(countries).sort(function(a, b) { return countries[b] - countries[a] });
// if we have more than 5 countries than we can get a Top 5 and need to cut someone out
if ( sortedCountries.length > 5 ) {
sortedCountries = sortedCountries.slice(0,5)
}
var content = ''
sortedCountries.map((countryName, idx) => {
content += `<p style='margin:0px; ${idx === 0 ? "font-weight:bold":"" }' >${countryName}: ${Math.round((countries[countryName]/reviewCount)*100)}%</p>`
})
$countryRatio.html(`
<i style='color:#3c763d' class='fa fa-check'></i> Nationality Distribution Calculated ${Object.keys(countries).length > 5 ? `(Top 5 of ${Object.keys(countries).length})` : '' } </br>
<div style='margin-left: 17px; border-top: 1px solid whitesmoke; width: 181px;'>
${content}
</div>
`);
return countries;
}