-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
179 lines (146 loc) · 4.87 KB
/
app.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
const express = require("express");
const expressHandlebars = require("express-handlebars");
// ------------------------------ Retrieve Profiles ------------------------------
const $rdf = require("rdflib");
const fs = require("fs");
//load the file into a string
var data = fs.readFileSync("profiles.ttl").toString();
var store = $rdf.graph();
var contentType = "text/turtle";
var baseURI = "http://example.com/demo";
//set up the namespace prefixes
var FOAF = $rdf.Namespace("http://xmlns.com/foaf/0.1/");
// store interests from profiles.tts of each user
var profiles = [];
// list all user URIs
var usersURI = ["#SZ", "#RB", "#MK"];
try {
$rdf.parse(data, store, baseURI, contentType);
usersURI.map((item, index) => {
var user = store.any($rdf.sym(baseURI + item), FOAF("name"));
var interest = store.any($rdf.sym(baseURI + item), FOAF("interest"));
var userInterest = {
id: index,
name: user.value,
interest: interest.value,
};
profiles.push(userInterest);
});
} catch (err) {
console.log(err);
}
// ------------------------------ Retrieve Profiles END ------------------------------
// ------------------------------ Queries ------------------------------
const ParsingClient = require("sparql-http-client/ParsingClient");
const endpointUrl = "https://dbpedia.org/sparql";
// ------------------------------ DBPEDIA ------------------------------
// Purposely using random to get different results each time
for (const profile of profiles) {
const query = `
SELECT DISTINCT ?film ?filmLabel ?abstract ?releaseDate
WHERE {
?film dbo:wikiPageWikiLink dbr:${profile.interest} .
?film foaf:name ?filmLabel .
?film dbo:abstract ?abstract .
FILTER (langMatches(lang(?abstract),"en"))
?film dbo:releaseDate ?releaseDate .
} ORDER BY RAND() LIMIT 10
`;
const client = new ParsingClient({
endpointUrl,
headers: { Accept: "application/json" },
});
client.query.select(query).then((bindings) => {
let suggestions = [];
bindings.forEach((row) => {
let suggestion = {};
Object.entries(row).forEach(([key, value]) => {
suggestion[key] = value.value;
});
suggestions.push(suggestion);
});
profile.suggestions = { ...profile.suggestions, ...suggestions };
});
}
// ------------------------------ WIKIDATA ------------------------------
const SparqlClient = require("sparql-http-client");
const endpointUrlWIKIDATA = "https://query.wikidata.org/sparql";
for (const profile of profiles) {
const query = `
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX schema: <http://schema.org/>
SELECT distinct ?film ?filmLabel ?releaseDate ?abstract
WHERE {
?film wdt:P136/rdfs:label "${profile.interest.toLowerCase().replace("_", " ")}"@en .
?film schema:description ?abstract .
FILTER (langMatches(lang(?abstract),"en"))
?film wdt:P577 ?releaseDate
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} LIMIT 10
`;
const client = new SparqlClient({ endpointUrl: endpointUrlWIKIDATA });
client.query.select(query).then((stream) => {
let suggestions = [];
stream
.on("data", (row) => {
let suggestion = {};
Object.entries(row).forEach(([key, value]) => {
suggestion[key] = value.value;
});
suggestions.push(suggestion);
})
.on("end", () => {
profile.suggestionsBis = { ...profile.suggestionsBis, ...suggestions };
});
});
}
// ------------------------------ Queries END ------------------------------
// Our Express app.
const app = express();
app.engine(
"hbs",
expressHandlebars.engine({
defaultLayout: "main.hbs",
})
);
// GET /profile/:id
app.get("/profile/:id", function (request, response) {
const id = request.params.id;
const profile = profiles.find((profile) => {
if (profile.id.toString() == parseInt(id)) {
return profile;
}
});
const model = {
moviesSuggestion: profile.suggestions,
moviesSuggestionBis: profile.suggestionsBis,
genre: profile.interest,
};
response.render("profile.hbs", model);
});
// GET /profiles
app.get("/profiles", function (request, response) {
const model = {
profiles: profiles,
};
response.render("profiles.hbs", model);
});
// GET /layout.css
app.get("/layout.css", function (request, response) {
response.sendFile("layout.css", { root: "." });
});
// GET /
app.get("/", function (request, response) {
response.render("start.hbs");
});
app.listen(8080); //Server PORT
const Handlebars = require("handlebars");
Handlebars.registerHelper('replace', function( find, replace, options) {
var string = options.fn(this);
return string.replace( find, replace );
});
Handlebars.registerHelper('formatDate', function(datetime) {
// take the first 10 characters of the date
return datetime.substring(0, 10);
});