-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathintake.html
More file actions
381 lines (345 loc) · 12.5 KB
/
intake.html
File metadata and controls
381 lines (345 loc) · 12.5 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<html>
<head>
<title>Google Takeout importer</title> <!--
<script src="https://linkeddata.github.io/mashlib/dist/mashlib.js"></script>
-->
<script src="http://localhost:3080/timbl/Automation/Library/Mashup/mashlib.js"></script>
<script>
document.addEventListener('DOMContentLoaded', event => {
// zip file reading https://gildas-lormeau.github.io/zip.js/core-api.html
const live = false // live or test mode
const dom = document
const UI = window.UI
const store = UI.store
const fetcher = store.fetcher
// const load = store.fetcher.load does not work
const ns = UI.ns
const contains = ns.ldp('contains')
const type = ns.rdf('type')
console.log('dom loaded')
const pre = dom.getElementById('log')
const div = dom.getElementById('work')
const statusArea = dom.getElementById('statusArea')
function log(str) {
console.log(str)
pre.textContent += str + '\n'
}
log('hello world')
async function importTakeout (root) {
function branch(branch, twig) {
return $rdf.sym(branch.uri + twig)
}
function tail(file) { // last part of the path
let split = file.uri.split('/') // @@ unencode
return decodeURIComponent(split.pop() || split.pop()) // file or folder
}
function fileIn (folder, tail) {
return $rdf.sym(folder.uri + tail)
}
function happy (response) {
if (!response.ok) {
let msg = ' HTTP error! Status: ' + response.status + ' for ' + response.url
log(msg)
response.error = new Error(msg)
// statusRow.appendChild(UI.widgets.errorMessageBlock(dom, msg))
}
return response.ok
}
/** Convert a JSON like object to RDF
*/
function mapPredicate(p) {
return $rdf.sym('http://www.w3.org/ns/pim/json#' + p) // @@ fix Makyby takeout ns?
}
function jsToRDF (x, newDoc) {
//log(' jsToRDF: ' + JSON.stringify(x))
if (x instanceof Array) {
let y = new $rdf.List()
x.forAll(x1 => y.elements.push(jsToRDF(x1, newDoc)))
return y
} else if (x instanceof Object){
let subject = new $rdf.BlankNode()
for (let key in x) {
let object = jsToRDF(x[key], newDoc)
let predicate = mapPredicate(key)
log (` Add {${subject} ${predicate} ${object}} @ ${tail(newDoc)}`)
store.add(subject, predicate, object, newDoc)
}
return subject
} else {
return $rdf.Literal.fromValue(x)
}
}
/** Convert a JSON file into an RDF file
*/
async function importJSON (doc, newDoc, mapping) {
log('Importing JSON doc ' + doc)
let response = await fetcher.webOperation('GET', doc.uri)
log(' ... @@ done load ' + doc)
if (!happy(response)) return response.error
const desc = response.responseText
const json = JSON.parse(desc)
let subject = $rdf.sym(newDoc.uri + '.ttl#this')
log(' regurgitated json:')
log(JSON.stringify(json) + '\n')
let RDFobject = jsToRDF(json, newDoc)
store.add(subject, mapPredicate('data'), RDFobject, newDoc)
let text = $rdf.serialize(newDoc, store)
log(` RDF from JSON: doc ${newDoc}\n:text: >>>>${text}<<<<\n`)
return newDoc
}
async function importCSV (doc, namespace) {
log(' CSV doc ' + doc)
let response = await fetcher.webOperation('GET', doc)
if (!happy(response)) return
const text = response.responseText
log(' Parsing CSV ' + desc)
loadCSV(text, store, doc, namespace)
}
// @@@ parse CSV////////////////////////////////////////
function parseCSV (text, delim) {
/** Column heading sanatize
*/
function sanitize (s) {
return s.replace('\n', ' ')
}
/** Santize a string can be used as ID
*/
function sanitizeID (s) {
const alpha = 'abcdefghijklmnopqrstuvwxyz' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const digits = '0123456789'
const alphnum = alpha + digits
let res = ''
for (let ch of s) {
if (alphnum.includes(ch)) {
res += ch
} else {
if (res.slice(-1) !== '_') {
res += '_' // convert to underscore but not multiple
}
}
}
while (res.slice(-1) === '_') {
res = res.slice(0,-1)
}
return res
}
/** Read a comma separated file (or tab sep) text
* @param {string} delim - typically comma or tab charcater
*/
function readOneRow() { // Was: readTabs
line = lines.shift() // take first
while (line !== "" && '\r\n'.includes(line.slice(-1))) {
line = line.slice(0, -1)
}
var fields = []
var field = ''
while (true) {
if (line[0] === '"') { // Quoted string
line = line.slice(1) // chop quote
while (1) { // Looking for unescaped terminator
let j = line.indexOf('"')
if (j >= 0) {
if (line.slice(j+1, j+2) === '"') {
field += line.slice(0,j +1)
line = line.slice(j + 2)
} else { //
field += line.slice(0,j) // we are done
line = line.slice(j + 1)
break // next field
}
} else {
field += line + '\n'
line = lines.shift() // Get more lines as quoted strings can cross them
}
} // looking fior end of field
} else { // not quote
let j = line.indexOf(delim)
if (j < 0) { // No more fields
field = line
} else {
field = line.slice(0, j)
line = line.slice(j+1)
}
}
fields.append(field)
} // feields
return fields
}
delim = delim || ','
var lines = text.split('\n')
const headers = readOneRow()
headers = headers.map(h => {
return h.trim()
})
if (headers.slice(-1)[0] === '') {
headers = headers.slice(0, -1) // remove empty field from paypal trailing comma
}
var lineno = 2
var table = []
while (true) {
let values = readOneRow()
if (values.length === 0) break
if (values.length === 1) continue
if (values .length !== headers.length) {
console.log(` Line ${lineno}: Warning: ${headers.length} headings but ${values.length} values`)
}
table.append(values)
}
return [headers, values]
} // parse CSV
function loadCSV (text, store, doc, namespace) {
namespace = namespace || 'http://www.w3.org/ns/pim/csv' // @@@
var headers, values
const base = doc.uri
[ headers, values ] = parseCSV(text, ',')
let labels = headers.map(h => {
let h = sanitizeID(h)
return h.slice(0,1).toLowerCase() + h.slice(1) // Initial lower case for predicate
})
for (let r=0; r < values.length; r++) {
row = values[r]
for (let col=0; col < row.length; col++) {
let subject = store.sym(base + 'n' + r)
let predicate = store.sym(namespace + label[col])
let object = row[col] // @@@ guess data types
store.add(subject, predicate, object, doc)
}
}
}
// @@@ end parse CSV////////////////////////////////////////
async function importGroups (folder) {
log(' Import Groups:')
let resp = await fetcher.load(folder)
if (happy(resp)) {
store.each(folder,contains).forEach(groupFolder => {
if (groupFolder.uri.endsWith('/')) {
log(' Group folder ' + groupFolder)
importCSV(fileIn(groupFolder, 'members.csv'))
}// ignore .DS_Store
})
}
}
function debugTurtle (x) {
let sts = store.connectedStatements(x)
let sz = new $rdf.Serializer(store)
return sz.statementsToN3(sts)
}
async function importPhotos (folder) {
log(' Import Photos:')
let resp = await fetcher.load(folder)
if (!happy(resp)) return resp.error
let albums = store.each(folder,contains)
log('albums.length: ' + albums.length)
for (let i=0; i< albums.length; i++) {
let photoFolder = albums[i]
log(' Photo Album ' + photoFolder)
if (!photoFolder.uri.endsWith('/')) continue
let resp = await fetcher.load(photoFolder)
if (!happy(resp)) return response.error
log(' Importing the metadata.json:')
let newAlbum = $rdf.sym(newPhotoLibrary.dir().uri + 'Albums/' + tail(photoFolder) + '/index.ttl#this')
let newAlbumDoc = newAlbum.doc()
let status = await importJSON(fileIn(photoFolder, 'metadata.json'), newAlbumDoc)
store.each(photoFolder,contains).forEach(photo => {
log('Photo data ' + photo)
log(debugTurtle(photo))
let classURIs = store.each(photo, type).map(x => x.uri).join(',')
if (classURIs.includes('iana/media-types/video') ||
classURIs.includes('iana/media-types/image')) {
let newPhotoDoc = $rdf.sym(newAlbumDoc.uri + tail(photo))
let newPhotoMetaDoc = $rdf.sym(newAlbumDoc.uri + tail(photo) + .ttl)
log(' Media: '+ photo)
let mediaMetaDoc = $rdf.sym(photo.uri + '.json')
let mediaMetaDoc = $rdf.sym(photo.uri + '.json')
log(' Media: '+ mediaMetaDoc)
await importJSON(mediaMetaDoc, newPhotoMetaDoc)
} else {
log(' Non-media: ' + photo)
}
})
}
}
var ABContext = {dom, div, statusArea}
if (live) {
ABContext = await UI.authn.findAppInstances(ABContext, ns.vcard('AddressBook'))
} else {
ABContext.instances = [ $rdf.sym('http://localhost:3080/timbl/Data/Contacts/index.ttl#this')]
ABContext.me = $rdf.sym('https://www.w3.org/People/Berners-Lee/card#i')
}
var addressBook
if (!ABContext.instances.length) {
log("@@@ Wot no address book")
} else {
addressBook = ABContext.instances[0]
log("Address book " + addressBook)
}
var photoLibraryContext = {dom, div, statusArea}
if (live) {
photoLibraryContext = await UI.authn.findAppInstances(photoLibraryContext, ns.solid('PhotoLibrary'))
} else {
photoLibraryContext.instances = [ $rdf.sym('http://localhost:3080/timbl/Photos/index.ttl#this')]
photoLibraryContext.me = $rdf.sym('https://www.w3.org/People/Berners-Lee/card#i')
}
var newPhotoLibrary = photoLibraryContext.instances[0]
log('loading ' + root)
let resp = await fetcher.load(root)
log('loaded ' + root)
let folders = store.each(root, contains)
for (let i=0; i < folders.length; i++) {
let folder = folders[i]
log(' Folder: ' + folder)
let name = tail(folder)// @@ unencode
log(' Looking at ' + name)
if (name.startsWith('.')) {
log(' ignoring hidden ' + name)
} else {
switch(name) {
case '+1s':
log(' Import Plus-ones (Bookmarks):')
// Here need to parse a HTML Netscape bookmark file
break
case 'Contacts':
log(' Import Contacts:')
break
case 'Contacts':
log(' Import Contacts:')
break
case 'Google Photos':
log(' Import Google Photos:')
await importPhotos(folder)
break
case 'Google+ Circles':
log(' Import Google Photos:')
break
case 'Groups':
let status = await importGroups(folder)
break
case 'index.html':
log(' skipping ' + name)
break
default:
log(' @@@@@@@ Unknown Top level Takeout: ' + name)
}
}
}
}
const here = $rdf.sym(document.location.href)
const root = $rdf.sym(here.dir().uri + 'Takeout/')
importTakeout(root)
})
</script>
</head>
<body>
<h1>Import Takeout</h1>
<p>This HTL file, if you want to uyse it as a tool,
should be put in the server as your google takeout data.
<div>
<pre id='log'>
</pre>
<div id='work' style='border-radius: 0.5em; border: solid gray 0.1em; padding: 1em;margin: 0.5em;'>
</div>
<div id='statusArea' style='border-radius: 0.5em; border: solid gray 0.1em; padding: 1em;margin: 0.5em;'>
</div>
</div>
</body>
</html>