Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit 6207953c authored by Benzakein Jeremy's avatar Benzakein Jeremy

get person with image url from id in swapi api

parent 3375a2cc
......@@ -4,7 +4,7 @@
<head>
<meta charset="utf-8" />
<title>Test</title>
<title>Test Person</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
......@@ -38,36 +38,22 @@
</div>
</nav>
<div class="jumbotron jumbotron-fluid bg-warning">
<h1 class="display-3">People from Star Wars</h1>
<h1 class="display-3">One random person from Star Wars</h1>
<hr class="my-2">
<p>More info</p>
<p class="lead">
<input type="text" id="searchQuery"></input>
<a class="btn btn-primary btn-lg" href="#" role="button" id="getImage">Go</a>
</p>
<div id="imgContainer"></div>
<table class="table" id="peopleTable">
<thead>
<tr>
<th></th>
<th>Nom</th>
<th>Année de Naissance</th>
</tr>
</thead>
<tbody id="peopleTableBody">
</tbody>
</table>
</div>
<table class="table" id="peopleTable">
<thead>
<tr>
<th></th>
<th>Nom</th>
<th>Année de Naissance</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row"></td>
<td></td>
<td></td>
</tr>
<tr>
<td scope="row"></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<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" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
......@@ -76,7 +62,7 @@
crossorigin="anonymous"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script src="people.js"></script>
<script src="test.1.js"></script>
</body>
</html>
\ No newline at end of file
/* jshint esversion: 8 */
$(() => {
$('#peopleTable').DataTable({
scrollCollapse: true
});
index = 2
getPerson(index).then(result=>{
console.log(result);
$tbody = $('#peopleTableBody');
$tbody.append("<tr id='person" + index + "'><td></td><td>" + result.name + "</td><td>" + result.birth_year + "</td></tr>");
}).catch(err=>{
console.error(err);
});
});
const getPeopleCount = async (index) => {
let count = 0;
await $.ajax('/getPeopleCount')
.done(function(data){
count = data;
})
.fail(function(e){
console.log(e);
});
return count;
};
const getPerson = async (index) => {
let person = null;
await $.ajax('/getPerson/'+index)
.done(function(data){
person =JSON.parse(data);
})
.fail(function(e){
console.log(e);
});
return person;
};
\ No newline at end of file
/*jshint esversion: 6 */
/*jshint esversion: 8, node: true */
'use strict'
const express = require('express-alias'); //import de la bibliothèque Express avec une extention
const app = express(); //instanciation d'une application Express
const swapi = require('swapi-node');
const Search = require('azure-cognitiveservices-imagesearch');
const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
const swapiPeople = require('./swapiPeople');
const services = require('./services');
//ici on définira ensuite les "routes" = les requêtes HTTP acceptées par l'application
app.get('/StarPeople/*.*', function(req, res) {
let fileToGet = req.url.split('/').pop();
res.sendFile(__dirname + '/StarPeople/' + fileToGet);
app.get('/StarPeople/*.*', function (req, res) {
let fileToGet = req.url.split('/').pop();
res.sendFile(__dirname + '/StarPeople/' + fileToGet);
});
app.get('/getAllPeople',(req, res) => {
res.send(swapiPeople.getAllPeople());
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=>{
res.send(JSON.stringify(result));
});
});
app.get('/getPortrait/*', (req,res) =>{
const serviceKey = "fedf3a8b769e42c5af770843a2c9746a";
app.get('/getPortrait/*', (req, res) => {
const serviceKey = "e88b02b0a83143d294ac278ced6c1c13";
const defaultPic = "https://forums.roku.com/styles/canvas/theme/images/no_avatar.jpg";
let searchTerm = swapiPeople.removeAccents(req.url.split('/').pop());
let searchTerm = 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.");
.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);
}
else {
result = imageResults.value[0].contentUrl;
res.send(result);
}
})
.catch(err => {
console.error(err);
res.send(result);
});
});
});
app.get('/getPortrait/*', (req, res) => {
res.send(swapiPeople.getImage(req.url.split('/').pop()));
app.get('/getPerson/*', (req, res) => {
const index = parseInt(req.url.split('/').pop());
services.getPers(index)
.then(pers=>{
res.send(JSON.stringify(pers));
});
});
app.alias('/StarPeople/home','/StarPeople/index.html');
app.alias('/StarPeople/home', '/StarPeople/index.html');
// Finalement, on "lance" le serveur.
......
/*jshint esversion: 8, node: true */
const express = require('express-alias'); //import de la bibliothèque Express avec une extention
const app = express(); //instanciation d'une application Express
const swapi = require('swapi-node');
const Search = require('azure-cognitiveservices-imagesearch');
const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
exports.getPers = async (index) => {
let personObj;
await swapi.getPerson(index)
.then(pers=>{
personObj = new Person(pers.name, pers.birth_year, pers.height, pers.mass);
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;
};
const 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";
//instantiate the image search client
const credentials = new CognitiveServicesCredentials(serviceKey);
const imageSearchClient = new Search.ImageSearchClient(credentials);
let result = defaultPic;
await imageSearchClient.imagesOperations.search(name)
.then(searchResult =>{
if (searchResult == null || searchResult.value.length == 0) {
console.error("No image results were found.");
} else {
result = searchResult.value[0].contentUrl;
}
})
.catch(err =>{
console.error("No image results were found.");
console.error(err);
});
return result;
};
class Person{
constructor(name, bithYear, height, mass,) {
this.name = name;
this.bithYear = bithYear;
this.height = height;
this.mass = mass;
}
setOrigin(origin){
this.origin = origin;
}
setImg(img){
this.img = img;
}
}
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