Created
July 4, 2021 11:08
-
-
Save Yelmor/271502983516ec421f1aecf5343bf78b to your computer and use it in GitHub Desktop.
export data from leanote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * leanote 接口参考:https://github.com/leanote/desktop-app/blob/c34c847586fbb566eb8ea4e87f2e7acd5c088221/src/api.js | |
| */ | |
| import Axios from 'axios'; | |
| import * as fs from 'fs/promises'; | |
| import * as path from 'path'; | |
| import * as Debug from 'debug'; | |
| const log = Debug('leanote_export'); | |
| const email = process.env.LEANOTE_USER; | |
| const pwd = process.env.LEANOTE_PASSWORD; | |
| const baseURL = process.env.LEANOTE_HOST || 'https://leanote.com'; | |
| let TOKEN = ''; | |
| const requester = Axios.create({ | |
| baseURL, | |
| }); | |
| requester.interceptors.request.use((config) => { | |
| config.params = { | |
| ...config.params, | |
| token: TOKEN, | |
| }; | |
| log('token', TOKEN); | |
| return config; | |
| }); | |
| export async function login() { | |
| const { data } = await requester.get('/api/auth/login', { | |
| params: { | |
| email, | |
| pwd | |
| } | |
| }); | |
| TOKEN = data.Token; | |
| } | |
| export async function getNotes() { | |
| const { data } = await requester.get('/api/note/getSyncNotes', { | |
| params: { | |
| afterUsn: -1, | |
| maxEntry: 1000, | |
| } | |
| }); | |
| return data; | |
| } | |
| function escapeFileName(name: string) { | |
| return name.replace(/[\<\>\:\"\/\\\|\?\*]/g, '_'); | |
| } | |
| export async function saveNote(title: string, noteId: string) { | |
| const { data: { Content } } = await requester.get('/api/note/getNoteContent', { | |
| params: { | |
| noteId, | |
| } | |
| }); | |
| const escapedTitle = escapeFileName(title); | |
| let filePath = path.resolve(`${escapedTitle}.md`); | |
| try { | |
| await fs.stat(filePath) | |
| filePath = path.resolve(`${escapedTitle}-${noteId}.md`); | |
| } catch(error) { | |
| } | |
| await fs.writeFile(filePath, Content); | |
| } | |
| (async() => { | |
| await login(); | |
| const notes = await getNotes(); | |
| log('got', notes.length, 'notes'); | |
| for (const item of notes) { | |
| if (item.IsDeleted || item.IsTrash || !item.IsMarkdown) { | |
| log('skip', item.NoteId); | |
| continue; | |
| } | |
| try { | |
| await saveNote(item.Title || item.NoteId, item.NoteId); | |
| log('saved', item.Title, item.NoteId); | |
| } catch(error) { | |
| log('error on', item, error); | |
| } | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment