Commit 343cdd7e authored by Benzakein Jeremy's avatar Benzakein Jeremy

last touches

parent 69940f1c
......@@ -39,22 +39,27 @@
</nav>
<div class="jumbotron jumbotron-fluid bg-warning custom-jumbo">
<table class="table" id="peopleTable">
<thead>
<tr>
<th></th>
<th>Nom</th>
<th>Année de Naissance</th>
<th>Origine</th>
<th>Taille</th>
<th>Poids</th>
</tr>
</thead>
<tbody id="peopleTableBody">
</tbody>
</table>
<div class="row">
<div class="col-10 offset-2">
<table class="table" id="peopleTable">
<thead>
<tr>
<th></th>
<th>Nom</th>
<th>Année de Naissance</th>
<th>Origine</th>
<th>Taille</th>
<th>Poids</th>
</tr>
</thead>
<tbody id="peopleTableBody">
</tbody>
</table>
</div>
</div>
</div>
<p class="foot">&#169; Copyrights 2019, StarPeople, Jeremy Benzakein & Raphaël Kern, CentraleSupelec, Gif-sur-Yvette</p>
<p class="foot">&#169; Copyrights 2019, StarPeople, Jeremy Benzakein & Raphaël Kern, CentraleSupelec, Gif-sur-Yvette
</p>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
......
......@@ -14,7 +14,7 @@ const getPeopleCount = async (index) => {
const getPerson = async (index) => {
let person = null;
let saved_data = JSON.parse(sessionStorage.getItem('peopleSaved'));
await $.ajax('/getPerson/' + index)
.done(function (data) {
person = JSON.parse(data);
......@@ -24,24 +24,46 @@ const getPerson = async (index) => {
});
return person;
};
const createRow = (obj) => {
datatable.row.add([
"sort_asc.png",
obj.name,
obj.birthYear,
obj.origin,
obj.height,
obj.mass
]).draw(true);
};
const getAllPeople = async () => {
var t_start = performance.now();
let count = await getPeopleCount();
let allPeople = [];
//if page has been seen before, no need to call the API again
const t_start = performance.now();
let allPeople = {};
const count = await getPeopleCount();
let saved_data = sessionStorage.getItem('peopleSaved');
if (saved_data!=null){
saved_data = JSON.parse(saved_data);
allPeople = saved_data;
for (let person_index in saved_data){
createRow(saved_data[person_index]);
}
if (Object.keys(saved_data).length == count){
console.log('getAllPeople took' + (performance.now() - t_start) / 1000 + " seconds");
return saved_data;
}
}
let i = 1;
while (allPeople.length < count) {
while (Object.keys(allPeople).length < count) {
if(i in allPeople) {
i++;
continue;
}
let pers = await getPerson(i);
if (pers != null) {
allPeople.push(pers);
datatable.row.add([
pers.img,
pers.name,
pers.birthYear,
pers.origin,
pers.height,
pers.mass
]).draw(true);
allPeople[i] = pers;
createRow(pers);
}
i++;
}
......@@ -54,9 +76,16 @@ let datatable;
$(() => {
//initialise paging, sorting, and searching in datatable
datatable = $('#peopleTable').DataTable({
"lengthMenu": [[5, 10, 25, -1], [5, 10, 25, "Tous"]],
//the table doesnt
stateSave: true,
"lengthMenu": [
[5, 10, 25, -1],
[5, 10, 25, "Tous"]
],
//sorting by names in alphabetic order by default
order: [[ 1, "asc" ]],
order: [
[1, "asc"]
],
columnDefs: [
//tell dataTable to render first column as HTML to show the image from the url
//disable sorting and searching of images
......@@ -70,28 +99,38 @@ $(() => {
width: "150px"
},
{
targets:[4,5],
targets: 4,
orderDataType: "custom-str"
},
{
targets: 5,
orderDataType: "custom-str"
}
]
});
//calling helper function that manages the API calls
getAllPeople().then(people => {console.log('all people are loaded !');})
.catch(err => {console.log(err);});
getAllPeople()
.then(people => {
//save results in sessionStorage so that the table's data doesn't have to be reloaded
sessionStorage.setItem('peopleSaved', JSON.stringify(people));
})
.catch(err => {
console.log(err);
});
});
//definition of a custom sorting method for datatable so that it sorts masses and heights correctly
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"custom-str-pre": function ( a ) {
var x = String(a).replace("kg", "").replace("m","").replace(",","");
return parseFloat( x );
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"custom-str-pre": function (a) {
let x = String(a).replace(" kg", "").replace(" cm", "").replace(",", "");
return parseFloat(x);
},
"custom-str-asc": function ( a, b ) {
"custom-str-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"custom-str-desc": function ( a, b ) {
"custom-str-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
\ No newline at end of file
......@@ -34,7 +34,11 @@ body{
/*Styling for the datatable*/
#peopleTable thead{
#peopleTable_wrapper{
width:80%;
}
#peopleTable thead{
background: #343a40;
color: #ffdd00;
}
......
......@@ -6,8 +6,8 @@ $('#getImage').click(function (e) {
let searchTerm = removeAccents($('#searchQuery').val());
$.get("/getPortrait/" + searchTerm, resultImgUrl => {
let img = $('img');
img.attr('id', imageResult);
let img = $('<img></img>');
img.attr('id', 'imageResult');
img.attr('src', resultImgUrl);
img.attr('width', '100%');
img.appendTo('#imgContainer');
......
......@@ -15,43 +15,17 @@ app.get('/StarPeople/*.*', function (req, res) {
});
app.get('/getPeopleCount',(req, res)=>{
const getPeople = async ()=>{
return await swapi.get('https://swapi.co/api/people').then(result=>{
return result.count;
});
};
getPeople().then(result=>{
services.getPeopleCount().then(result=>{
res.send(JSON.stringify(result));
});
});
app.get('/getPortrait/*', (req, res) => {
const serviceKey = "e88b02b0a83143d294ac278ced6c1c13";
const defaultPic = "https://forums.roku.com/styles/canvas/theme/images/no_avatar.jpg";
let searchTerm = removeAccents(req.url.split('/').pop());
let credentials = new CognitiveServicesCredentials(serviceKey);
let imageSearchClient = new Search.ImageSearchClient(credentials);
let result = defaultPic;
const sendQuery = async () => {
return await imageSearchClient.imagesOperations.search(searchTerm);
};
sendQuery()
.then(imageResults => {
console.debug(imageResults);
if (imageResults == null || imageResults.value.length == 0) {
console.error("No image results were found.");
res.send(result);
} else {
result = imageResults.value[0].contentUrl;
res.send(result);
}
})
.catch(err => {
console.error(err);
res.send(result);
});
let name = req.url.split('/').pop();
services.getImageFromName(name).then(resultURL=>{
res.send(resultURL);
});
});
app.get('/getPerson/*', (req, res) => {
......@@ -69,20 +43,4 @@ app.alias('/StarPeople/home', '/StarPeople/index.html');
// Finalement, on "lance" le serveur.
const port = 8000;
app.listen(port); //commence à accepter les requêtes
console.log("App listening on port " + port + "...");
let removeAccents = (str) => {
const accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
const accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
str = str.split('');
let i, x;
for (i = 0; i < str.length; i++) {
if ((x = accents.indexOf(str[i])) != -1) {
str[i] = accentsOut[x];
}
}
return str.join('');
};
\ No newline at end of file
console.log("App listening on port " + port + "...");
\ No newline at end of file
......@@ -6,33 +6,44 @@ const swapi = require('swapi-node');
const Search = require('azure-cognitiveservices-imagesearch');
const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
let people_dict = {};
exports.getPeopleCount = async ()=>{
return await swapi.get('https://swapi.co/api/people').then(result=>{
return result.count;
});
};
exports.getPers = async (index) => {
let personObj;
await swapi.getPerson(index)
.then(pers=>{
let n = pers.name;
let by= pers.birth_year;
let h =(pers.height != "unknown")? pers.height+"m":pers.height;
let m =(pers.mass != "unknown")? pers.mass+"kg":pers.mass;
personObj = new Person(n, by, h, m);
return pers.getHomeworld();
})
.then(hw=>{
if(hw != null && hw.name != undefined){
personObj.setOrigin(hw.name);
}
return getImageFromName(personObj.name);
})
.then(resultURL=>{
personObj.setImg(resultURL);
})
.catch(err =>{
console.error(err);
});
return personObj;
if(index == 17) return null;
else if(index in people_dict) return people_dict[index];
else{
await swapi.getPerson(index)
.then(pers=>{
let n = pers.name;
let by= pers.birth_year;
let h =(pers.height != "unknown")? pers.height+" cm":pers.height;
let m =(pers.mass != "unknown")? pers.mass+" kg":pers.mass;
personObj = new Person(n, by, h, m);
return pers.getHomeworld();
})
.then(hw=>{
if(hw != null && hw.name != undefined){
personObj.setOrigin(hw.name);
}
//return getImageFromName(personObj.name);
})
//.then(resultURL=>{
// personObj.setImg(resultURL);
//})
.catch(err =>{
console.error(err);
});
people_dict[index] = personObj;
return personObj;
}
};
const getImageFromName = async (name)=>{
exports.getImageFromName = async (name)=>{
//replace this value with your valid subscription key.
const serviceKey = "e88b02b0a83143d294ac278ced6c1c13";
const defaultPic = "https://forums.roku.com/styles/canvas/theme/images/no_avatar.jpg";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment