Skip to content

Instantly share code, notes, and snippets.

@samthetechie
Forked from spersico/index.mjs
Last active November 19, 2023 20:18
Show Gist options
  • Save samthetechie/9cab3c6b052f0570f3a0c95a692b62fe to your computer and use it in GitHub Desktop.
Save samthetechie/9cab3c6b052f0570f3a0c95a692b62fe to your computer and use it in GitHub Desktop.
Liberating Huawei Notes "HTML" (actually JSON) to TXT
import { writeFile, readdir, readFile, mkdir } from 'node:fs/promises';
import path from 'node:path';
/*
Steps to get/export your notes from Huawei Notes:
1. Login into a Huawei Account in the phone.
2. Activate in your phone, the Notes Syncing, which is inside of Huawei Account > Cloud
3. Log in into https://cloud.huawei.com
4. Go to https://cloud.huawei.com/home#/account/gdpr, and click on Download Notes
5. This will give you a zip file with a password. Extract the zip file into a folder.
6. Copy this file into the root directory folder as index.mjs
7. You'll need NodeJs installed for this: https://nodejs.org/en/
I made and tested this on v18+, but v19 and v16 should also work
8. open a console/terminal (see how to do that in your OS), and run in it "node index.mjs"
9. Your notes will be in the folder called notes
10. Each note will be a new file in this folder using <$title>.txt as the filename.
Improvements from previous gist: https://gist.github.com/spersico/19b92f2c37f01118c19f2ef9c113f0d7
1. cleaned up the title
2. each note to a new file
3. all notes to a folder called notes
4. btw actually the content is located in the json files... the html files are just a viewer webapp)
*/
async function readNotes() {
console.log(`πŸ“ | Huawei Notes HTML -> TXT`);
const __dirname = path.resolve(path.dirname(''));
const outputDir = path.join(__dirname, 'notes');
try {
await mkdir(outputDir);
console.log(`πŸ“ | Created output directory: ${outputDir}`);
} catch (error) {
console.error(`πŸ› | > Error creating output directory: `, outputDir, error);
return;
}
console.log(`πŸ“ | > Reading Directory:`, __dirname);
const folders = [];
const files = await readdir(__dirname, {
withFileTypes: true,
});
files.forEach((file) => {
if (file.isDirectory()) folders.push(file.name);
});
console.log(`πŸ“ | > Notes found:`, folders.length);
await Promise.all(
folders.map(async (folder) => {
const route = path.join(__dirname, folder, 'json.js');
try {
const weirdJs = await readFile(route, 'utf8');
const noteData = JSON.parse(weirdJs.replace('var data = ', '')).content;
const noteTitle = sanitizeTitle(noteData.title.split('\n')[0].trim());
const noteContent = noteData.content.trim();
await writeFile(path.join(outputDir, `${noteTitle}.txt`), noteContent, 'utf8');
console.log(`πŸ“ | > Note "${noteTitle}" exported successfully.`);
} catch (error) {
console.error(`πŸ› | > Error: `, route, error);
}
})
);
console.log(`πŸ“ | Notes successfully exported as individual text files! πŸŽ‰`);
}
function sanitizeTitle(title) {
const sanitized = title.replace(/[\\/:"*?<>|]/g, '_').replace(/\n/g, '').trim();
return sanitized || `Note_${Date.now()}`;
}
readNotes();
@pat-exe
Copy link

pat-exe commented Nov 16, 2023

When I run the script, it is only able to locate 28 out of my 286 notes. And the 'notes' folder that the script produces has no content located inside. Does the extracted zip file also need to be in the root directory?

@samthetechie
Copy link
Author

To be fair I improvised this with a friend as a one time thing back in summer, I hope the gist helps you to get further. What is different about those 28 notes from the rest? I wrote some notes at the top and linked to the previous gist. "6. Copy this file (i.e. the zip) into the root directory folder as index.mjs"

@pat-exe
Copy link

pat-exe commented Nov 17, 2023

Ok, what I was referring to was the actual huawei notes zip file from the cloud. After downloading it, if I extract the zip it becomes a folder called Notes202311.... Inside that folder is another folder called Notes, and then inside that is all my individual notes in folders containing a json and html file.

I was just wondering if maybe this had to be moved to the same place as the index.mjs file, or that didn't matter. And perhaps those other notes contained images in them or were checklists. That's the only thing I can think of that would make them different.

Update: I decided to just manually copy and paste the notes into notepad files, which took some time but worked in the end. All my data has now been saved from my old phone. Even though I didn't use the script, thanks for providing it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment