const cliProgress = require('cli-progress');
const promptiCloud = require('./prompt-credentials');
const podClient = require('../../../pod/client');
const utils = require('./utils')
/** The Note Item class. We create a Note for every iCloud note. */
class Note {
constructor(dateCreated, dateModified, deleted, externalId, username, title, content) {
this._type = 'Note';
this.dateCreated = dateCreated;
this.dateModified = dateModified;
this.deleted = deleted;
this.externalId = externalId;
this.username = username;
this.title = title;
this.content = content;
// this.folders = folders; TODO add folders
}
}
(async () => {
const client = await new podClient.PodClient();
await client.testConnection();
console.log('Connect to iCloud...');
const myCloud = await promptiCloud();
// download iCould note records
console.log('Fetch note records...');
const notes = myCloud.Notes.fetch();
notes.on('end', async () => {
// filter records to only contain notes (no directories)
let noteRecords = await notes.__records.filter(function (el) {
return el.recordType === 'Note';
});
// get detailed notes from records
console.log('Download notes...');
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
progressBar.start(noteRecords.length, 0, {});
let failedDownloads = []
for (const noteRecord of noteRecords) {
progressBar.increment();
let notesDetailed
try {
notesDetailed = await myCloud.Notes.resolve(noteRecord);
} catch (e) {
failedDownloads.push({
'error': e,
'title': Buffer.from(noteRecord['fields']['TitleEncrypted']['value'], "base64").toString()
})
continue
}
// TODO add folders
// let folders = [];
// for (const folder of notesDetailed[0]['fields']['Folders']['value']) {
// folders.push(folder['recordName']);
// }
let deleted = false;
if (notesDetailed[0]['parent'] && notesDetailed[0]['parent']['recordName'] === "TrashFolder-CloudKit") {
deleted = true;
}
let note = new Note(
utils.convertTimeStamp(notesDetailed[0]['created']['timestamp']),
utils.convertTimeStamp(notesDetailed[0]['modified']['timestamp']),
deleted,
notesDetailed[0]['recordName'],
notesDetailed[0]['created']['userRecordName'],
notesDetailed[0]['fields']['title'],
notesDetailed[0]['fields']['text']['string']
)
// TODO: folders: []
// TODO: 'resources': []
// TODO: 'sharedWith': []
// send to Pod
await client.createItem(note);
}
progressBar.stop();
myCloud.saveSession();
for (const failedDownload of failedDownloads) {
console.log(`Unable to download note: ${failedDownload.title} || ${failedDownload.error}`);
}
// let allItems = await client.getAllItems();
// console.log(allItems);
});
})();